Question

I am using IOS 7 sdk, xcode 5. I have a navigation controller that uses a button (onBtn1) to send me to UIViewController (TDSecondViewController). I have 4 buttons (onBtn1, onBtn2, onBtn3, onBtn4). Once my app displays the UIViewController, I want for the view controller to display different info based on the the button that is pressed.

sample button code in the navigation controller.

- (IBAction)onBtn1:(id)sender
{

        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        [self.navigationController pushViewController:[storyboard instantiateViewControllerWithIdentifier:@"TDSecondViewController"] animated:YES];

}

In TDSecondViewController I believe I need something like this:

 if(sender = onBtn1){
do this
} else if(sender = onBtn2){
do this
}else if(sender = onBtn3){
do this 
}else if(sender = onBtn4){

do this
}

The code currently takes me to the view controller with not problem when any of the buttons are pressed. I just can't seem to find what should be within the if statements. I also don't know exactly where the code should be placed. Any help regarding how I should do this would be greatly appreciated.

I tried different searches and could not find exactly what I am trying to do. Sorry if it has already been posted.

Était-ce utile?

La solution

What you have to do is a property in the second view controller, for example:

@property (nonatomic, assign) NSInteger buttonSelected;

On the first view controller, when you instantiate the second view controller:

- (IBAction)onBtn1:(id)sender
{
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        UIViewController vc2 = [storyboard instantiateViewControllerWithIdentifier:@"TDSecondViewController"];
        vc2.buttonSelected = 1;
        [self.navigationController pushViewController:vc2 animated:YES];
}

For each button, you can set a different value of vc2.buttonSelected.

On the second view controller, in viewDidLoad:

- (void)viewDidLoad {
    if (_buttonSelected == 1) {
        // do something
    } else if (_buttonSelected == 2) {
        // do something else
    }
    //... and so on...
}

Some things to notice:

  1. Replace UIViewController for the actual class of the view controller being pushed
  2. You can obtain the storyboard by doing self.storyboard, seems easier :)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top