Reputation: 67
My case is I wanna show new page from ViewController
modal when click button, can we do this like android just onClick
on the button and change the page ?
Click from MainViewController > present modally > ProfileViewController Then ProfileViewController > AboutViewController (dismissed)
In My ProfileViewController
@IBAction func testSupport(_ sender: Any) {
let aboutView = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
self.navigationController?.pushViewController(aboutView, animated: true)
self.dismiss(animated: false, completion: nil)
}
Upvotes: 0
Views: 2540
Reputation: 1693
Your "self" is presented. It have not navigationcontroller so you can pushviewcontroller. Try:
@IBAction func testSupport(_ sender: Any) {
let aboutView = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
self.dismiss(animated: true) {
if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
topController.navigationController?.pushViewController(aboutView, animated: true)
}
}
}
Upvotes: 1
Reputation: 1507
You can use delegates for this. In your ProfileViewController.swift
declare a protocol
protocol ProfileViewControllerDelegate {
func didPressSupportButton()
}
Then in your ProfileViewController
class declare
var delegate: ProfileViewControllerDelegate?
In your support action:
@IBAction func testSupport(_ sender: Any) {
delegate?.didPressSupportButton()
self.dismiss(animated: false, completion: nil)
}
When you present your view, if you're doing it programmatically, or in prepare for segue:
yourModalViewController.delegate = self
Then in your MainViewController
conform to protocol
extension MainViewController: ProfileViewControllerDelegate {
func didPressSupportButton {
let aboutView = self.storyboard?.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
self.navigationController?.pushViewController(aboutView, animated: true)
}
}
Upvotes: 1
Reputation:
You can achieve this :
Just Ctrl-drag from your ViewController to the AboutViewController you want. Then a popup will appear where you can choose which segue you want to perform (modal)
Now click on the segue (the link created between the 2 ViewControllers, and give it an identifier).
Add this code in your ViewController (the one you are currently in)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueId" {
let aboutViewController = segue.destination as! AboutViewController
// do some init if you want
}
}
Perform the segue within you button action :
@IBAction func didTapButton(_ sender: Any) {
self.performSegue(withIdentifier: "yourSegueId", sender: self)
}
You can present the ViewController just like your are currently doing, but you are asking for a modal presentation so :
@IBAction func didTapButton(_ sender: Any) {
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let aboutViewController = mainStoryboard.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
self.present(aboutViewController, animated: true, completion: nil) nil)
}
Upvotes: 1