2011-11-24 47 views
14

Tôi có hai UIViewControllers được chứa trong bộ điều khiển chế độ xem điều hướng và cả ở chế độ ngang. Tôi muốn chuyển đổi giữa hai uiviewcontroller mà không có một hình ảnh động như push. Vì vậy, tôi thực hiện một khoảng cách tùy chỉnh giữa hai người dùng này nếu người dùng nhấp vào nút xem điều khiển đầu tiên một nút.ios: Phân đoạn tùy chỉnh giữa hai chế độ xem trong chế độ ngang

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

@class AppDelegate; 

@interface NonAnimatedSegue : UIStoryboardSegue { 

} 

@property (nonatomic,assign) AppDelegate* appDelegate; 

@end 

Và đây thực hiện:

#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 

Trong kịch bản tôi chuyển sang segue tùy chỉnh và thực sự nó hoạt động tốt. Vấn đề duy nhất là uiviewcontroller thứ hai không được hiển thị ở chế độ nằm ngang mà là trong protrait. Nếu tôi loại bỏ các segue tùy chỉnh và thay thế nó bằng một segue đẩy sau đó tất cả mọi thứ hoạt động tốt, các viewcontroller thứ hai được hiển thị trong chế độ phong cảnh.

Vì vậy, tôi phải làm gì để chế độ xem thứ hai cũng ở chế độ xem ngang nếu tôi sử dụng phân đoạn tùy chỉnh của mình?

Trả lời

14

Đoạn mã trên không hoạt động vì destinationViewController không thể nhận cập nhật từ UIInterfaceOrientation trên chính nó. Nó nhận được những cập nhật thông qua nó là "Container View Controller" (Navigation Controller). Để có được segue tùy chỉnh hoạt động đúng, chúng ta cần chuyển sang chế độ xem mới thông qua Trình điều khiển Điều hướng.

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

Bạn là một con người đẹp. –

1

bạn có thể có quan điểm điều khiển đến tận trung tâm/chuyển đổi/vọt từ bộ điều khiển nguồn (nào đã được định hướng đúng):

-(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; 
} 
Các vấn đề liên quan