MillerMedia
MillerMedia

Reputation: 3671

IBAction for UIButton Causes an unrecognized selector sent to instance error (iOS)

I am having trouble using some UIButtons to call actions in my Tab Bar application.

My .h file:

#import <UIKit/UIKit.h>

@interface ContactViewController : UIViewController {
    UIButton *callTollFreeButton;
    UIButton *callLocalButton;
    UIButton *emailButton;
}

@property (nonatomic, retain) IBOutlet UIButton *callTollFreeButton;
@property (nonatomic, retain) IBOutlet UIButton *callLocalButton;
@property (nonatomic, retain) IBOutlet UIButton *emailButton;

-(IBAction)callPhone:(id)sender;
-(IBAction)callTollFree:(id)sender;
-(IBAction)clickEmailButton:(id)sender;
@end

My .m file:

#import "ContactViewController.h"

@implementation ContactViewController
@synthesize callLocalButton,callTollFreeButton,emailButton;

-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:7576236600"]];
}

-(IBAction)callTollFree:(id)sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:8003334645"]];
}

-(IBAction)clickEmailButton:(id)sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:[email protected]?subject=Hello"]];    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
    }
return self;
}

- (void)dealloc
{
[callLocalButton release];
[callTollFreeButton release];
[emailButton release];
[super dealloc];
}

- (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
{
[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 (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

My .m file:

The error message I get is (I get error message for any button clicked, this one is clicking the e-mail button in particular):

2011-11-19 18:39:38.786 Miller Tab Bar[761:207] -[UIViewController clickEmailButton:]: unrecognized selector sent to instance 0x6b29f40
2011-11-19 18:39:38.790 Miller Tab Bar[761:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController clickEmailButton:]: unrecognized selector sent to instance 0x6b29f40'

I've tried to connect and reconnect the outlets and actions to the objects on the view controller. Is it possible that it crashes because the SDK can't run test phone calls or e-mails?

I typed 'po 0x6b328d0' into the debugger to find out what the problem object is and it came back as a UIViewController which possibly means that the View Controller is being released before the action is called? What would be causing that?

Thanks.


Picture of Interface Builder for ContactViewController.xib

Upvotes: 2

Views: 9804

Answers (3)

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

In your Nib or storyboard, it looks like your view controller instance is of type UIViewController instead of ContactViewController.

So select the view controller in your Nib (or storyboard), and change its type to ContactViewController.

Of course, if this view controller isn't being loaded from a nib or storyboard, then just check where you create it and make sure you created an instance of the correct class.

Upvotes: 5

Jesse Black
Jesse Black

Reputation: 7986

It is a bit bizarre. I think your viewcontroller isn't being set up properly.

The bizarre part is you shouldn't be able to set the actions if it is not set up properly (File Owner- ContactViewController)

When I send an unrecognized selector to a custom ViewController, it says

-[customViewController foo]: unrecognized selector sent to instance 0x6a2b440

It seems the xib's actual file owner is being instantiated as a UIViewController

Upvotes: 1

Peter Kazazes
Peter Kazazes

Reputation: 3628

Try changing:

UIButton *callTollFreeButton;
UIButton *callLocalButton;
UIButton *emailButton;

to

IBOutlet UIButton *callTollFreeButton;
IBOutlet UIButton *callLocalButton;
IBOutlet UIButton *emailButton;

And hook up the UILabels to their respective buttons. See if that helps.

Upvotes: 0

Related Questions