Create a new CocoaTouch Class SecondViewController
, subclassing UIViewController
including XIB
and create IBOutlet
for the label
Declare a variable:
var labelTitle: String!
in SecondViewController
SecondViewController ViewDidLoad will look like this:
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = labelTitle
}
// Whereas titleLabel is the UILabel Outlet connected from XIB
Now in your FirstViewController
, on your button Tapped action
add the following
@IBAction func buttonAction(sender : UIButton) {
// If the VCs are in a storyboard you will need to get the storyboard to access them
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) as UIStoryboard
let secondVC = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
secondVC.labelTitle = sender.titleLabel?.text
// If not do this
let secondVC = SecondViewController.init(nibName: "SecondViewController", bundle: nil)
secondVC.labelTitle = sender.titleLabel?.text
}
//Next present or push your ViewController