【问题标题】:App crashes on [UIAlertView show]应用程序在 [UIAlertView 显示] 上崩溃
【发布时间】:2011-05-20 11:09:26
【问题描述】:

只要我想显示警报,我的应用就会崩溃。这段代码非常基本,我找不到任何问题。有人可以验证我是否做错了什么吗?

@implementation SampleClass
- (void) showAlert
{
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Make an informed choice"
                      message:@"Descriptive text"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"OK", nil];
[alert show];
[alert release];    
}
@end

堆栈是:

0   libobjc.A.dylib                 0x3006bc98 objc_msgSend + 16
1   NESampleApp                     0x002ec5bc 0x1000 + 3061180
2   UIKit                           0x35584bee -[UIWindow _sendTouchesForEvent:] + 362
3   UIKit                           0x35584568 -[UIWindow sendEvent:] + 256
4   UIKit                           0x3556d30c -[UIApplication sendEvent:] + 292
5   UIKit                           0x3556cc4c _UIApplicationHandleEvent + 5084
6   GraphicsServices                0x35350e70 PurpleEventCallback + 660
7   CoreFoundation                  0x3599da90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
8   CoreFoundation                  0x3599f838 __CFRunLoopDoSource1 + 160
9   CoreFoundation                  0x359a0606 __CFRunLoopRun + 514
10  CoreFoundation                  0x35930ebc CFRunLoopRunSpecific + 224
11  CoreFoundation                  0x35930dc4 CFRunLoopRunInMode + 52
12  GraphicsServices                0x35350418 GSEventRunModal + 108
13  GraphicsServices                0x353504c4 GSEventRun + 56
14  UIKit                           0x35597d62 -[UIApplication _run] + 398
15  UIKit                           0x35595800 UIApplicationMain + 664
16  NESampleApp                     0x0017d10c 0x1000 + 1556748
17  NESampleApp                     0x007d3720 0x1000 + 8202016

更新:在将代理设置为nil 后,我得到了更多信息以及崩溃:

<Error>: -[CALayer isTransformGestureInput]: unrecognized selector sent to instance 0xb4c8a0
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer isTransformGestureInput]: unrecognized selector sent to instance 0xb4c8a0'

【问题讨论】:

  • 您的代码似乎是正确的,请检查您调用此方法的位置
  • 你有没有写过委托方法。它有时也负责崩溃
  • 没有。这个类没有其他方法。是否存在显示警报可能导致应用程序行为异常的已知情况?
  • 是的,试一试 write delegate:nil insetead of delegate:self
  • 发生崩溃的类是什么?尝试 gdb 提示&gt; po 0xb4c8a0(或与崩溃相关的任何对象)。

标签: objective-c uialertview show ios-4.2


【解决方案1】:

试试这个而不是[alert show]

[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

【讨论】:

    【解决方案2】:

    虽然这个答案可能无法解决这个特定(相当老的)问题,但我确实来到这里,因为我有同样的症状。我的问题的答案是我没有将“nil”作为 otherButtonTitles 的第二项传递:@“OK”。

    不好:

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Make an informed choice"
                          message:@"Descriptive text"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"OK"];
    

    好:

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Make an informed choice"
                          message:@"Descriptive text"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"OK", nil];
    

    有趣的是,这只崩溃了我的设备,而不是 iOS 模拟器。

    【讨论】:

      【解决方案3】:

      您的 otherButtonTitles 参数需要为零终止。

      一般情况下,采用可变数量参数的方法需要以 nil 结尾。例如:

      [NSArray arrayWithObjects:objA, objB, nil];
      

      在你的情况下:

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"otherbutton", nil];
      

      【讨论】:

        【解决方案4】:

        可能是您在块内调用此方法吗?这将导致它崩溃。在你的街区内试试这个:

        [self performSelectorOnMainThread:@selector(showAlert) 
        withObject:nil waitUntilDone:YES];
        

        遇到了同样的问题。这解决了它。答案来自:How do display a UIAlertView from a block on iOS?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-07
          • 1970-01-01
          • 2013-06-23
          • 1970-01-01
          • 1970-01-01
          • 2012-11-19
          • 1970-01-01
          • 2018-11-17
          相关资源
          最近更新 更多