基本使用UIAlertView中会导致EXC_BAD_ACCESS
我采取一个非常简单的iOS应用程序只是练显示弹出警报,我得到一个错误,当我按下alert按钮:基本使用UIAlertView中会导致EXC_BAD_ACCESS
线程1:EXC_BAD_ACCESS (码= 1,地址= 0x676f6f57)
这是代码:
- (IBAction)AlertButton {
alert = [[UIAlertView alloc]
initWithTitle:@"Alert" message:@"Alert"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:@"Apple", "Google" ,nil];
[alert show];}
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://apple.com"]];
}
if(buttonIndex == 2){
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://google.com"]];
}}
问题与UIAlertView
的构造,在该行:
otherButtonTitles:@"Apple", "Google" ,nil];
你忘了@
之前"Google"
。最后修改:
-(void)alertView :(UIAlertView *)alertView clickedButttonAtIndex:(NSInteger)buttonIndex{
通过
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
“此外,你必须在你的类上声明协议UIAlertViewDelegate的采用” - 不,你不需要,它只是编译器的提示。 – 2013-02-21 19:59:59
是的,你是对的,在这种情况下没有必要,我修复了答案 – tkanzakic 2013-02-21 20:03:46
谢谢。 (不是我主张避免它,因为这样做是很好的做法,只是为了记录 - 人们经常会忘记Objective-C是一种高度动态的语言,声明事关重大。) – 2013-02-21 20:04:48
真正的问题是在你的前会错过的,所以它不是NSString
,因此崩溃。
使用本 .H
不需要IBOutlet.just
UIAlertView *alert;
.M
alert = [[UIAlertView alloc]
initWithTitle:@"Alert"
message:@"Alert"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:@"Apple", @"Google", nil
];
非常感谢你:) – Rayan 2013-02-21 20:38:37
请不要张贴链接到您的整个项目。如果你不能足够简洁地描述你的问题以适应帖子内容,那么你的问题不适合堆栈溢出。 – 2013-02-21 19:40:29
我相信问题是你的button = AlertButton的名称,IBaction的名称也是AlertButton。上述代码中的UIAlertView没有任何问题。 – 2013-02-21 19:44:16