切换旋转视图时,iOS 应用程序在打开时崩溃
Posted
技术标签:
【中文标题】切换旋转视图时,iOS 应用程序在打开时崩溃【英文标题】:iOS app crashes upon opening when switching rotation views 【发布时间】:2011-01-21 23:31:22 【问题描述】:我正在尝试在 XCode 中开发一个应用程序,该应用程序将在旋转时切换到新视图。
这是view1controller.h
的代码:
#import UIKit/UIKit.h
@interface TestViewController : UIViewController
IBOutlet UIView *portrait;
IBOutlet UIView *landscape;
@property(nonatomic,retain) UIView *portrait;
@property(nonatomic,retain) UIView *landscape;
@end
这是view1controller.m
的代码:
#import "view1controller.h"
@implementation TestViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)))
self.view = landscape;
else if (((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)))
self.view = portrait;
return YES;
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
- (void)viewDidUnload
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[super dealloc];
@synthesize portrait,landscape;
@end
无论如何,应用程序打开但打开时崩溃。
【问题讨论】:
【参考方案1】:尝试类似:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
self.view = landscapeleft;
if (interfaceOrientation == UIInterfaceOrientationPortrait)
self.view = portrait;
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
self.view = portraitupsidedown;
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
self.view = landscapeleft;
return YES;
【讨论】:
最重要的是我不必创建 3 个 .xib 文件。多合一:)【参考方案2】:标题中的括号放错了位置。
@interface TestViewController : UIViewController
IBOutlet UIView *portrait;
IBOutlet UIView *landscape;
@property(nonatomic,retain) UIView *portrait;
@property(nonatomic,retain) UIView *landscape;
@end
另外,@synthesize portrait,landscape;
需要隶属于@implementation TestViewController
@implementation TestViewController
@synthesize portrait,landscape;
注意这张图片中的 UIView:
【讨论】:
嗯。它似乎仍然在启动时崩溃:/ 您是否在 Interface Builder 中连接了它们?您需要在 IB 中创建 2 个单独的 UIView。 谢谢,我会尽可能多地留下向上箭头,但显然没有足够的代表。 另外,现在我已经完成了这项工作,我不知道这是否可能,但是否可以将另一个倒置的第三个纵向视图合并到其中? 用一个例子添加了另一个答案。【参考方案3】:我会做以下事情:
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)))
[portrait removeFromSuperview];
[self.view addSubview:landscape];
else if (((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)))
[landscape removeFromSuperview];
[self.view addSubview:portrait];
希望这会有所帮助!
【讨论】:
不需要调用removeFromSuperview。以上是关于切换旋转视图时,iOS 应用程序在打开时崩溃的主要内容,如果未能解决你的问题,请参考以下文章