我已经阅读了大量关于iPhone开发的书籍和示例,但我注意到MVC的概念并没有真正被正确地教授(尽管作者确实说Xcode“借给”了MVC的编码方式)。
一个简单的例子。我想做一个简单的计算器应用程序(就像许多刚开始做的人一样)。
我有一个工作版本,我的所有代码都在xxxxxViewController.m文件中。行动和出口都工作得很好。这种方法的问题是,如果我想拥有多个视图(普通计算器和科学计算器),我需要复制并粘贴我的代码,所以我现在有两个版本。我显然是在努力避免这种情况。
因此,我创建了自己的类(基于NSObject)作为我的CalculatorEngine。
问题是,当我试图分配和初始化我的CalculatorEngine时,我收到了诸如“用不同的类型重新定义CalculatorEngine”和“类型说明符缺失,缺省为整型”之类的错误。
我想我遗漏了一些明显的东西。
你能为我指出任何类型的样本的方向吗?其中一个单独的类被用作“引擎”,而不是将代码放在xxxxViewController中?
在这一点上,下面的代码实际上没有做任何事情。我只是想在CalculatorViewController.m中获取可用的CalculatorEngine对象。这就是我收到错误的地方。
// CalculatorAppDelegate.h
// Calculator
#import <UIKit/UIKit.h>
@class CalculatorViewController, CalculatorEngine;
@interface CalculatorAppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet CalculatorViewController *viewController;
@end
// CalculatorAppDelegate.m
// Calculator
#import "CalculatorAppDelegate.h"
#import "CalculatorViewController.h"
@implementation CalculatorAppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
}
@end
// CalculatorViewController.h
// Calculator
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController
@end
// CalculatorViewController.m
// Calculator
#import "CalculatorViewController.h"
#import "CalculatorEngine.h"
@implementation CalculatorViewController
// This was wrong here. Now moved to viewDidLoad().
//CalculatorEngine *CalcEng;
//CalcEng = [[CalculatorEngine alloc] init];
// trouble here. CalcEng is unknow.
-(IBAction)buttonPressed:(id)sender {
[CalcEng setRegisterX:1];
}
- (void)viewDidLoad
{
CalculatorEngine *CalcEng;
CalcEng = [[CalculatorEngine alloc] init];
[super viewDidLoad]
}
- (void)didReceiveMemoryWarning
{
}
- (void)viewDidUnload
{
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
}
@end
// CalculatorEngine.h
// Calculator
#import <Foundation/Foundation.h>
@interface CalculatorEngine : NSObject
@end
// CalculatorEngine.m
// Calculator
#import "CalculatorEngine.h"
@implementation CalculatorEngine
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
@end发布于 2011-10-24 10:48:11
此代码位于错误的位置:
CalculatorEngine *CalcEng;
CalcEng = [[CalculatorEngine alloc] init];把它放到-(void)viewDidLoad中。
你不能调用你的方法,因为你的视图控制器没有保持对CalcEngine的引用(顺便说一下,像这样的变量应该是驼峰大小写的,以符合命名约定,所以它应该是calcEngine)。要保留引用,您需要添加一个iVar,或者更恰当地说,添加一个名为CalcEngine的属性。为此,您的CalculatorViewController标头应如下所示:
// CalculatorViewController.h
// Calculator
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController {
CalculatorEngine *CalcEngine;
}
@property (retain) CalculatorEngine *CalcEngine;
@end您的实现将如下所示:
// CalculatorViewController.m
// Calculator
#import "CalculatorViewController.h"
#import "CalculatorEngine.h"
@implementation CalculatorViewController
// This was wrong here. Now moved to viewDidLoad().
//CalculatorEngine *CalcEng;
//CalcEng = [[CalculatorEngine alloc] init];
// trouble here. CalcEng is unknow.
-(IBAction)buttonPressed:(id)sender {
[CalcEng setRegisterX:1];
}
- (void)viewDidLoad
{
CalcEng = [[CalculatorEngine alloc] init];
[super viewDidLoad]
}
- (void)didReceiveMemoryWarning
{
}
- (void)viewDidUnload
{
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
}
@end不要误解这一点,但您应该花一些时间阅读Apple's Objective C guide。您遇到的问题与MVC无关,而是与objective-c有关。
发布于 2011-10-24 10:17:37
你有没有?
@class CalculatorEngine;但不是吗?
#import "CalculatorEngine.h"https://stackoverflow.com/questions/7870621
复制相似问题