当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Xcode development model-view-controller (MVC) issue

问题描述:

I have been reading numerous books on iPhone development and doing the examples but I notice the idea of MVC is not really being taught correctly (although the authors do say that Xcode "lends itself" to the MVC way of coding).

A quick example. I want to make a simple calculator app (as many who are starting out do).

I have a working version with all of my code inside the xxxxxViewController.m file. Actions and Outlets all working well. The trouble with this approach is if I want to have multiple views (normal calculator and scientific calculator) I would have copy and paste my code so I now have two versions. I am clearly trying to avoid this.

So, I have created my own class (based on NSObject) as my CalculatorEngine.

Trouble is when trying to allocate and initialise my CalculatorEngine I receive errors such as "Redefinition of CalculatorEngine with a different type" and "Type specifier missing, defaults to int".

I guess I am missing something obvious.

Can you point me in the direction of a sample of any kind where a separate class is being used as an "engine" rather than having the code inside the xxxxViewController?

At this point the code below does not actually do anything. I am just trying to get the CalculatorEngine object useable in CalculatorViewController.m. This is where I receive the error.

// 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

网友答案:

This code is in the wrong location:

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

Put that into -(void)viewDidLoad.

UPDATE You cannot call your method because your view controller is not keeping a reference to the CalcEngine (by the way, variables like this should be camel cased to keep in line with naming conventions, so it would be calcEngine). To keep a reference you need to add an iVar, or more appropriately, a property called CalcEngine. To do this, your CalculatorViewController header would look like this:

//  CalculatorViewController.h
//  Calculator

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController {
   CalculatorEngine *CalcEngine;
}

@property (retain) CalculatorEngine *CalcEngine;
@end

Your implementation would look like this:

//  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

Don't take this the wrong way, but you should spend some time reading Apple's Objective C guide. The problems you are having have nothing to do with MVC, but with objective-c.

网友答案:

Did you?

@class CalculatorEngine;

But not?

#import "CalculatorEngine.h"
分享给朋友:
您可能感兴趣的文章:
随机阅读: