Programmatically navigate to another view controller/scene

后端 未结 19 3015
一向
一向 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:09

    Swift3:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController("LoginViewController") as UIViewController
    self.navigationController?.pushViewController(vc, animated: true)
    

    Try this out. You just confused nib with storyboard representation.

    0 讨论(0)
  • 2020-11-27 12:11

    I already found the answer

    Swift 4

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
    self.present(nextViewController, animated:true, completion:nil)
    

    Swift 3

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    
    let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
    self.presentViewController(nextViewController, animated:true, completion:nil)
    
    0 讨论(0)
  • 2020-11-27 12:12

    XCODE 9.2 AND SWIFT 3.0

    ViewController to NextViewcontroller without Segue Connection

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
    self.navigationController?.pushViewController(nextViewController, animated:true)
    

    or

    let VC:NextViewController = storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
    self.navigationController?.pushViewController(VC, animated: true)
    
    0 讨论(0)
  • 2020-11-27 12:16

    Programmatically there are different ways based on different situations

    1. load storyenter code hereboard nib file

      let yourVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController;
      self.present(yourVc, animated: true, completion: nil)
      
    2. Load from xib

      let yourVc = YourViewController.init(nibName: "YourViewController", bundle: nil)
      self.present(yourVc, animated: true, completion: nil)
      
    3. Navigate through Segue

      self.performSegue(withIdentifier: "your UIView", sender: self)
      
    0 讨论(0)
  • 2020-11-27 12:18
    let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
    // self.present(signUpVC, animated: false, completion: nil)
    self.navigationController?.pushViewController(signUpVC, animated: true)
    
    0 讨论(0)
  • 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
        })
    }
    }
    
    0 讨论(0)
提交回复
热议问题