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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8198290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:29:37  来源:igfitidea点击:

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

iosxcodeuibuttonunrecognized-selector

提问by MillerMedia

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

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

My .h file:

我的 .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

My .m file:

我的 .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 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:

我的 .m 文件:

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?

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

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?

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

Thanks.

谢谢。



Picture of Interface Builder for ContactViewController.xib

ContactViewController.xib 的界面生成器图片

回答by Firoze Lafeer

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

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

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

因此,在您的 Nib(或故事板)中选择视图控制器,并将其类型更改为 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.

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

回答by Jesse Black

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)

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

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

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

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

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

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

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

回答by Peter Kazazes

Try changing:

尝试改变:

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

to

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

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

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