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