I'm implementing an app iOS with the MVC design pattern.
The app has 5 interfaces and I proceeded in this way:
In the model I have implemented a method that sends messages to the web service to request the data. According to the MVC, the Controller must receive data from the Model and send them to the View, so in the Controller I have implemented a method that invokes the method of the Model. In the View I instantiate an object Controller and call the Controller method. When the app starts, Xcode starts up only the commands of the method of AppDelegate (Controller) without reading the call to the Model method.
I apologize if the reasoning is twisted. In summary:
// AppDelegate.h
#import "WebServiceModel.h"
@interface AppDelegate: UIResponder <UIApplicationDelegate> {
WebServiceModel *model;
}
@property (retain, nonatomic) WebServiceModel *model;
- (void) func;
_________________
// AppDelegate.m
@implementation AppDelegate
@syntesize model;
- (void) func {
NSLog(@"OK!");
[model function];
}
@end
_________________
// WebServiceModel.h
#import "AppDelegate.h"
@interface WebServiceModel: NSObject <NSXMLParserDelegate> {
AppDelegate *controller;
}
- (void) function;
_________________
// WebServiceModel.m
@implementation WebServiceModel
- (void) function {
NSLog(@"YES!");
//other instructions
}
@end
_________________
// View Controller.h
#import "AppDelegate.h"
@interface ViewController: UIViewController {
AppDelegate *controller;
}
_________________
// ViewController.m
@implementation ViewController
- (void) viewDidLoad {
NSLog(@"OH!");
controller = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[controller func];
}
@end
When the app starts, in "All Output" you see only "OH!" and "OK!", but no "YES!".
Because the Model's method "function" isn't called?
Thanks to those who answer me!
You haven't actually created an instance of the model object, so what's actually happening is you're invoking -function on nil. Fixing this is easy, add the following method to AppDelegate:
- (id)init
{
self = [super init];
if (nil != self)
{
self.model = [[WebServiceModel alloc] init];
}
return self;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加