我一直在阅读有关iPhone开发的大量书籍和做例子,但我注意到MVC的想法并没有被正确地教授(尽管作者确实说Xcode“适合”MVC编码方式)。 / p>
一个简单的例子。我想制作一个简单的计算器应用程序(正如许多人开始做的那样)。
我有一个工作版本,包含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 :(得分:1)
此代码位于错误的位置:
CalculatorEngine *CalcEng;
CalcEng = [[CalculatorEngine alloc] init];
将其放入-(void)viewDidLoad
。
<强>更新强> 你不能调用你的方法,因为你的视图控制器没有保留对CalcEngine的引用(顺便说一句,像这样的变量应该是为了与命名约定保持一致,所以它将是calcEngine)。要保留引用,您需要添加一个名为CalcEngine的iVar或更合适的属性。为此,您的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。
有关答案 1 :(得分:0)
你呢?
@class CalculatorEngine;
但不是吗?
#import "CalculatorEngine.h"