Reputation: 53
I'm working on a multi-page brochure app, and have used segues to join pages. I'm getting to the point where I have 20 or so pages and the segue system seems messy.
I have created a button programmatically in the ViewController. I'm just looking to make that button always links to a different View no matter what page it is on. This would mean I wouldn't have to draw segues hundreds of times!
class ViewController: UIViewController {
override func viewDidLoad() {
let button:UIButton = UIButton(frame: CGRect(x: 25, y: 50, width: 250, height: 75))
button.backgroundColor = .black
button.setTitle("", for: .normal)
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
}
func buttonClicked() {
print("Button Clicked")
}
I can't find any code anywhere that would allow me to do this, so I'm wondering if it's even possible.
Any help is appreciated.
Upvotes: 2
Views: 3515
Reputation: 25
Ok here's what i understand, you want to switch ViewControllers on button press.
For the ViewController you want it to Seque too, Add Signupvc to 'Storyboard ID' below Custom Class on the right hand side.
@IBAction func differentSequeAction(sender: AnyObject)
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let SignUp: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "Signupvc")
self.present(SignUp, animated: true, completion: nil)
}
Upvotes: 0
Reputation: 598
I am assuming that you want to show another page programmatically without segue.Here is the sample code.
let storyboard: UIStoryboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("YoourViewControllerStoryBoardID") as UIViewController
self.present(vc, animated: true, completion: nil)
This method creates a new instance of the specified view controller each time you call it.
If you already have the pointer to viewController just present it.
Upvotes: 4