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

发布于 2024-08-07 03:10:17 字数 588 浏览 5 评论 0原文

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

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

此代码位于 viewDidLoad 中(“蓝色”和“红色”都是 UIViewController 的子类)。我希望我会显示红色视图,然后立即隐藏它,并带有一些动画。然而,这段代码仅呈现模态视图,并没有关闭它。有什么想法吗?第一个日志显示“null”,而另外两个日志显示

另一点是,如果我将此代码放入 applicationDidFinishLaunching: 红色视图根本不会出现,并且所有日志得到“空”

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你另情深 2024-08-14 03:10:17

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

但要回答您最初的问题,您做错的事情是您在错误的控制器上调用 dismissModalViewControllerAnimated: !它应该是这样的:

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

通常“红色”控制器应该决定在某个时刻解雇自己(也许当单击“取消”按钮时)。然后“红色”控制器可以调用 self 上的方法:

[self dismissModalViewControllerAnimated:YES];

如果仍然不起作用,则可能与控制器以动画方式呈现有关,因此您可以不允许在呈现控制器后这么快就将其解散。

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).

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:

[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 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.

铁轨上的流浪者 2024-08-14 03:10:17

Swift

已更新为 Swift 3

在此处输入图像描述

故事板

创建两个视图控制器,每个控制器上都有一个按钮。对于第二个视图控制器,将类名设置为 SecondViewController,将 Storyboard 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)
    }
}

来源:

Swift

Updated for 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 SecondViewController and the storyboard ID to secondVC.

Code

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)
    }
}

Source:

苏辞 2024-08-14 03:10:17

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

[self dismissViewControllerAnimated:YES completion:nil];

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.

[self dismissViewControllerAnimated:YES completion:nil];
梦里人 2024-08-14 03:10:17

PresentModalViewController:

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

解雇ModalViewController:

[self dismissModalViewControllerAnimated:YES];

presentModalViewController:

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

dismissModalViewController:

[self dismissModalViewControllerAnimated:YES];
三五鸿雁 2024-08-14 03:10:17

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

只需创建一个从 TabBarController 的 FirstViewController(不是导航控制器)到具有登录 UI 的 LoginViewController 的 Segue,并将其命名为“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%有效!

希望有帮助!

The easiest way to do it is using Storyboard and a 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".

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.

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

- (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.

Works 100%!

Hope it helps!

蓝眼睛不忧郁 2024-08-14 03:10:17

Swift

self.dismissViewControllerAnimated(true,完成:nil)

Swift

self.dismissViewControllerAnimated(true, completion: nil)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文