Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Segueing to other View Controller Programmatically & Reassigning values from other classes

After a lot of googling, reading apples documentation and more googling. I still can't seem to solve this problem. I thought this was going to be pretty easy in swift, but I can't figure out how to do it.

Problem: I want to segue to my AddCategory Controller (current method of segueing works fine) and then during the segue to pass the values from the category variable to the categoryText variable from my AddCategory class.

class CategoryViewController: UIViewController {

    var category: String = String()
    var addSubscription = AddCategory() // View Controller Class

    func returnToAddView() {
        self.performSegueWithIdentifier("returnAddViewController", sender: nil)
        addSubscription.categoryText = self.category
    }

    @IBAction func EntertainmentButton(sender: AnyObject) {
        category = "Entertainment"
        returnToAddView()
    }
}

My AddCategory ViewController

class AddCategory: UIViewController {
    var categoryText: String = String()

    override func viewDidLoad() {
        println(categoryText)
    }
}

I want to assign the category variable to the categoryText variable from my AddCategory class, but for some reason the values of the category variable are not being assigned to the categoryText variable from my AddCategory class.

If anyone can help me out with this it would be greatly appreciated,. Thanks,

Ozzie

1 Answer

Kirkbyo, it seems that you are setting the attributes of the desired view controller but you are not using that view controller, you just perform the segue with an identifier, which will load a view controller with all default attributes.

Here is the code you will need to use:

Objective C:

YourViewControllerName *controller = [[YourViewControllerName alloc] init];

//Set any attributes of the view controller before it is displayed, this is where you would set the category text in your code.

[self presentViewController:controller animated:YES completion:nil];

Swift:

var controller = YourViewController()

//Set any attributes of the view controller before it is displayed, this is where you would set the category text in your code.

self.presentViewController(viewControllerToPresent: controller, animated: true, completion: nil)

Instead of just using the performSegueWithIdentifier:sender: method to display a view controller, you should use showViewController:sender: to show the view controller with all of the attributes you want to set.

If you have any questions, feel free to ask!

Hey Landon,

Thanks for helping me out with this. When using the showViewController method, it just loads a black screen. I also tried using presentViewController but the same thing happened... To give you a better idea of my storyboard flow I created this mockup (Nothing Pretty)

Storyboard Flow

Thanks Again,

Ozzie

Still did not load properly... Here's what it looks like

Blank Screen

Ozzy, I highly apologize for the inconvenience! My next guess would be to use the code that you use in your question, but add the prepareForSegue:sender: method to configure for the next view controller.

Objective C:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"yourSegueIdentifier"]) {

        YourControllerClassName *controller = (YourControllerClassName *)segue.destinationViewController;

        //Configure controller attributes.
    }
}

Swift:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

   if (segue.identifier == "yourSegueIdentifer") {

        let controller = segue.destinationViewController as YourControllerClassName

        //Configure controller attributes.
   }
}

If you have any questions, feel free to ask!

I will give it a try. Thanks for trying to help :)

Ozzie