2011-10-24 32 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