2

我们已经在 iOS 6 环境中编译和部署了一个应用程序。但是,现在该应用程序在 iOS 7 中每当显示警报时都会崩溃。应用程序只是在[alertView 显示] 中崩溃;

但是,同样的应用程序在 iOS 6 中运行完美。

显示警报的代码

-(void)displayAlertWithMessage:(NSString *)message withTitle:(NSString *)title andTag:(int)tag
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

像这样打电话

[self displayAlertWithMessage:@"Please enter valid username!" withTitle:nil andTag:1];

这是崩溃日志。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/developer/Library/Application Support/iPhone Simulator/7.0/Applications/A0BCB945-8E4A-4D06-BEE8-240FF45ECF78/MyProject.app> (loaded)' with name '_UIModalItemAppViewController''
*** First throw call stack:
(
    0   CoreFoundation                      0x003176f4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x027c88b6 objc_exception_throw + 44
    2   CoreFoundation                      0x003174cb +[NSException raise:format:] + 139
    3   UIKit                               0x015d1bec -[UINib instantiateWithOwner:options:] + 951
    4   UIKit                               0x01444f05 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
    5   UIKit                               0x014456ad -[UIViewController loadView] + 302
    6   UIKit                               0x014459ae -[UIViewController loadViewIfRequired] + 78
    7   UIKit                               0x01445eb4 -[UIViewController view] + 35
    8   UIKit                               0x018dc9da -[_UIModalItemsCoordinator _presentingViewControllerForAlertCompatibilityCreateIfNeeded:] + 248
    9   UIKit                               0x018dc8dd -[_UIModalItemsCoordinator _presentingViewControllerForAlertCompatibility] + 41
    10  UIKit                               0x01812801 -[UIAlertView(Private) popupAlertAnimated:animationType:atOffset:] + 382
    11  UIKit                               0x01812c1d -[UIAlertView(Private) popupAlertAnimated:animationType:] + 56
    12  UIKit                               0x01817c17 -[UIAlertView showWithAnimationType:] + 48
    13  UIKit                               0x01817c45 -[UIAlertView show] + 41
    14  MyProject                        0x000330a8 -[LoginViewController displayAlertWithMessage:withTitle:andTag:] + 232
    15  MyProject                        0x00032e8b -[LoginViewController isValidContent] + 299
    16  MyProject                        0x00033136 -[LoginViewController showSetPinView] + 54
    17  libobjc.A.dylib                     0x027da874 -[NSObject performSelector:withObject:withObject:] + 77
    18  UIKit                               0x0133524c -[UIApplication sendAction:to:from:forEvent:] + 108
    19  UIKit                               0x013351d8 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    20  UIKit                               0x0142ba5d -[UIControl sendAction:to:forEvent:] + 66
    21  UIKit                               0x0142be20 -[UIControl _sendActionsForEvents:withEvent:] + 577
    22  UIKit                               0x0142b0cf -[UIControl touchesEnded:withEvent:] + 641
    23  UIKit                               0x0137221d -[UIWindow _sendTouchesForEvent:] + 852
    24  UIKit                               0x01372e84 -[UIWindow sendEvent:] + 1232
    25  UIKit                               0x01346b86 -[UIApplication sendEvent:] + 242
    26  MyProject                        0x000c2c75 -[Application sendEvent:] + 101
    27  UIKit                               0x0133135f _UIApplicationHandleEventQueue + 11421
    28  CoreFoundation                      0x002a096f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    29  CoreFoundation                      0x002a02fb __CFRunLoopDoSources0 + 235
    30  CoreFoundation                      0x002bd3ce __CFRunLoopRun + 910
    31  CoreFoundation                      0x002bcbf3 CFRunLoopRunSpecific + 467
    32  CoreFoundation                      0x002bca0b CFRunLoopRunInMode + 123
    33  GraphicsServices                    0x036f0a27 GSEventRunModal + 192
    34  GraphicsServices                    0x036f084e GSEventRun + 104
    35  UIKit                               0x01333f0b UIApplicationMain + 1225
    36  MyProject                        0x00006612 main + 178
    37  MyProject                        0x00006555 start + 53
)
libc++abi.dylib: terminating with uncaught exception of type NSException

提前致谢。

4

2 回答 2

2

根据崩溃日志,它告诉我“[UIViewController _loadViewFromNibNamed:bundle:]”无法加载 nib 文件。

但是,在我的项目中,我为UIViewController类创建了一个类别并覆盖了该-(id)init方法。直到 ios6,虽然显示警告,但此类别方法根本没有执行,但从 ios7 开始,覆盖了UIViewController的init()方法。

因此,我通过在 UIViewController 类别的 init() 方法中添加另一个验证来解决此问题,以检查类方法是否为 UIAlertView,如果它是 UIAlertView 则只需调用[super init]; 而不是执行与视图控制器相关的其他工作。

示例代码:

    - (id)init
    {
          NSString *viewControllerName = [NSString stringWithFormat:@"%@",[self class]];
          if([viewControllerName isEqualToString:@"UIAlertView"])
          {
             self = [super init];
          }
          else
          {
             // my stuff
          }
          return self;
    }
于 2013-10-11T06:17:25.757 回答
-1

你可以测试它..尽量避免零..而不是放一些东西......比如[self displayAlertWithMessage:@“请输入有效的用户名!” withTitle:@"Hello" andTag:1]

并且您需要发布警报视图,因此在函数中编写发布代码...

于 2013-09-24T11:45:59.787 回答