iphone - 访问 UIViewController 时的 SIGABRT

标签 iphone ios xcode uiviewcontroller

我习惯于从 Storyboard文件或 xib 文件加载 ViewController,所以当我第一次尝试在代码中加载时:

UIStoryboard *storyboard = self.storyboard;

ProfileViewController *profViewController = [storyboard instantiateViewControllerWithIdentifier:@"ProfileView"];

NSLog(@"Self View Frame: %@", NSStringFromCGSize(self.view.frame.size));
NSLog(@"ProfViewCont Frame: %@", NSStringFromCGSize(profViewController.view.frame.size));

我收到以下错误:

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSPlaceholderMutableString initWithString:]: nil argument”

假设我的 Storyboard xib 已正确加载,我需要做哪些不同的事情?这是 viewDidLoad:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add subviews

    self.view.backgroundColor = [UIColor clearColor];
    self.view.opaque = YES;

    [UIView beginAnimations:nil context:nil];
    [self.view setFrame:CGRectMake(0, 480, 320, 480)];

    [UIView setAnimationDuration:0.75f];
    [self.view setFrame:CGRectMake(0, 0, 320, 480)];
    [UIView commitAnimations];

    // Sort stat bars top to bottom (HP == 0):
    statBars = [statBars
                sortedArrayUsingComparator:^NSComparisonResult(id label1, id label2)
                {
                    if ([label1 frame].origin.y < [label2 frame].origin.y)
                        return NSOrderedAscending;
                    else
                        return NSOrderedDescending;
                }];

    // Sort types left to right:
    types = [types
             sortedArrayUsingComparator:^NSComparisonResult(id label1, id label2)
             {
                 if ([label1 frame].origin.x < [label2 frame].origin.x)
                     return NSOrderedAscending;
                 else
                     return NSOrderedDescending;
             }];

    [self displayInfo:currentIndex];
}

感谢您的帮助!

最佳答案

没关系,原来错误是在 [self displayInfo:currentIndex] ViewDidLoad 的最后一行。我错误地认为在实例化 ViewController 时安全地调用了 viewDidLoad。当我调用 self.view 时,它会在很久以后调用。

<_< 延迟初始化,对吧?

关于iphone - 访问 UIViewController 时的 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512212/

相关文章:

iphone - 我怎样才能获得第二个mov播放时间

ios - 一个新的 TableView 位于另一个 TableView 之后

ios - 带有 MagicalRecord 的核心数据不会保存到 iCloud

xcode - 断点在错误的行

iphone - 与 NSString 相关的问题

ios - 相机参数 - iPhone 7

iphone - GuitarToolkit 如何在后台显示持久的事件横幅?

ios - 是否可以将第三方 Objective-C 代码导入 Swift Playground ?

iOS Beta Testflight 构建在 24 小时后仍在进行中

ios - React Native - iOS - 无效的 `Podfile` 文件 : undefined method `[]' for nil:NilClass