谁能给我一个示例代码,我可以用它来首先呈现模式视图控制器,然后关闭它?这就是我一直在尝试的:

    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”

有帮助吗?

解决方案

首先,当你把的applicationDidFinishLaunching的代码,它可能是从Interface Builder中实例化控制器还没有连接到您的应用程序的情况下(这样“红”“蓝”仍然nil)。

但是,为了回答你最初的问题,你在做什么错的是你错误的控制器上调用dismissModalViewControllerAnimated:!这应该是这样的:

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

通常,“红色”控制器应该决定在某一时刻(被点击“取消”按钮时也许)以关闭自己。然后,“红”控制器可以调用该方法上self

[self dismissModalViewControllerAnimated:YES];

如果它仍然不能正常工作,它可能是与该控制器在动画的方式呈现事实,所以你可能不会被允许提出它后不久解散控制器。

其他提示

在最简单的方法我累在xcode的4.52是创建一个附加视图,并通过使用模态赛格瑞连接它们(从控制视图中的一个拖动按钮第二视图,选择了模态)。 然后,在一个按钮第二视图或您所创建的模式视图中拖动。控制并拖动该按钮可将头文件,并用行动连接。这会在你的Controller.m或者文件的IBAction为。在代码中找到你的按钮动作类型。

[self dismissViewControllerAnimated:YES completion:nil];

迅速

更新为 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)
    }
}

来源:

presentModalViewController:

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

dismissModalViewController:

[self dismissModalViewControllerAnimated:YES];

夫特

self.dismissViewControllerAnimated(true, completion: nil)

的最简单方法是使用故事板和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%!

希望它能帮助!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top