2011-10-24 30 views
1

我一直在阅读大量关于iPhone开发和书中示例的书籍,但我注意到MVC的想法并没有真正被正确教授(尽管作者确实说Xcode“本身“到MVC的编码方式)。Xcode开发模型 - 视图 - 控制器(MVC)问题

一个简单的例子。我想制作一个简单的计算器应用程序(许多人开始这样做)。

我有我的所有代码在xxxxxViewController.m文件内的工作版本。行动和奥特莱斯运作良好。这种方法的麻烦是如果我想有多个视图(普通计算器和科学计算器)我会复制并粘贴我的代码,所以我现在有两个版本。我清楚地试图避免这种情况。

所以,我创建了我自己的类(基于NSObject)作为我的CalculatorEngine。

问题是当试图分配和初始化我的CalculatorEngine我收到错误,如“用不同类型重新定义CalculatorEngine”和“缺少类型说明符,默认为int”。

我想我错过了一些明显的东西。

你能指点我一个单独的类被用作“引擎”而不是在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 
+0

这是一个Objective-C问题。这不是iPhone特有的。 “缺少类型说明符,默认为int”的部分听起来像是缺少包含。发布你的代码,或者至少是一个框架(在方法内部留下代码)。 –

+0

示例代码现已包含在原始文章中。 – David

回答

1

此代码是在错误的位置:

CalculatorEngine *CalcEng; 
CalcEng = [[CalculatorEngine alloc] init]; 

它放入-(void)viewDidLoad

UPDATE 你不能打电话给你的方法,因为您的视图控制器没有保持到CalcEngine参考(顺便说一下,像这样的变量应该是骆驼套管保持符合命名约定,所以这将是calcEngine )。为了保持参考,您需要添加一个名为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无关。

+0

这似乎是伎俩。谢谢。 :-) – David

+0

这让我编译。但是现在当试图从我的类中调用CalculatorViewController.m中的方法时,我收到错误“使用未声明的标识符”。我将更新原始文章中的代码,以显示我如何尝试调用该方法。 – David

+0

更新了我的答案。 – sosborn

0

对不对?

@class CalculatorEngine; 

但是不是?

#import "CalculatorEngine.h" 
+0

原始帖子中已包含示例代码。 – David