I am trying to navigate to specific view controller but it is not working.
what i am doing is below
var viewControllers:[UIViewController]!
var firstView : UIViewController!
let storyboard = UIStoryboard(name: "Main", bundle: nil)
firstView = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
viewControllers = [firstView]
#IBAction func BackAction(_ sender: Any) {
for aViewController in viewControllers {
if aViewController is FirstViewController {
self.navigationController!.popToViewController(aViewController, animated: true)
}
}
}
but not working .
Please help me where i am wrong.
Thanks
Related
I have two viewControllers "FirstViewController"&"SecondViewControler" that contain a button that presents a new viewController called "BibliothequesViewController" when a button is clicked:
#IBAction func onLibrariesButtonClicked(_ sender: Any) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
self.present(vc, animated: true, completion: nil)
}
For the time being, the BibliothequesViewController contains a button that's redirects to the FirstViewController with a Segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("SegWay being performed")
// Every segway has a beginning point and ending point
// We're trying to access the ending point
// as type FullScreenMovieController
// Then we're setting the chosenMovie variable of FullScreenMovieController to 1
let vc = segue.destination as! FirstViewController
vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
print("vc.receivedSelectedLibraryFromBibliothequesList :", vc.receivedSelectedLibraryFromBibliothequesList )
}
This is the code that performs the Segue:
self.performSegue(withIdentifier: "BiblioToLoginSegue", sender: self)
What I need to implement is:
Redirect to the view (FirstViewController or SecondViewControler) that actually presented BibliothequesViewController.
The code above automatically redirects to FirstViewController whether it was the one that presented BibliothequesViewController or not.
I don't see which approach should I follow to implement this.
Should I use push instead of present in the FirstViewController&SecondViewControler?
The simplest way to teach a class to know something is to add a property:
class BibliothequesViewController {
...
var presentedBy: UIViewController?
...
}
to assign the property after the controller instantiation
let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
vc.presentedBy = self
and to use this property when the time comes.
I took #Roman's advice but I changed a couple of things.
CAVEAT: I am not sure this is the right approach but it worked for me.
BibliothequesViewController
class BibliothequesViewController: UIViewController {
static let sharedInstance = BibliothequesViewController()
var presentedBy: UIViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if BibliothequesViewController.sharedInstance.presentedBy! is LoginViewController {
let vc = segue.destination as! LoginViewController
vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
} else {
let vc = segue.destination as! ForgetPasswordViewController
vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
}
}
#IBAction func onLibrarySelected(_ sender: Any) {
if BibliothequesViewController.sharedInstance.presentedBy! is LoginViewController {
self.performSegue(withIdentifier: "BiblioToLoginSegue", sender: self)
} else {
self.performSegue(withIdentifier: "biblioToPasswordForgottenSigue", sender: self)
}
}
}
LoginViewController
class LoginViewController: UIViewController {
#IBAction func onLibrariesButtonClicked(_ sender: Any) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
BibliothequesViewController.sharedInstance.presentedBy=self
self.present(vc, animated: true, completion: nil)
}
}
ForgetPasswordViewController
class ForgetPasswordViewController: UIViewController {
#IBAction func onLibrariesButtonClicked(_ sender: Any) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
BibliothequesViewController.sharedInstance.presentedBy=self
self.present(vc, animated: true, completion: nil)
}
}
NOTE: You have to create two Segues from BibliothequesViewController to LoginViewController and ForgetPasswordViewController with the identifiers mentioned in BibliothequesViewController.
I have this warning:
Presenting view controllers on detached view controllers is discouraged
I need to know how can set my rootViewController in another VC and avoid this warning
I have this code in my VC:
#IBAction func dissmissInfo(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")
present(vc, animated: true, completion: nil)
})
And in the firstVC I have this:
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.keyWindow?.rootViewController = self
}
but when I try to move to another VC I have the same warning:
Presenting view controllers on detached view controllers is discouraged
You mean you want to set firstVC
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")
as the new RootViewController?
If YES:
#IBAction func dissmissInfo(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")
UIApplication.shared.keyWindow?.rootViewController = vc
})
Then in firstVC, remove
UIApplication.shared.keyWindow?.rootViewController = self
I have some view controllers which are presenting another view controller like so:
class LikeDidTapViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Question") as? QuestionViewController {
if let navigator = navigationController {
navigator.pushViewController(viewController, animated: true)
}
}
}
}
In QuestionViewController I need to check which view controller presented it so I can use conditionals to perform various tasks. How can I accomplish this?
I am attempting to transfer a value between my view controllers. When I simply connect my button to the other view controller, like this:
I am able to make it work. I am overriding prepareforSegue.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestinationViewController : ViewTwo = segue.destinationViewController as! ViewTwo
DestinationViewController.scoretext = score.text!
}
It all works fine but when I open the other controller programatically, the value isn't transferred. This is the code I'm using for that...
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo
self.presentViewController(vc, animated: true, completion: nil)
When you use instantiateViewControllerWithIdentifier, prepareForSegue is not called. You will have to assign scoretext after instantiation only.
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo
vc.scoretext = score.text!
self.presentViewController(vc, animated: true, completion: nil)
Hy, I am new to swift. I have on question. I have one view controller in which have a textbox and button and similarly I have second view controller which have a textbox and button. I want to do that whenever I pressed button of first view controller The second view controller appear and data of textbox of first view controller will pass to the textbox of second view controller but without the use of segue.
Please help me.
In FirstViewController.swift
#IBAction weak var textFiled: UITextField!
#IBAction func sendDatatoNextVC(sender: UIButton) {
let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
let vc : SecondViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewControllerSBID”) as SecondViewController
vc. recevedString = textFiled.text
self.presentViewController(vc, animated: true, completion: nil)
}
In SecondViewController.swift
var recevedString: String = “”
#IBAction weak var textFiled2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textFiled2.text = recevedString
}
You can use instantiateViewControllerWithIdentifier if you don't want to use segue.
Consider below code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let someViewConroller = storyboard.instantiateViewControllerWithIdentifier("someViewControllerID") as! SomeViewController
someViewConroller.someObject = yourObject //here you can assign object to SomeViewController
self.presentViewController(someViewConroller, animated: true, completion: nil)
try this:
if you creating custom view controller.
var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController
controller.exercisedPassed = "Ex1"
self.presentViewController(controller, animated: true, completion: nil)
In first VC ,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewController(withIdentifier:"SecondViewController") as! SecondViewController
vc.detail = [name,age,sal]
self.present(vc, animated: true, completion: nil)
In second VC ,
var detail:[String]?