我习惯于从storyboard文件或xib文件加载ViewControllers,所以当我第一次尝试在代码中加载一个时:
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参数'
假设我的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];
}
感谢您的帮助!
答案 0 :(得分:1)
没关系,事实证明错误发生在[self displayInfo:currentIndex]中ViewDidLoad中的最后一行。我错误地认为在实例化ViewController时安全地调用了viewDidLoad。当我打电话给self.view时,很久以后就会调用它。
&LT; _&LT;懒惰的初始化,呵呵?
答案 1 :(得分:0)
检查您的ProfileViewController
xib,您必须将某个错误的插座连接到文件所有者