简体   繁体   English

在模态选择(ViewController)中显示UINavigationController?

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

I have now been stuck on this problem for more then 2 weeks! 我已经在这个问题上停留了2个多星期! In my project, I have 1 single ViewController(slide) I want to enable both landscape and portrait in. The rest of all controllers/views(slides) I want to enable portrait-mode only. 在我的项目中,我有1个要同时启用横向和纵向模式的ViewController(幻灯片)。所有其他控制器/视图(幻灯片)的其余部分我仅想启用纵向模式。

The tricky part is, the "ViewController" I am referring to is connected to both NavigationControllers and TabBarControllers. 棘手的部分是,我所指的“ ViewController”同时连接到NavigationControllers和TabBarControllers。 See the scheme below where the ViewController I want to enable both landscape/portrait is named: ReferredViewController. 请参阅下面的方案,其中要同时启用横向/纵向的ViewController的名称为: ReferredViewController。

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

So far I have tried to make a CATEGORY for both NavigationControllers and TabBarControllers. 到目前为止,我试图让一个类别两个NavigationControllers和TabBarControllers。 But since my NavigationControllers and TabBarControllers are placed at the very start of the project this will set the rules for the whole project. 但是由于我的NavigationControllers和TabBarControllers放置在项目的最开始,所以这将为整个项目设置规则。 My ReferredViewController is placed at the end or in the middle of the projects "storyboard". 我的ReferredViewController放置在项目“ storyboard”的末尾或中间。 I have tried to set the rules by code aswell for the single ReferredViewController without any success. 我试图通过代码为单个ReferredViewController设置规则,但没有成功。

My best shot is to change the event between FirstViewController and ReferredViewController from "push" to "modal". 我最好的办法是将FirstViewController和ReferredViewController之间的事件从“ push”更改为“ modal”。 ReferredViewController can then rotate both portrait/landscape and the rest of the project is locked in portrait. 然后,ReferredViewController可以同时旋转人像/风景,并且项目的其余部分被锁定为人像。 BUT, as you may know all navigations (NavigationBar) will be lost and the user will become stuck at that single slide. 但是,您可能知道,所有导航(NavigationBar)都将丢失,并且用户将被卡在该单张幻灯片上。

So I am trying to enable the NavigationBar with the following code example in the ReferredViewController.m file: 因此,我尝试在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];

But ofc nothing happens and I am back to square one again :O. 但是办公室什么也没有发生,我又回到正题:O。 FML! FML!

In this line: 在这一行:

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

you are conflating two UIViewController instance methods: 您正在混淆两个UIViewController实例方法:

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

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

The first of these is now the standard, the second method was deprecated in ios6. 第一种方法现已成为标准,第二种方法已在ios6中弃用。

Also the presenting view controller should be self (the ReferredViewController), not self's navigationController. 呈现的视图控制器也应该是self(ReferredViewController),而不是self的navigationController。

Your presented view controller can dismiss itself thus 您呈现的视图控制器可以自行关闭

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

But take a look at fibnochi's answer, it may be a better way for you to achieve your result. 但是,看看fibnochi的答案,这可能是您达到目标的更好方法。

you have to over ride UITaBarController because it is you base view controller. 您必须克服UITaBarController,因为它是基础视图控制器。 I have done this for my navigation controller. 我已经为我的导航控制器完成了此操作。 Tell me if this helped. 告诉我这是否有帮助。

@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.

相关问题 模态segue的viewcontroller中的uiscrollview - uiscrollview in viewcontroller in modal segue 将Modal UINavigationController展开到另一个ViewController - Unwind a Modal UINavigationController to another ViewController 如何以编程方式在UINavigationController中显示第二个模式ViewController - How to display second modal ViewController in UINavigationController programmatically UINavigationController 未显示在模态视图控制器上 - UINavigationController not showing on modal view controller Xcode 6中的“显示”segue将视图控件呈现为iOS 7中的模态 - 'Show' segue in Xcode 6 presents the viewcontroller as a modal in iOS 7 iOS Modal Segue 丢弃源 ViewController? - iOS Modal Segue drops source ViewController? iOS故事板:在同一个ViewController上使用模式和推送Segue - iOS storyboard: modal and push segue on the same viewcontroller 当呈现UINavigationController模态时,segue.destinationViewController为nil - segue.destinationViewController is nil when presenting UINavigationController modal 无法为模式segue的UINavigationController设置rootviewcontroller - Can't set rootviewcontroller for modal segue'd UINavigationController 如何使用自适应segue实现UINavigationController用于模态表示,而不是用于弹出窗口 - How to implement a UINavigationController for modal presentation, not for popover, with an adaptive segue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM