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

8 浏览
0 Comments

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

有没有人可以给我一个示例代码,我可以用它首先展示一个模态视图控制器,然后将其关闭?我尝试过下面的代码:\n

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

\n这段代码在viewDidLoad中(\"blue\"和\"red\"都是UIViewController的子类)。我期望我将显示红色视图,然后立即隐藏它,带有一些动画。然而,这段代码只展示了模态视图,却没有关闭它。有什么建议吗?第一个日志显示为\"null\",而另外两个日志显示为\n另一个问题是,如果我将这段代码放在applicationDidFinishLaunching:中,红色视图根本不会出现,所有的日志都显示为\"null\"。

0
0 Comments

在Xcode 4.52中,我尝试了最简单的方法,即创建一个附加视图,并通过使用segue modal将它们连接起来(从第一个视图中的按钮控制拖到第二个视图,选择Modal)。

然后在第二个视图或模态视图中拖入一个按钮。控制并将此按钮拖到头文件中,并使用action连接。这将在controller.m文件中创建一个IBAction。在代码中找到您的按钮操作类型。

[self dismissViewControllerAnimated:YES completion:nil];

请注意,根据UIViewController文档,这将自动将消息转发给self.presentingViewController,所以为了清晰起见,最好调用[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

问题的原因是使用segue modal创建的模态视图控制器需要使用dismissViewControllerAnimated方法来关闭视图。解决方法是在按钮的IBAction中调用[self dismissViewControllerAnimated:YES completion:nil];[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];来关闭模态视图控制器。

0
0 Comments

在上述代码中,问题是如何在视图控制器之间显示和关闭模态视图控制器。

问题的原因是,在ViewController.swift文件中的presentButtonTapped方法中,我们通过使用storyboard实例创建了一个新的视图控制器实例,并将其呈现给用户。但是,我们没有为新视图控制器设置一个按钮或方法来关闭它。

解决方法是在SecondViewController.swift文件中添加一个dismissButtonTapped方法,该方法在按钮被点击时关闭模态视图控制器。在该方法中,我们使用self.dismiss方法将当前视图控制器关闭。

下面是修复问题的代码:

ViewController.swift

import UIKit

class ViewController: UIViewController {

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 {

func dismissButtonTapped(_ sender: UIButton) {

self.dismiss(animated: true, completion: nil)

}

}

以上修复方法将为第二个视图控制器添加一个按钮,并在按钮被点击时关闭模态视图控制器。

参考来源:

- [Documentation](https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html)

- [SO answer](https://stackoverflow.com/a/24036067/3681880)

0
0 Comments

出现问题的原因是在applicationDidFinishLaunching中调用了dismissModalViewControllerAnimated方法,但是可能由于控制器从Interface Builder实例化时还未与应用程序链接在一起,所以"red"和"blue"仍然是nil。

解决方法是调用dismissModalViewControllerAnimated方法的控制器错误。应该像这样:

[blue presentModalViewController:red animated:YES];

[red dismissModalViewControllerAnimated:YES];

通常,“red”控制器应该在某个时刻决定自己解除,可能是在点击“取消”按钮时。然后,“red”控制器可以在self上调用该方法:

[self dismissModalViewControllerAnimated:YES];

如果仍然无法解决问题,可能与控制器以动画方式呈现有关,因此在呈现后不允许立即解除控制器。

根据iPhone OS的View Controller编程指南,在解除模态视图控制器时,这种方法是不正确的,应该使用委托。因此,在呈现模态视图之前,将自己设置为委托,然后从模态视图控制器调用委托来解除。

需要记住的是,自从io6以来,这种呈现模态视图的方式已经过时了。请使用:[self presentViewController:animated:completion:];代替

不,View Controller编程指南并没有说这种方法是不正确的。它确实说,你提出的方法是“首选方法”。换句话说,虽然你的方法可能适用于许多情况,但这并不是唯一的方法,只是你应该首先尝试的方法。我建议你将你引用的方法作为附加答案添加,因为对于一些读者来说,你的方法可能更好。

[ self dismissModalViewControllerAnimated:YES]; 已弃用

在调用dismissModalViewControllerAnimated方法时,调用red还是blue有什么区别?从文档中看,似乎应该在blue(呈现VC)上调用...

0