UIButton的IBAction导致无法识别的选择器发送到实例错误(iOS)

时间:2011-11-19 23:49:52

标签: ios xcode uibutton unrecognized-selector

我无法使用某些UIButtons在我的Tab栏应用程序中调用操作。

我的.h文件:

#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

我的.m文件:

#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@email.com?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

我的.m文件:

我得到的错误信息是(我收到任何点击按钮的错误消息,特别是点击了电子邮件按钮):

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'

我尝试将插座和操作连接并重新连接到视图控制器上的对象。它是否可能崩溃,因为SDK无法运行测试电话或电子邮件?

我在调试器中键入'po 0x6b328d0'以找出问题对象是什么,它作为UIViewController返回,这可能意味着在调用操作之前释放View Controller?会导致什么?

感谢。


Picture of Interface Builder for ContactViewController.xib

3 个答案:

答案 0 :(得分:4)

在您的Nib或storyboard中,看起来您的视图控制器实例的类型为UIViewController而不是ContactViewController。

因此,在Nib(或storyboard)中选择视图控制器,并将其类型更改为ContactViewController。

当然,如果没有从笔尖或故事板加载此视图控制器,那么只需检查创建它的位置,并确保创建了正确类的实例。

答案 1 :(得分:0)

尝试更改:

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

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

UILabels连接到各自的按钮。看看是否有帮助。

答案 2 :(得分:0)

这有点奇怪。我认为你的viewcontroller没有正确设置。

奇怪的是,如果操作设置不正确,您应该无法设置操作(文件所有者 - ContactViewController)

当我将无法识别的选择器发送到自定义ViewController时,它会显示

- [customViewController foo]:无法识别的选择器发送到实例0x6a2b440

似乎xib的实际文件所有者被实例化为UIViewController