I just started building an app using X-Code 4.2. I created the app with the single view application template.
I created a MainIconViewController which is a simple VC which contains an image, label and button. I add the MainIconViewController view to my mainViewController view and when the button is pressed I get a crash in Main.m that says:
-[__NSCFType mainButtonPressed:]: unrecognized selector sent to instance 0xb867ba0
The line in Main.m where the crash occurs is:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
right now buttonPressed: is completely empty.
Any ideas how to fix this?
I just tried making a whole new project to re-test this. I created the project, created a viewController subclass that contained a single button. I then create an instance of this view in the main view controller and add it to the view. When I press the button, the application crashes just like before.
EDIT:
Here’s All the code:
//MainIconViewController.h
@protocol MainIconViewControllerDelegate <NSObject>
-(void) openFileNamed:(NSString *)name;
-(void) createNewFile;
@end
#import <UIKit/UIKit.h>
@interface MainIconViewController : UIViewController {
}
@property (assign) id <MainIconViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (void)mainButtonPressed:(id)sender;
@end
//////////////////////////////
//MainIconViewController.m
#import "MainIconViewController.h"
@implementation MainIconViewController
@synthesize titleLabel;
@synthesize imageView;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setTitleLabel:nil];
[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)mainButtonPressed:(id)sender{
}
@end
First your method have to be declared like this :
Then you must call your controller like this :
(safer, but that’s ok if your xib file is named like the controller)
And of course, don’t forget to link you button into IB with the action.
Check also that your
File's owner(into IB) for the xib that corresponds toMainIconViewControlleris set to MainIconViewController.Note that you here have a memory leak. You may retain the controller in your class and release it whne not needed anymore. If release too early, it will crash because your view does not have a controller in memory when needed. If not released (like here you do), the controller keeps staying in memory and you have a memory leak.
Note that the way you do is not recommended by Apple (one view hierarchy = one view controller). If you use ARC, that could be the problem of your crash.