3 回答
TA贡献1876条经验 获得超6个赞
我认为您误解了有关iOS模式视图控制器的一些核心概念。当您关闭VC1时,任何由VC1呈现的视图控制器也将被关闭。Apple希望模式视图控制器以堆叠的方式流动-在您的情况下,VC2由VC1提供。一旦您从VC1提供VC2,就将VC1解雇,这简直是一团糟。为了实现所需的功能,在VC1退出后,buttonPressedFromVC1应该立即使mainVC出现在VC2中。我认为,没有代表就可以实现这一目标。大致情况:
UIViewController presentingVC = [self presentingViewController];
[self dismissViewControllerAnimated:YES completion:
^{
[presentingVC presentViewController:vc2 animated:YES completion:nil];
}];
注意self.presentingViewController存储在其他变量中,因为在vc1关闭自身之后,您不应对其进行任何引用。
TA贡献1794条经验 获得超7个赞
Swift中的示例,描绘了上述代工厂的说明和Apple的文档:
基于Apple的文档和上述铸造厂的说明(纠正了一些错误),使用委托设计模式的presentViewController版本:
ViewController.swift
import UIKit
protocol ViewControllerProtocol {
func dismissViewController1AndPresentViewController2()
}
class ViewController: UIViewController, ViewControllerProtocol {
@IBAction func goToViewController1BtnPressed(sender: UIButton) {
let vc1: ViewController1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1") as ViewController1
vc1.delegate = self
vc1.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.presentViewController(vc1, animated: true, completion: nil)
}
func dismissViewController1AndPresentViewController2() {
self.dismissViewControllerAnimated(false, completion: { () -> Void in
let vc2: ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as ViewController2
self.presentViewController(vc2, animated: true, completion: nil)
})
}
}
ViewController1.swift
import UIKit
class ViewController1: UIViewController {
var delegate: protocol<ViewControllerProtocol>!
@IBAction func goToViewController2(sender: UIButton) {
self.delegate.dismissViewController1AndPresentViewController2()
}
}
ViewController2.swift
import UIKit
class ViewController2: UIViewController {
}
基于上述代工厂的解释(更正了一些错误),使用委托设计模式的pushViewController版本:
ViewController.swift
import UIKit
protocol ViewControllerProtocol {
func popViewController1AndPushViewController2()
}
class ViewController: UIViewController, ViewControllerProtocol {
@IBAction func goToViewController1BtnPressed(sender: UIButton) {
let vc1: ViewController1 = self.storyboard?.instantiateViewControllerWithIdentifier("VC1") as ViewController1
vc1.delegate = self
self.navigationController?.pushViewController(vc1, animated: true)
}
func popViewController1AndPushViewController2() {
self.navigationController?.popViewControllerAnimated(false)
let vc2: ViewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as ViewController2
self.navigationController?.pushViewController(vc2, animated: true)
}
}
ViewController1.swift
import UIKit
class ViewController1: UIViewController {
var delegate: protocol<ViewControllerProtocol>!
@IBAction func goToViewController2(sender: UIButton) {
self.delegate.popViewController1AndPushViewController2()
}
}
ViewController2.swift
import UIKit
class ViewController2: UIViewController {
}
- 3 回答
- 0 关注
- 576 浏览
添加回答
举报