iOS - 错过导航流程

时间:2016-04-26 12:57:51

标签: ios objective-c uinavigationcontroller

在我的应用程序中,我使用以下代码添加子视图控制器。

self.onlineUserList = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OnlineUserList"];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.onlineUserList];
self.onlineUserList.delegate = self;
navController.navigationBar.hidden = YES;

CGRect aRect = [[UIScreen mainScreen] bounds];

[navController.view setFrame:(CGRect){0, 0, aRect.size.width, aRect.size.height-47}];

[self addChildViewController:navController];
[self.view addSubview:navController.view];
[self didMoveToParentViewController:navController];

我正在使用以下代码删除子视图控制器。

[_onlineUserList removeFromParentViewController];
[_onlineUserList.view removeFromSuperview];
[_onlineUserList didMoveToParentViewController:nil];

运作良好。但删除子视图控制器后,我无法在父视图控制器上执行任何操作。我想我错过了一些导航流程。请帮帮我。

1 个答案:

答案 0 :(得分:1)

只需将所有Viewcontrollers放入数组中,检查一下。

如果您想要所有导航视图控制器,

NSArray *currentControllers = self.navigationController.viewControllers;

如果你想要第一个pushviewcontroller,

UIViewController *firstcontroller = self.navigationController.viewControllers.firstObject;

如果你想要最后一个对象,

UIViewController *Lastcontroller = self.navigationController.viewControllers.lastObject;

现在使用此代码进行跟踪,删除哪个viewcontroller。

或另一种方法是在didfinishlaunchwithoption中提供您的parentviewcontroller的标识符,例如

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    SlideMenu = [storyboard instantiateViewControllerWithIdentifier:@"SlideMenu"]; 

然后为topviewcontroller添加方法,

- (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil) {
        return rootViewController;
    }

    if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [self topViewController:lastViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
}

现在在你的appdelegate中创建另一个方法来呈现你的parentviewcontroller,如

- (void)ShowMenu{
    [SlideMenu ShowToViewController:[self topViewController]];
}

然后当你删除childviewcontroller然后调用

[[AppDelegate mainDelegate] ShowMenu];

我希望此信息足以解决您的问题。