I call an instance to a class in my main.m to my Controls.m class but it seems to be giving me a “Unrecognized selector sent to instance” error. Any idea what I am doing wrong here? Every time I hit the button it just crashes, but isn’t Controls.m set to self in this code? It shouldn’t have trouble finding the test selector action.
Main.m
- (void)loadView {
Controls *ct = [[Controls alloc] init];
[ct addControls];
[ct release];
}
Controls.m
- (void)addControls {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, mv.frame.size.height-60, mv.frame.size.width, 40)];
UIBarButtonItem *barBtnDataOverlay = [[UIBarButtonItem alloc] initWithTitle:@"Test Button" style:UIBarButtonSystemItemAction target:self action:@selector(test)];
NSArray *toolbarButtons = [[NSArray alloc] initWithObjects:barBtnDataOverlay, nil];
toolbar.items = toolbarButtons;
[mv addSubview:toolbar];
[barBtnDataOverlay release];
[toolbar release];
}
- (void)test {
NSLog(@"TEST button hit");
}
ctwill be dealloced by[ct release]as no retain is left. Try to add actretin property to your class to keep it around.In class definition:
In your implementation:
…
Change your loadView to something like:
Or even neater:
You should also release
ctsomewhere like inviewDidUnloadBTW, is this in a
UIViewControllerclass? then theloadViewmethod should probably assign theviewinstance variable. If you look in the documentation forUIViewControlleryou will see this: