How to transition to a new view controller with code only using Swift

后端 未结 9 1515
半阙折子戏
半阙折子戏 2020-12-23 08:58

I want to transition from ViewController to secondViewController, when the user presses a UIButton, using code only in Swift.

//Function to transition
func          


        
相关标签:
9条回答
  • 2020-12-23 09:31

    Always use nibName file otherwise your preloaded content of Xib will not show .

    vc : ViewController =  ViewController(nibName: "ViewController", bundle: nil) //change this to your class name
    
     self.presentViewController(vc, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-23 09:33

    The problem is that your code is creating a blank UIViewController, not a SecondViewController. You need to create an instance of your subclass, not a UIViewController,

    func transition(Sender: UIButton!) {   
        let secondViewController:SecondViewController = SecondViewController()
    
        self.presentViewController(secondViewController, animated: true, completion: nil)
    
     }
    

    If you've overridden init(nibName nibName: String!,bundle nibBundle: NSBundle!) in your SecondViewController class, then you need to change the code to,

    let sec: SecondViewController = SecondViewController(nibName: nil, bundle: nil)
    
    0 讨论(0)
  • 2020-12-23 09:39

    SWIFT

    Usually for normal transition we use,

    let next:SecondViewController = SecondViewController()
    self.presentViewController(next, animated: true, completion: nil)
    

    But sometimes when using navigation controller, you might face a black screen. In that case, you need to use like,

    let next:ThirdViewController = storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController
    self.navigationController?.pushViewController(next, animated: true)
    

    Moreover none of the above solution preserves navigationbar when you call from storyboard or single xib to another xib. If you use nav bar and want to preserve it just like normal push, you have to use,

    Let's say, "MyViewController" is identifier for MyViewController

    let viewController = MyViewController(nibName: "MyViewController", bundle: nil)
    self.navigationController?.pushViewController(viewController, animated: true)
    
    0 讨论(0)
  • 2020-12-23 09:43

    For anyone doing this on iOS8, this is what I had to do:

    I have a swift class file titled SettingsView.swift and a .xib file named SettingsView.xib. I run this in MasterViewController.swift (or any view controller really to open a second view controller)

    @IBAction func openSettings(sender: AnyObject) {
            var mySettings: SettingsView = SettingsView(nibName: "SettingsView", bundle: nil) /<--- Notice this "nibName" 
            var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
            mySettings.modalTransitionStyle = modalStyle
            self.presentViewController(mySettings, animated: true, completion: nil)
    
        }
    
    0 讨论(0)
  • 2020-12-23 09:44

    Your code is just fine. The reason you're getting a black screen is because there's nothing on your second view controller.

    Try something like:

    secondViewController.view.backgroundColor = UIColor.redColor();
    

    Now the view controller it shows should be red.

    To actually do something with secondViewController, create a subclass of UIViewController and instead of

    let secondViewController:UIViewController = UIViewController()
    

    create an instance of your second view controller:

    //If using code
    let secondViewController = MyCustomViewController.alloc()
    
    //If using storyboard, assuming you have a view controller with storyboard ID "MyCustomViewController"
    let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("MyCustomViewController") as UIViewController
    
    0 讨论(0)
  • 2020-12-23 09:46

    Updated for Swift 3, some of these answers are a bit outdated.

    let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let vc : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "myStoryboardID") as UIViewController
            self.present(vc, animated: true, completion: nil)    }
    
    0 讨论(0)
提交回复
热议问题