[英]iOS - Change View with the device orientation
我正在使用iOS5和Storyboard。 我有两个带有两个不同标识符的视图。 我想在更改设备方向时更改视图。 这是我的代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Landscape"];
}
else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Portrait"];
}
return YES;
}
但是应用崩溃了。 这是什么问题?
编辑:如果我添加两个uiview,并且在旋转设备时将两个id放到uiview中,则会崩溃。 这是代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) orientationChanged:(id)object
{
portraitView = [self.storyboard instantiateViewControllerWithIdentifier:@"TermoregolatoreTop"];
landscapeView = [self.storyboard instantiateViewControllerWithIdentifier:@"Landscape"];
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
else
{
self.view = self.landscapeView;
}
}
self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Portrait"];
将返回一个id
到UIViewController
并且您将其分配给UIView
。
释放视图控制器时,必须在UIDeviceOrientationDidChangeNotification
上删除观察者。
发生崩溃是因为该通知是在很可能已从内存中删除的类上触发的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.