2011-11-24 30 views
14

我有兩個UIViewController包含在navigatoin視圖控制器中,並且都處於橫向模式。我想在兩個uiviewcontroller之間切換,而不需要像推動一樣的動畫。因此,如果用戶在第一個視圖控制器中單擊按鈕,我會在這兩者之間執行自定義搜索。ios:風景模式下兩個視圖控制器之間的自定義segue

#import <Foundation/Foundation.h> 
#import "AppDelegate.h" 

@class AppDelegate; 

@interface NonAnimatedSegue : UIStoryboardSegue { 

} 

@property (nonatomic,assign) AppDelegate* appDelegate; 

@end 

這實現:

#import "NonAnimatedSegue.h" 

@implementation NonAnimatedSegue 

@synthesize appDelegate = _appDelegate; 

-(void) perform{ 
    self.appDelegate = [[UIApplication sharedApplication] delegate]; 
    UIViewController *srcViewController = (UIViewController *) self.sourceViewController; 
    UIViewController *destViewController = (UIViewController *) self.destinationViewController; 
[srcViewController.view removeFromSuperview]; 
[self.appDelegate.window addSubview:destViewController.view]; 
self.appDelegate.window.rootViewController=destViewController; 
} 

@end 

在我切換到定製賽格瑞,實際上它工作正常的腳本中。唯一的問題是第二個uiviewcontroller不是以橫向模式顯示,而是在protrait中顯示。如果我刪除自定義的segue並用push segue替換它,則一切正常,第二個viewcontroller以橫向模式顯示。

那麼,如果我使用自定義的segue,那麼第二個viewcontroller也處於橫向視圖中,我該怎麼做?

回答

14

上述代碼無法正常工作,因爲destinationViewController無法從UIInterfaceOrientation自行接收更新。它通過它的「Container View Controller」(導航控制器)接收這些更新。爲了使自定義的segue正常工作,我們需要通過導航控制器轉換到新的視圖。

-(void) perform{ 
    [[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO]; 
} 
+3

你是一個美麗的人。 –

1

你可以有目的地視圖控制器採取中心/從源頭控制(這已經是方向正確)變換/界限:

-(void) perform{ 
    self.appDelegate = [[UIApplication sharedApplication] delegate]; 
    UIViewController *src = (UIViewController *) self.sourceViewController; 
    UIViewController *dst = (UIViewController *) self.destinationViewController; 

// match orientation/position 
dst.view.center = src.view.center; 
dst.view.transform = src.view.transform; 
dst.view.bounds = src.view.bounds; 

[dst.view removeFromSuperview]; 
[self.appDelegate.window addSubview:dst.view]; 
self.appDelegate.window.rootViewController=dst; 
} 
相關問題