[英]Way to load portrait and Landscape nib on orientation change?
有没有办法指定笔尖名称,以便应用程序在方向发生变化时自动为风景或肖像拾取相应的笔尖。与iPhone 4和iPhone 5一样,Default @ 2x.png和Default-568h@2x.png类似那是不是?,我正在尝试在App委托方法中使用addObserver进行方向更改,并且工作正常,但是在directionChanged方法中更改笔尖时导致应用崩溃并产生如下错误:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FRAppDelegate 0xa18bf20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key __mapView.'
我的代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[FRViewController alloc] initWithNibName:@"FRViewController" bundle:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
{
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewController" owner:self options:nil];
[self.viewController setView:[nibArray objectAtIndex:0]];
NSLog(@"device orientation comes to portrait mode ");
/* start special animation */
}
break;
case UIDeviceOrientationLandscapeRight:
case UIDeviceOrientationLandscapeLeft:
{
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewControllerLand" owner:self options:nil];
[self.viewController setView:[nibArray objectAtIndex:0]];
NSLog(@"device orientatino comes to Landscape mode");
/* start special animation */
}
break;
default:
break;
};
}
如果您确实想加载其他笔尖,则可以使用:
[[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone" owner:self options:nil]; NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"PageView_iPhone" owner:self options:nil]; PageView_iPhone* myView = [nibViews objectAtIndex:0];
然后在某些地方将视图换成新视图
喜欢
self.view = myView;
在初始化过程中,我设置了我的视图(以编程方式创建它们),并在视图控制器中为每个视图创建了额外的CGPoint用于中心位置或CGRect用于框架。
在init中创建视图后,我调用名为layoutViewsForOrientation的方法来设置初始位置,并传递当前状态栏的方向。 当方向改变时,我使用以下代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { BOOL result = NO; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { result = (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { result = YES; } if( result == YES ) [self layoutViewsForOrientation:interfaceOrientation]; return result; }
尽管这不是加载笔尖的直接解决方案,但我认为您遇到了该错误,因为第二个笔尖没有正确设置UI对象的出口(即__mapView
)。 即,无论您从xib连接__mapView到哪里,该类中都不再存在IBOutlet。
更新:之所以发生这种情况,是因为您正在加载将FRAppDelegate
保留为owner
的笔尖(您将self
指定为owner
。)如果将其更改为self.viewController
,则异常应消失。
即; 代替 :
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewController" owner:self options:nil];
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewControllerLand" owner:self options:nil];
采用
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewController" owner:self.viewController options:nil];
NSArray *nibArray=[[NSBundle mainBundle] loadNibNamed:@"FRViewControllerLand" owner:self.viewController options:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.