在Swift中关闭并呈现视图控制器

时间:2016-06-12 05:23:24

标签: ios swift uiviewcontroller

您好我试图提供一个viewcontroller并关闭我当前的模态视图,但此代码无法正常工作

self.dismissViewControllerAnimated(true, completion: {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
    self.presentViewController(vc!, animated: true, completion: nil)
})

反之亦然,在presentviewcontroller的完成块上也不起作用

编辑:替换vc!自我

7 个答案:

答案 0 :(得分:16)

你必须获得呈现self(当前ViewController)的viewController。如果该视图控制器是rootViewController,您可以使用如下,如果不是该查询它基于您查看控制器层次结构。

float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f; // Scale our height by a factor of 20

答案 1 :(得分:8)

你不能这样做,因为当UIViewController A调用UIViewController B并且第一个控制器被解除时,两个控制器都是nil。

你需要有一个UIViewController作为基础,在这种情况下,MainViewController是基础。您需要使用协议来调用控制器之间的导航。

你可以使用协议,比如说如下: -

进入你的viewController设置协议:

    protocol FirstViewControllerProtocol {
    func dismissViewController()
}

class FirstViewController: UIViewController {
    var delegate:FirstViewControllerProtocol!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func goBack(sender: AnyObject) {
        self.dismissViewControllerAnimated(true) { 
            self.delegate!.dismissViewController()
        }
    }

现在在主视图控制器中

class MainViewController: UIViewController, FirstViewControllerProtocol {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func goToFirstViewController(sender: AnyObject) {
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(FirstViewController)) as! FirstViewController
    viewController.delegate = self
    self.presentViewController(viewController, animated: true, completion: nil)
}



//MARK: Protocol
func dismissViewController() {
    if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
        self.presentViewController(viewController, animated: true, completion: nil)
    }
}

Code example with storyboard:

答案 2 :(得分:6)

我认为你的代码中有一个错误,那就是' self'应该是呈现视图控制器来呈现' vc'而不是' vc'它的自我

您的代码

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                vc!.presentViewController(vc!, animated: true, completion: nil)
            })

试试这个

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                self.presentViewController(vc!, animated: true, completion: nil)
            })

希望这有用

答案 3 :(得分:3)

 var hd = d3.keys(res[0]);

        for (var i = hd.length - 1; i >= 0; i--) {
            if( (hd[i] === "Station") || (hd[i] === "ID") || (hd[i] === "Elev") || (hd[i] === "Latitude")
                || (hd[i] === "Longitude") || (hd[i] === "County") || (hd[i] === "Nat_ID") || (hd[i] === "Year_Built")
                || (hd[i] === "Capacity")){
            }else{

            }
        }

答案 4 :(得分:1)

这是Swift3的解决方案

呈现ViewController

let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController

self.present(NotificationVC, animated: true, completion: nil)

关闭ViewController:

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

答案 5 :(得分:0)

if let vc = storyboard?.instantiateViewController(withIdentifier: "IdOfYourVC") {

    present(vc, animated: true, completion: nil)
}

答案 6 :(得分:0)

快速4和5

weak var presentingVC = self.presentingViewController

self.dismiss(animated: true, completion: {
    let vc = SecondController()
    presentingVC?.present(vc, animated: true, completion: nil)
})