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

Passing data to a custom UIView

Hi all,

I have a UIContainerView that holds 3 subviews (in a scrollview swipe with a page control). Right now, I have each UIView subview created manually. However, with the complexity of one of the views, i thought it easiest to lay it out in a xib. with hardcoded inputs, I have the xib loading properly into my ContainerView. However, I now want to use data from a query located in my containerViewController to populate text for properties in my custom UIViews. When doing something similar for a custom TableViewCell you can access the properties as such:

let cell:MyCustomCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as MyCustomCell

You can then access the properties of "MyCustomCell". How would you do this for a UIView?

Thanks for your help.

2 Answers

Hi Aaron,

I think you mean UIViewController as views inherit data while the controller passes it along, to accomplish this all you need is a class level variable defined in your custom controller which you can assign data to that variable using the prepareForSegue function in the parent view controller.

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    // "MyView" is the name of the segue between the view controllers
    if (segue.identifier == "MyView") {
        let view: CustomController = segue!.destinationViewController as CustomController
        view.customVariable = "Something"
    }
}

So as you can see once we've selected the view controller and down cast it to our controller CustomController as have access to any custom properties/methods within it.

Hope that helps.

Thanks Chris.

Yeah I can pass data through from one ViewController to another, but what I'm struggling to do (and I probably didn't word the question properly) is to somehow update the properties of a UIView (and associated xib) from a query I am running in a UIViewController.

So I have a ViewController which contains a ContainerView (HeaderView). HeaderView displays information about the user (name, member since, quote, etc) that I pull from a Parse.com query. Within HeaderView, I have 3 subviews that are all created and laid out manually. One of the subviews (subview2) needs to have a slightly more complex layout than the others, so I created a xib (and tied it's class to a UIView subclassed swift file called headersubview.swift) to lay it out. I can go into headersubview.swift and do something like:

self.hometownLabel.text = "Chicago"

The problem I'm facing is figuring out how to update subview2 with information from a query on HeaderView. Because the information in the subview is unique to the user, it would need to use the data from the table to populate it.

Is this possible? Thanks so much for your help.

How did you solve it?