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

发布于 2024-12-17 04:39:44 字数 2995 浏览 2 评论 0原文

我在使用某些 UIButtons 来调用选项卡栏应用程序中的操作时遇到问题。

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

我的 .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 返回,这可能意味着视图控制器在调用操作之前被释放?是什么原因造成的呢?

谢谢。


ContactViewController.xib 的界面生成器图片

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や莫失莫忘 2024-12-24 04:39:44

在您的 Nib 或 Storyboard 中,您的视图控制器实例的类型似乎是 UIViewController 而不是 ContactViewController。

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

当然,如果此视图控制器不是从 nib 或 Storyboard 加载的,那么只需检查创建它的位置并确保创建了正确类的实例。

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.

酒中人 2024-12-24 04:39:44

这有点奇怪。我认为您的视图控制器设置不正确。

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

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

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

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

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

情深缘浅 2024-12-24 04:39:44

尝试更改:

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

并将

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文