Programmatically navigate to another view controller/scene

后端 未结 19 3017
一向
一向 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 11:59

    You can move from one scene to another programmatically using below code :

      let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    
      let objSomeViewController = storyBoard.instantiateViewControllerWithIdentifier(“storyboardID”) as! SomeViewController
    
      // If you want to push to new ViewController then use this
      self.navigationController?.pushViewController(objSomeViewController, animated: true)
    
      // ---- OR ----
    
      // If you want to present the new ViewController then use this
      self.presentViewController(objSomeViewController, animated:true, completion:nil) 
    

    Here storyBoardID is value that you set to scene using Interface Builder. This is shown below :

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

    I'm not sure try below steps,i think may be error occurs below reasons.

    • you rename some files outside XCode. To solve it remove the files from your project and re-import the files in your project.
    • check and add missing Nib file in Build phases->Copy bundle resources.finally check the nib name spelling, it's correct, case sensitive.
    • check the properties of the .xib/storyboard files in the file inspector ,the property "Target Membership" pitch on the select box,then your xib/storyboard file was linked with your target.
    • such as incorrect type of NIB.Right click on the file and click "Get Info" to verify that the type is what you would expect.
    0 讨论(0)
  • 2020-11-27 12:04

    If you are building UI without drag and drop (without using storyboard) and want to navigate default page or ViewController.swift to another page? Follow these steps 1) add a class (.swift) 2) Import UIKit 3) Declare class name like

    class DemoNavigationClass :UIViewController{
         override func viewDidLoad(){
             let lbl_Hello = UILabel(frame: CGRect(x:self.view.frame.width/3, y:self.view.frame.height/2, 200, 30));
    lbl_Hello.text = "You are on Next Page"
    lbl_Hello.textColor = UIColor.white
    self.view.addSubview(lbl_Hello)
        }
    }
    

    after creating second page come back to first page (ViewController.swift) Here make a button in viewDidLoad method

    let button = UIButton()
    button.frame = (frame: CGRect(x: self.view.frame.width/3, y: self.view.frame.height/1.5,   width: 200, height: 50))
     button.backgroundColor = UIColor.red
     button.setTitle("Go to Next ", for: .normal)
     button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
     self.view.addSubview(button)
    

    now define buttonAction method outside of viewDidLoad() in same class

    func buttonAction(sender: UIButton!) 
    {
        let obj : DemoNavigationClass = DemoNavigationClass();
        self.navigationController?.pushViewController(obj, animated: true)
    }
    

    Keep one thing that i forget in main.storyboard there is a scene with a arrow, select that arrow and press delete button

    now drag and drop a navigationcontroller and delete tableview which comes with navaigation controller. select navigationcontroller press control on keyboard and drag it in to another scene on storyboard that is ViewController. This mean that your viewcontroller become root viewcontroller hope this help you Thanks in main.storyboard , drag and drop navigationcontroller,

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

    In addition to the good answers above to set the navigation view controller on top of your screen on your app, you can add it to your AppDelegate.swift file inside the block as follows

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        window = UIWindow()
        window?.makeKeyAndVisible()
        window?.rootViewController = UINavigationController(rootViewController: LoginViewController())
    
        return true
    
    }
    
    0 讨论(0)
  • 2020-11-27 12:06

    You can do navigation between view controllers using code with the help of storyboard and storyboardId of the view controller. This code is for Swift 3:

    let signUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
    self.navigationController?.pushViewController(signUpVC, animated: true)
    
    0 讨论(0)
  • 2020-11-27 12:06
     let vc = DetailUserViewController()
     vc.userdetails = userViewModels[indexPath.row]
     self.navigationController?.pushViewController(vc, animated: true)
    
    0 讨论(0)
提交回复
热议问题