Nareille
Nareille

Reputation: 821

How to tell the upcomming viewController by which button he was called

I have a viewController, lets say blaVC, which calls another view controller blubbVC through a button press. As there are 4 buttons, all calling blubbVC with the same nib, but the content should vary according to which button was pressed in blaVC (content will be loaded from a plist), I'd need blubbVC to know "I've been called, because button A was pressed, so I'll load from the plist with objectForKey:@"button A"" (so to speak..).

I call blubbVC like that

- (IBAction) buttonA:(id)sender {
blubbVC *detailView = [[blubbVC alloc] initWithNibName:@"blubbNib" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}

I tried to pass sender, but as I load the content in initWithNibName, sender comes to late, the content was already loaded (or tried to) and my view stays empty. How would I do that?

Thank you!

Upvotes: 0

Views: 116

Answers (2)

shawnwall
shawnwall

Reputation: 4607

You need to create a custom init method in your view controller. Something like:

+(id)initWithCaller:(NSString*)caller
{
    if(self == [super initWithNibName:@"blubbNib" bundle:nil]) 
    {
      calledFrom = x;
    }
    return self;
}

That was just typing from memory so there may be some errors. You'd then use that new init method in your IBActions

Upvotes: 0

bobDevil
bobDevil

Reputation: 29508

You should define a function that says takes the sender (or some value representing which button was pressed) that sets up all the state, after it's been initialized.

For example:

[detailView setupContent:(id)sender];

When you init the view controller, it's not yet displayed. Before you call pushViewController, call the new function, so you'd have:

- (IBAction) buttonA:(id)sender {
    blubbVC *detailView = [[blubbVC alloc] initWithNibName:@"blubbNib" bundle:nil];
    [detailView setupContent:sender];
    [self.navigationController pushViewController:detailView animated:YES];
    [detailView release];
}

If it's true that this is too late (you need the data in 'initWithNibName'), then either define your own -[initWithNibName: andSender:] or something similar, and calls [super initWithNibName:] before setting up the rest of the state.

Upvotes: 1

Related Questions