调用UIActionSheet时应​​用程序崩溃

时间:2011-10-21 16:39:57

标签: objective-c ios ipad

我正在为iPad编写应用程序。在这个应用程序中,我必须在所有屏幕上的导航栏上使用“共享”按钮。我在Home课程中应用了“共享”按钮。我在导航栏上编写了以下代码来应用按钮:

CGRect frame5 = CGRectMake(835.0, 0.0, 40.0, 40.0);
UIImage *buttonImage5 = [UIImage imageNamed:@"ShareIcon.png"];
UIButton *ShareButton = [UIButton buttonWithType:UIButtonTypeCustom];
ShareButton.frame = frame5;
[ShareButton setBackgroundImage:buttonImage5 forState:UIControlStateNormal];
ShareButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
ShareButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[ShareButton addTarget:self action:@selector(actionSheet) forControlEvents:UIControlEventTouchUpInside];
[ShareButton setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar addSubview:ShareButton];

分享按钮用于通过电子邮件发送或打印屏幕。为此,我使用了UIActionSheet。我编写了以下方法来应用UIActionSheet:

-(void)actionSheet:(id)sender
{
    UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email", @"Print", nil];
    [popup showFromRect:CGRectMake(720.0, 3.0, 40.0, 40.0) inView:[self view] animated:TRUE];
    [popup showFromBarButtonItem:(UIBarButtonItem *)sender animated:TRUE];
    [popup release];
}

此方法应用于“共享”按钮。当我构建并运行我的应用程序时,没有错误或警告,即构建成功。但是,当我单击共享按钮时,应用程序崩溃并在调试器控制台窗口中显示以下内容:

[Session started at 2011-10-21 21:26:58 +0530.]
2011-10-21 21:27:03.895 NewBostonEndoscopy[1351:207] -[Home actsht]: unrecognized selector sent to instance 0x6227010
2011-10-21 21:27:03.898 NewBostonEndoscopy[1351:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Home actsht]: unrecognized selector sent to instance 0x6227010'

我不明白为什么会这样。有什么猜测? 任何帮助将受到高度赞赏。 感谢致敬 Prateek

3 个答案:

答案 0 :(得分:1)

您正在显示弹出窗口两次,我不确定这是否是您崩溃的原因,但请尝试删除该行

[popup showFromRect:CGRectMake(720.0, 3.0, 40.0, 40.0) inView:[self view] animated:TRUE];

答案 1 :(得分:1)

[ShareButton addTarget:self action:@selector(actionSheet) forControlEvents:UIControlEventTouchUpInside]; 改成: [ShareButton addTarget:self action:@selector(actionSheet:) forControlEvents:UIControlEventTouchUpInside]; 失踪的':'在actionSheet之后是罪魁祸首。

答案 2 :(得分:0)

正如日志所说,您在-actsht上调用了Home,这显然没有实现具有此名称的方法。尝试搜索您的文档actsht,看看您的意思。

您可能在UIBarButtonItem初始值设定项中使用过它,例如-initWithBarButtonSystemItem:target:action:,并为action指定了不存在的选择器。编译器不会注意到这一点。