Ethan Allen
Ethan Allen

Reputation: 14835

How do I set a property on a Storyboard UIViewController between segues on iOS 5?

I am trying to set a simple image on an UIImageView as such when a button is touched on the first view:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    ImageViewController *imageViewController = [segue destinationViewController];
    imageViewController.title = @"test";
    imageViewController.imvBig.image = [UIImage imageNamed:@"test.png"];
    imageViewController.lblTest.text = @"test2";
}

The .title gets set and appears just fine, but for some reason I can't get the imvBig.image to show up, nor can I get the label text to appear. Yes, I have the property created with an Outlet, etc (as I just dragged it over from IB right into the code and it was autocreated).

Can someone tell me how to properly set a property from one regular view to the next in a Storyboard setup?

Upvotes: 3

Views: 3785

Answers (3)

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

You don't need to set the viewController just use the segue destinationViewController property. I always set a identifier in the storyboard first so I can use multiple segues:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
if ([[segue identifier] isEqualToString:@"Show ImageViewController"]) {

[segue.destinationViewController setTitle:@Test"];
[segue.destinationViewController setFoo:@"Bar"];
//More stuff to set
}

}

also create an image in the ImageViewController as a property and synthesize it,then set that to the image view in the viewDidAppear:

self. imvBig.image = myNewImge;//this in the viewDidLoad

And Set that image in the segue:

[segue.destinationViewController setMyImage:[UIImage imageNamed@"test.png"]];

Upvotes: 0

Ethan Allen
Ethan Allen

Reputation: 14835

Looks like I was able to get this to work by creating an NSString property and then setting the label to the string on viewDidLoad of the ImageViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.lbltest.text = _myString;
}

I guess I can't set the property directly, until after the view is loaded.

Upvotes: 2

rob mayoff
rob mayoff

Reputation: 385890

Probably the ImageViewController has not loaded its views from the storyboard yet when the system sends prepareForSegue:sender:, so those outlets (imvBig and lblTest) are nil. Messages sent to nil are silently ignored.

It's usually not appropriate for the source view controller to muck around with the destination view controller's views. The destination view controller should set those view's settings, usually in either viewDidLoad, viewWillAppear:, or viewDidAppear:.

Upvotes: 2

Related Questions