How to dismiss own view controller and present another view controller in a button tap?

前端 未结 4 814
耶瑟儿~
耶瑟儿~ 2020-12-15 12:47

Let\'s say I have 3 view controller labeled \"A\",\"B\" and \"C\". Right now, \"A\" is the rootViewController of the window and it presents \"B\" modally when a button is ta

4条回答
  •  無奈伤痛
    2020-12-15 12:48

    It seems that it is not possible to go from B to C without showing A briefly, which looks unprofessional. However, you can put a black subview over top of A until you've animated to C.

    In Swift 3:

    class A : UIViewController {
        ...
        func showB() {
            // Adding the black view before dismissing B does not work;
            // the view is not displayed.
            let black = UIView()
            black.backgroundColor = UIColor.black
            black.frame = self.view.bounds // assumes A is not zoomed
    
            let b = B()
            self.present(b, animated:true, completion: {
                self.view.addSubview(black)
            })
    
            // Note: self.present() will start the animation,
            // then b.imDone will be set.  It is done here for
            // clarity of what happens next, as if it were all
            // one function.
            b.imDone = {
                b.dismiss(animated:false, completion: {
                    self.present(C(), animated:true, completion: {
                        black?.removeFromSuperview()
                    })
                })
            }
        }
    }
    
    class B : UIViewController {
        var imDone : (() -> Void)?
        ...
        func f()
        {
            imDone?()
        }
        ...
    }
    
    class C : UIViewController
    {
        ...
    }
    

提交回复
热议问题