[英]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.