首页 文章

显示和关闭模态视图控制器

提问于
浏览
70

任何人都可以给我示例代码,我可以用来首先提出一个模态视图控制器,然后解雇它?这就是我一直在尝试的:

NSLog(@"%@", blue.modalViewController);
    [blue presentModalViewController:red animated:YES];
    NSLog(@"%@", blue.modalViewController);
    [blue dismissModalViewControllerAnimated:YES];
    NSLog(@"%@", blue.modalViewController);

此代码位于viewDidLoad中("blue"和"red"都是UIViewController的子类) . 我希望我会显示红色视图然后立即隐藏它,并带有一些动画 . 但是这段代码只提供了模态视图,并没有忽略它 . 任何的想法?第一个日志显示"null",而另外两个日志显示<RedViewController:0x3d21bf0>
另一点是,如果我将此代码放在applicationDidFinishLaunching中:红色视图根本不显示,并且所有日志都得到"null"

6 回答

  • 13

    迅速

    self.dismissViewControllerAnimated(true, completion: nil)

  • 13

    首先,当您将该代码放在applicationDidFinishLaunching中时,可能是从Interface Builder实例化的控制器尚未链接到您的应用程序(因此"red"和"blue"仍然是 nil ) .

    但要回答你的初步问题,你在错误的控制器上调用了 dismissModalViewControllerAnimated: !它应该是这样的:

    [blue presentModalViewController:red animated:YES];
    [red dismissModalViewControllerAnimated:YES];
    

    通常"red"控制器应该决定在某个时候解雇自己(可能是在单击"cancel"按钮时) . 然后"red"控制器可以在 self 上调用该方法:

    [self dismissModalViewControllerAnimated:YES];
    

    如果它仍然不起作用,它可能与控制器以动画方式呈现的事实有关,因此可能不允许在呈现控制器之后很快解除控制器 .

  • 105

    我在xcode 4.52中最简单的方法是创建一个额外的视图并使用segue模式连接它们(控制按钮从视图一拖到第二个视图,选择模态) . 然后将按钮拖动到第二个视图或您创建的模态视图 . 控制并将此按钮拖动到头文件并使用操作连接 . 这将在您的controller.m文件中创建一个IBaction . 在代码中找到您的按钮操作类型 .

    [self dismissViewControllerAnimated:YES completion:nil];
    
  • 9

    斯威夫特

    针对Swift 3进行了更新

    enter image description here

    故事板

    创建两个视图控制器,每个控制器上都有一个按钮对于第二个视图控制器,将类名设置为 SecondViewController ,将故事板ID设置为 secondVC .

    代码

    ViewController.swift

    import UIKit
    class ViewController: UIViewController {
    
        @IBAction func presentButtonTapped(_ sender: UIButton) {
    
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let myModalViewController = storyboard.instantiateViewController(withIdentifier: "secondVC")
            myModalViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
            myModalViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
            self.present(myModalViewController, animated: true, completion: nil)
        }
    }
    

    SecondViewController.swift

    import UIKit
    class SecondViewController: UIViewController {
    
        @IBAction func dismissButtonTapped(_ sender: UIButton) {
            self.dismiss(animated: true, completion: nil)
        }
    }
    

    资源:

  • 3

    presentModalViewController:

    MainViewController *mainViewController=[[MainViewController alloc]init];
    [self.navigationController presentModalViewController:mainViewController animated:YES];
    

    dismissModalViewController:

    [self dismissModalViewControllerAnimated:YES];
    
  • 2

    最简单的方法是使用Storyboard和Segue .

    只需从TabBarController的FirstViewController(而不是导航控制器)创建Segue到具有登录UI的LoginViewController,并将其命名为“showLogin” .

    创建一个返回BOOL的方法,以验证用户是否登录和/或他/她的会话是否有效...最好是在AppDelegate上 . 称之为isSessionValid .

    在您的FirstViewController.m上重写方法viewDidAppear,如下所示:

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        if([self isSessionValid]==NO){
            [self performSegueWithIdentifier:@"showLogin" sender:self];
        }
    }
    

    然后,如果用户成功登录,只需关闭或弹出LoginViewController以显示您的选项卡 .

    100%工作!

    希望能帮助到你!

相关问题