Programmatically navigate to another view controller/scene

后端 未结 19 3187
一向
一向 2020-11-27 11:29

I got an error message during navigating from first view controller to second view controller. My coding is like this one

let vc = LoginViewController(nibNam         


        
19条回答
  •  北海茫月
    2020-11-27 12:19

    I Give you my code to make a transition. In this example the action is connecting to an UIButton. So don't forget to set it. Don't forget to set the name of your ViewController in the transition method.

    Don't forget to set your storyboard too. Your need to have one view per viewController. Connect each ViewController to each view in storyBoard. You can see on the screenshoot bellow

    enter image description here enter image description here

    class PresentationViewController: UIViewController {
        override func viewDidLoad() {
             super.viewDidLoad()
    
             var playButton   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    
             let image = UIImage(named: "YourPlayButton") as UIImage?
    
             playButton.frame = CGRectMake(0, 0, 100, 100)
             playButton.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)
             playButton.addTarget(self, action: "transition:", forControlEvents:  UIControlEvents.TouchUpInside)
             playButton.setBackgroundImage(image, forState: UIControlState.Normal)
    
             self.view.addSubview(playButton)
        }
    
    
    func transition(sender:UIButton!)
    {
        println("transition")
        let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("YourSecondViewController") as UIViewController
    
        let window = UIApplication.sharedApplication().windows[0] as UIWindow
        UIView.transitionFromView(
            window.rootViewController!.view,
            toView: secondViewController.view,
            duration: 0.65,
            options: .TransitionCrossDissolve,
            completion: {
                finished in window.rootViewController = secondViewController
        })
    }
    }
    

提交回复
热议问题