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

Mil*_*dia 2 xcode uibutton ios unrecognized-selector

我在使用某些 UIButton 来调用 Tab Bar 应用程序中的操作时遇到问题。

我的 .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
Run Code Online (Sandbox Code Playgroud)

我的 .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
Run Code Online (Sandbox Code Playgroud)

我的 .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'
Run Code Online (Sandbox Code Playgroud)

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

我在调试器中键入“po 0x6b328d0”以找出问题对象是什么,它作为 UIViewController 返回,这可能意味着在调用操作之前释放了视图控制器?什么会导致这种情况?

谢谢。


ContactViewController.xib 的界面生成器图片

Fir*_*eer 5

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

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

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

  • 不,问题在于创建或加载视图控制器时。当您稍后尝试使用按钮时,您会注意到它。我的猜测是标签栏控制器正在加载一个 UIViewController,而不是这个视图控制器。UITabBarController 在哪里?你还没有显示代码或笔尖。 (3认同)