(Swift) Transferring Data between View Controllers - ios

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)

Related

How can I present VC and set as new Root viewController in Swift?

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

Not navigate to specific view controller?

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

How to navigate and pass data to another view controller without use of segue in swift

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]?

How to call another Storyboard as a popover?

Assumed there is a 'ViewControllerOne' in Storyboard 'A'. If a user tapped a button on it, 'ViewControllerOne' will call Storyboard 'B' and show it as a popover on the screen. It seems that I can't find any references for my problem.
I would like to call another Storyboard from a Storyboard and show it as a popover, is it possible to do this? Thank you.
EDIT
I've tried the solution provided by #Sohil R. Memon, but I got an error :
reason: 'Storyboard (<UIStoryboard: 0x7fe69a841f00>) doesn't contain a view controller with identifier 'PaymentOnly''
I've added the RestorationID to the View Controller.
But if I change the code to :
let storyBoard : UIStoryboard = UIStoryboard(name: "PaymentOnly", bundle:nil)
let navigationController: UINavigationController = storyBoard.instantiateInitialViewController() as! UINavigationController
let viewController = navigationController.viewControllers[0] as! PaymentOnlyViewController
self.navigationController?.pushViewController(viewController, animated: true)
It worked but doesn't pop over the view. Below is the code from #Sohil R. Memon :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PaymentOption" {
let storyBoard = UIStoryboard(name: "PaymentOnly", bundle: nil)
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("PaymentOnly") as! PaymentOnlyViewController
self.presentViewController(vc, animated: false, completion: nil)
}
}
You can obviously present / popover any of the UIViewController from anywhere in any UIStoryBoard.
Expecting this is your first UIViewController and you want to call any other UIViewController from other UIStoryBoard. So for that find the below code:
let storyBoard = UIStoryboard(name: "storyboard_name", bundle: nil)
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("chatVC") as! ChatVC
self.presentViewController(vc, animated: false, completion: nil)
This way you can present or push any of the UIViewController.
Popover reference link.

How do I transition to a different view controllers present in different storyboards programmatically using swift?

let customerStoryboard = UIStoryboard(name: "Customer", bundle: nil) //First(Current) storyboard
let agentStoryboard = UIStoryboard(name: "Agent", bundle: nil) //Second storyboard
var otherVC = UIViewController! // ViewController reference
otherVC = agentStoryboard.instantiateViewControllerWithIdentifier("AgentProfile")
The last statement is not executing or showing results, is anything else required to do other than this?
You only instantiate view controller. To present it you should use presentViewController method
someVC.presentViewController(otherVC, animated: true, completion: nil)
Use this code to show the second view controller.
let otherVC : AnyObject! = self.agentStoryboard.instantiateViewControllerWithIdentifier("AgentProfile")
self.showViewController(otherVC as UIViewController, sender: otherVC)
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)

Resources