【问题标题】:dismiss view controller before presenting another one在呈现另一个视图控制器之前关闭视图控制器
【发布时间】:2016-11-20 01:41:11
【问题描述】:

我正在使用 MZFormSheetPresentationController 显示一个对话框,要求用户输入密码,如下图所示: 用户点击OK后会出现另一个视图控制器,如下图所示: 但是在关闭新的视图控制器后,旧的视图控制器仍然存在在我的情况下应用这样的答案我的代码我正在使用什么:

- (IBAction)okButtonTouchDown:(id)sender {

   // UIStoryboard *storyboard = self.storyboard;
  //  UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"myprofile"];
   // [self presentViewController:nav animated:YES completion:^{

       // [self dismissViewControllerAnimated:YES completion:nil]; // here the self point to the new view controller not to the old one //




//[self dismissViewControllerAnimated:YES completion:^{


      //  UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"myprofile"];
     //   [self presentViewController:nav animated:YES completion:^{



        //}];


    // }];


   //}];



    __weak EditProfileViewController *aBlockSelf = self;
UIStoryboard *storyboard = self.storyboard;
UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"myprofile"];
 [self   presentViewController:nav animated:YES completion:^{
    [[aBlockSelf presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    }];

  }


// i try this solution but the new view controller presented and after some time dismissed //

即使我尝试在完成后关闭viewcontroller,我也会遇到此错误警告:尝试在其视图上显示不在窗口层次结构中的视图!

那么有没有办法不使用委托和协议方法来解决这个问题?

【问题讨论】:

  • 你应该先解散,然后再展示新的 VC
  • @shubhank 我已经这样做了,但我遇到了不在窗口层次结构中!错误信息
  • 好吧,自从 VC 被解散后,你的内部块将有 self nil。所以你需要在这里使用委托并将VC呈现在父级中。也无需在 cmets 中大喊大叫!
  • 你能有一个简单的教程如何使用委托协议,因为我找不到一些简单的
  • 理想情况下,您应该关闭 sheetPresentation 视图,然后只有您应该加载新视图....这是根据视图层次结构执行的确切方法...

标签: ios objective-c iphone


【解决方案1】:

将此作为扩展添加到您的 AppDelegate

extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
        return topViewController(nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        if let selected = tab.selectedViewController {
            return topViewController(selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(presented)
    }
    return base
}

}

然后这样做

self.dismissViewControllerAnimated(true, completion: {
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("yourIdentifierHere")
        UIApplication.topViewController()?.presentViewController(vc!, animated: true, completion: nil)
    })

目标-C

- (UIViewController *)topViewController{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController {
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController;
        return [self topViewController:[navigationController.viewControllers lastObject]];   
    }
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabController = (UITabBarController *)rootViewController;
        return [self topViewController:tabController.selectedViewController];
    }
    if (rootViewController.presentedViewController) {
        return [self topViewController:rootViewController];
    }
    return rootViewController;
}

【讨论】:

  • 我已经这样做了,但即使在日志中也没有任何反应 _ [[self presentingViewController]presentViewController:nav animated:YES completion:nil]; __
  • 再次更新了我的答案,我为此创建了一个示例项目并且它有效:)
  • 你能在对象 c 中而不是在 swift 中制作它吗
  • 完成,再次检查
  • 您不需要为此扩展。你只是把他弄糊涂了。
猜你喜欢
  • 2017-02-03
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多