Call of IBAction in a viewcontroller added to IOC container causes crash
Hi,
It's possible to work with XIB and IBAction ?
- create a UIVIiewController with a XIB named RootViewController, the property view is an UIView, I place a UIButton where there is an IBAction on Touch Up Inside event, my code is with this interface : @class SomeService;
@interface RootViewController : UIViewController { SomeService *injService; }
@property (nonatomic, retain) SomeService *service;
- (IBAction)click;
@end
and with this implementation : @implementation RootViewController
@synthesize service = injService;
- (void)viewDidLoad { NSLog(@"%@", [self.service doService]); }
- (IBAction)click { // causes EXEC_BAD_ACCESS }
- (void)dealloc { self.service = nil; [super dealloc]; }
@end
- add it to IOC container : [[self.container withInitSelector:@selector(initWithNibName:bundle:) params:@"RootViewController", [NSBundle mainBundle], nil] addComponent:[RootViewController class]];
- place root controller view into window : UIViewController *rootViewController = (UIViewController *)[self.container getComponent:[RootViewController class]]; [self.window addSubview:rootViewController.view];
It was just a memory problem. My RootViewController instance was not retained in my AppDelegate.
instead of :
UIViewController *rootViewController = (UIViewController *)[self.container getComponent:[RootViewController class]]; [self.window addSubview:rootViewController.view];
write :
UIViewController *ctrl = (UIViewController *)[self.container getComponent:[RootViewController class]];
rootViewController = [ctrl retain]; // rootViewController is a private variable of my class
[self.window addSubview:rootViewController.view];
I thought that, because I have sample XIB application which run without problems. But carefully with that retain. More transparent and readable is assign rootViewController instance into AppDelegate property.