Swift NavBar to TabBar prepareforsegue - ios

I am trying to send data via prepareforsegue to a TabBarController that is embedded in a NavigationBarController.
This was my last attempt and it did not work
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destinationViewController = tabBarController!.viewControllers![0] as! ProgressStartViewController // or whatever tab index you're trying to access
destinationViewController.departureAirportLineChart = "data1"
destinationViewController.arrivalAirportLineChart = "data2"
}
Any ideas?

Related

How to data in segue using Swift

I'd like to pass my array from my first to my second ViewController.
In the end of my first VC I tried to pass it like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = ViewController2()
secondVC.passengers2 = passengers
print(secondVC.passengers2)
print(passengers)
}
"passengers" is my first array in "ViewController.swift" and "passengers2" my sewcond array in "ViewController2.swift".
When I go over to my second VC, the console tells me, that "passengers" and "passengers2" have the same value, but as soon as I am in "ViewController2.swift", "passengers2" is emtpy for some reason.
Does anyone know why?
The problem in your code is that you instantiate a new ViewController2 object, which is never used.
You want to use the destination view controller that is inside your segue object, like that :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destinationViewController as? ViewController2 {
destinationVC.passengers2 = passengers
}
}

Programmatic embed segue

I'm using a Page View Controller to implement an image carousel in my app. I have a container view in my parent view controller which has an embed segue to the Page View Controller. The problem I'm having is when I need to go off and fetch these images asynchronously and the segue happens before viewDidLoad gets the images.
My prepare for segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "startCarousel" {
let carouselPageViewController = segue.destination as! CarouselPageViewController
carouselPageViewController.images = carouselImages
}
}
in viewDidLoad:
GalleryManager.sharedInstance.getData = {
(data) in
// Assign images to instance property here ...
}
My thinking is that once I get the images that I programmatically start the segue myself. Or if I should be doing this a different way any advice would be much appreciated.
You can do something like, return false in shouldPerformSegue and initiate segue when data is loaded
i.e.
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "startCarousel" {
return (detail != nil)
}
return true
}
Then in viewDidLoad
GalleryManager.sharedInstance.getData = { [weak self]
(data) in
self?.performSegue(withIdentifier: "startCarousel", sender: nil)
}
No, you can't delay an embed segue. If you use an embed segue it fires as soon as the view controller is loaded.
You need to wait until the data loads and then call the setViewControllers(direction:animated:completion) to install the view controllers that you will use to display the data once the data is loaded.
Swift 3, Xcode 8
You can realize in this way:
set a reference to your page view controller in prepareForSegue in your masterViewController:
var myContainerViewController: UIPageViewController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "embedSegue" {
if let destination = segue.destination as? YourPageViewControllerClass {
self.myContainerViewController = destination
}
}
Define a function in your own pageViewController class:
func someFunction(){
//configure your image or swipe page
}
when you get the images, in your masterViewController call:
self.myContainerViewController?.someFunction()
you can't delay an embed segue.
you try to put button in view controller and then after view load set button click to set image...

How do I get the property value from a container view in Swift

I have 1 UIViewController that contains a UIContainerView and a UIButton.
Also, I have a UITableViewController (It has a UITextField and a UITextView) that is embedded in the UIContainerView in the UIViewController.
I'm trying to get the string values that will be available in TextField and in TextView.
I tried to use the segue to get the properties values, but I failed to do so, see the code below.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TVCSegue" {
let containerTVC = segue.destination as? FieldsTVC
self.textField.text = FieldsTVC?.textField?.text
self.textView.text = FieldsTVC?.textView?.text
}
}
The code above has 'textField' and 'textView' as properties in the UIViewController to assign the values to.
But I believe that it doesn't work as I get the values before they changes. Please provide me with a functional way to do so.
When your main view loads, the container performs a segue to its assigned initial / root ViewController. At that point, you can get a reference to it:
var theFieldsTVC: FieldsTVC?
Now, in prepare for segue, assign that variable:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TVCSegue" {
if let vc = segue.destination as? FieldsTVC {
theFieldsTVC = vc
}
}
}
Then, you can:
#IBAction func buttonTapped(_ sender: Any) {
self.textField.text = theFieldsTVC?.textField?.text
self.textView.text = theFieldsTVC?.textView?.text
}

Navigation Between Two View Controller

I have two View Controllers. in mainViewController there is TabBarButton when it clicked user goes to 1stViewController and there is UIButton when it has clicked user goes to 2ndViewController.
before making my 2ndViewController everything worked fine. but after i add segue function in my program i got error.
could not cast value of type 'Calculator.1stViewController' (0x791a8) to 'Calculator.2ndViewController' (0x794c8).
briefly my code is
#IBAction func CalculateGpa(){
//Calucation Goes Here
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var AnotherName : 2ndViewController = segue.destinationViewController as! 2ndViewController
//Code here
}
UIButton Perform the segue and BarButton do nothing it just go to 1stView Controller. when i run the program and click BarButton im getting that above error.
its because prepareForSegue cant identify to which button it should perform right? how to solve it?
try this code.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "youridentifiername") {
//your class name
if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
viewController.dealEntry = deal
}
}
}

prepareForSegue can't get the destination view controller on iOS 7

I have to write an iOS app that supports both iOS 7 and 8 and in this app I have two view controllers A and B with navigation controllers and a segue between them. When the segue activates, I have to fetch data from the view controller A to the view controller B. My code works perfectly on iOS 8 but not on iOS 7.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if segue.identifier == userDataSegue {
let navController = segue.destinationViewController as? UINavigationController
if let userDataTableViewController = navController?.topViewController as? UserDataTableViewController {
userDataTableViewController.userData = user
}
}
}
Do you know why does this happen and how can I fix it? I could "work around" the problem by saving stuff in NSUserDefaults and reading it in the vc B but that would make my code a mess.
I appreciate any help :D
Don't forget to create a public property userData in Second ViewController.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "segueTest") {
var svc = segue!.destinationViewController as secondViewController;
svc.userData = user
}
}

Resources