ios 呈现和关闭模态视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1529632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:30:19  来源:igfitidea点击:

Present and dismiss modal view controller

iosobjective-cuiviewcontrollermodal-view

提问by phunehehe

Can anyone give me the example code that I can use to first present a modal view controller, then dismiss it? This is what I have been trying:

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

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

This code is in viewDidLoad ("blue" and "red" are both subclasses of UIViewController). I expect that I will show the red view and then immediately hide it, with some animation. However this piece of code only presents the modal view and does not dismiss it. Any idea? The first log shows "null" while the two other logs show <RedViewController: 0x3d21bf0>

这段代码在 viewDidLoad 中(“blue”和“red”都是 UIViewController 的子类)。我希望我会显示红色视图,然后立即隐藏它,并带有一些动画。然而,这段代码只显示了模态视图,并没有关闭它。任何的想法?第一个日志显示“空”,而其他两个日志显示<RedViewController: 0x3d21bf0>

Another point is, if I put this code in applicationDidFinishLaunching: the red view does not appear at all, and all logs get "null"

还有一点是,如果我把这段代码放在 applicationDidFinishLaunching 中:红色视图根本不出现,所有日志都得到“空”

回答by Tom van Zummeren

First of all, when you put that code in applicationDidFinishLaunching, it might be the case that controllers instantiated from Interface Builder are not yet linked to your application (so "red" and "blue" are still nil).

首先,当您将该代码放入 applicationDidFinishLaunching 时,可能会出现从 Interface Builder 实例化的控制器尚未链接到您的应用程序的情况(因此“红色”和“蓝色”仍然是nil)。

But to answer your initial question, what you're doing wrong is that you're calling dismissModalViewControllerAnimated:on the wrong controller! It should be like this:

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

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

Usually the "red" controller should decide to dismiss himself at some point (maybe when a "cancel" button is clicked). Then the "red" controller could call the method on self:

通常,“红色”控制器应该决定在某个时候(可能是单击“取消”按钮时)自行解散。然后“红色”控制器可以调用方法self

[self dismissModalViewControllerAnimated:YES];

If it still doesn't work, it might have something to do with the fact that the controller is presented in an animation fashion, so you might not be allowed to dismiss the controller so soon after presenting it.

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

回答by Suragch

Swift

迅速

Updated for Swift 3

为 Swift 3 更新

enter image description here

在此处输入图片说明

Storyboard

故事板

Create two View Controllers with a button on each. For the second view controller, set the class name to SecondViewControllerand the storyboard ID to secondVC.

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

Code

代码

ViewController.swift

视图控制器.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

SecondViewController.swift

import UIKit
class SecondViewController: UIViewController {

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

Source:

来源:

回答by max

The easiest way i tired in xcode 4.52 was to create an additional view and connect them by using segue modal(control drag the button from view one to the second view, chose Modal). Then drag in a button to second view or the modal view that you created. Control and drag this button to the header file and use action connection. This will create an IBaction in your controller.m file. Find your button action type in the code.

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

[self dismissViewControllerAnimated:YES completion:nil];

回答by Jerry Thomsan

presentModalViewController:

presentModalViewController:

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

dismissModalViewController:

解雇ModalViewController:

[self dismissModalViewControllerAnimated:YES];

回答by Oscar Salguero

The easiest way to do it is using Storyboard and a Segue.

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

Just create a Segue from the FirstViewController (not the Navigation Controller) of your TabBarController to a LoginViewController with the login UI and name it "showLogin".

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

Create a method that returns a BOOL to validate if the user logged in and/or his/her session is valid... preferably on the AppDelegate. Call it isSessionValid.

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

On your FirstViewController.m override the method viewDidAppear as follows:

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

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if([self isSessionValid]==NO){
        [self performSegueWithIdentifier:@"showLogin" sender:self];
    }
}

Then if the user logged in successfully, just dismiss or pop-out the LoginViewController to show your tabs.

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

Works 100%!

100% 有效!

Hope it helps!

希望能帮助到你!

回答by Michael

Swift

迅速

self.dismissViewControllerAnimated(true, completion: nil)

self.dismissViewControllerAnimated(true, completion: nil)