2011-10-24 4 views
1

私はiPhoneの開発とその例題について数多くの本を読んできましたが、MVCが実際に正しく教えられていないことに気付きました(ただし、Xcodeは "それ自身をMVCのコーディング方法に変換します)。Xcode開発モデルビューコントローラ(MVC)の問題

簡単な例です。私は単純な電卓アプリを作りたいと思っています。

私はxxxxxViewController.mファイルの中に自分のコードをすべて入れた作業用バージョンを持っています。アクションとアウトレットはすべてうまく動作します。このアプローチの問題は、複数のビュー(通常の電卓と科学計算機)を使用したい場合に、私は2つのバージョンを持つように私のコードをコピーして貼り付けることです。私は明らかにこれを避けようとしています。

私はCalculatorEngineとして独自のクラス(NSObjectベース)を作成しました。

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専用のものではありません。 "Type specifier missing、ints to default"は欠落したインクルードのように聞こえる。あなたのコード、または少なくともスケルトン(コードをメソッドの中に残す)を投稿してください。 –

+0

サンプルコードは、元の投稿に含まれています。 – David

答えて

1

このコードは、間違った場所にあります。

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

置くことを-(void)viewDidLoadに。

あなたのビューコントローラは、(ちなみに、このような変数は、ラクダが命名規則に沿って維持するために同棲しなければならないので、それはcalcEngineだろうCalcEngineへの参照を保持していないので、あなたはあなたのメソッドを呼び出すことはできませんUPDATE )。参照を保持するには、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とは関係がありませんが、目的はcです。

+0

そのトリックをしたようです。ありがとうございました。 :-) – David

+0

これで私はコンパイルすることができました。しかし、私のクラスのメソッドをCalculatorViewController.mから呼び出そうとすると、「宣言されていない識別子を使用する」というエラーが表示されます。元の投稿のコードを更新して、このメソッドをどのように呼び出そうとしているかを示します。 – David

+0

私の答えを更新しました。 – sosborn

0

でしたか?

@class CalculatorEngine; 

ただし、

#import "CalculatorEngine.h" 
+0

サンプルコードは、元の投稿に含まれています。 – David

関連する問題