簡體   English   中英

在模態選擇(ViewController)中顯示UINavigationController?

[英]Showing UINavigationController in a Modal segue (ViewController)?

我已經在這個問題上停留了2個多星期! 在我的項目中,我有1個要同時啟用橫向和縱向模式的ViewController(幻燈片)。所有其他控制器/視圖(幻燈片)的其余部分我僅想啟用縱向模式。

棘手的部分是,我所指的“ ViewController”同時連接到NavigationControllers和TabBarControllers。 請參閱下面的方案,其中要同時啟用橫向/縱向的ViewController的名稱為: ReferredViewController。

TabBarController ----> NavigationController ----> FristViewController-(推送事件)-> ReferredViewController

到目前為止,我試圖讓一個類別兩個NavigationControllers和TabBarControllers。 但是由於我的NavigationControllers和TabBarControllers放置在項目的最開始,所以這將為整個項目設置規則。 我的ReferredViewController放置在項目“ storyboard”的末尾或中間。 我試圖通過代碼為單個ReferredViewController設置規則,但沒有成功。

我最好的辦法是將FirstViewController和ReferredViewController之間的事件從“ push”更改為“ modal”。 然后,ReferredViewController可以同時旋轉人像/風景,並且項目的其余部分被鎖定為人像。 但是,您可能知道,所有導航(NavigationBar)都將丟失,並且用戶將被卡在該單張幻燈片上。

因此,我嘗試在ReferredViewController.m文件中使用以下代碼示例啟用NavigationBar

ShowTaskViewController *detailViewController = [[ShowTaskViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]     initWithRootViewController:detailViewController];

navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentModalViewController:navController animated:YES  completion:nil];
[navController release];
[detailViewController release];

但是辦公室什么也沒有發生,我又回到正題:O。 FML!

在這一行:

[self.navigationController presentModalViewController:navController 
                                             animated:YES  
                                           completion:nil];

您正在混淆兩個UIViewController實例方法:

- (void)presentViewController:(UIViewController *)viewControllerToPresent 
                     animated:(BOOL)flag 
                   completion:(void (^)(void))completion

- (void)presentModalViewController:(UIViewController *)modalViewController 
                          animated:(BOOL)animated

第一種方法現已成為標准,第二種方法已在ios6中棄用。

呈現的視圖控制器也應該是self(ReferredViewController),而不是self的navigationController。

您呈現的視圖控制器可以自行關閉

 [[self presentingViewController] dismissViewControllerAnimated:YES 
                                                     completion:(void (^)(void))completion];

但是,看看fibnochi的答案,這可能是您達到目標的更好方法。

您必須克服UITaBarController,因為它是基礎視圖控制器。 我已經為我的導航控制器完成了此操作。 告訴我這是否有幫助。

@interface UINavigationController (Autorotation)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
- (BOOL) shouldAutorotate;
- (NSUInteger) supportedInterfaceOrientations;
@end

@implementation UINavigationController (Autorotation)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

if ([self.visibleViewController isKindOfClass:[MWPhotoBrowser class]] || [self.visibleViewController isKindOfClass:[ZoomPictureViewController class]]) {
    return YES;
}
return  (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL) shouldAutorotate{
    return YES;
}

-(NSUInteger) supportedInterfaceOrientations{

    if ([self.visibleViewController isKindOfClass:[MWPhotoBrowser class]] ||     [self.visibleViewController isKindOfClass:[ZoomPictureViewController class]]) {
    return UIInterfaceOrientationMaskAll;
}

return UIInterfaceOrientationMaskPortrait;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM