3

I have a main view with 3 buttons. Clicking on any of the buttons adds a SubView.

The buttons have different titles and are all linked to IBAction "switchView"

The "switchView" code is below.

- (IBAction)switchView:(id)sender{
    secondView *myViewController = [[secondView alloc] initWithNibName:@"secondView" bundle:nil];
    [self.view addSubview:myViewController.view];
}

The "secondView" loads up correctly and everything works well.

The problem is I want to be able to know which button was the Sender.

I don't want to create 3 subviews, one for each button. The code and XIB would be absolutely the same>

The only difference would be a variable that I would like to set up in the second view (viewDidLoad method) depending on who is the Sender (which button was clicked)

Is this possible? Or I would need to create 3 subViews - one for each button?

Your help is greatly appreciated!

user885483
  • 107
  • 1
  • 2
  • 10

3 Answers3

16

You can identify different buttons with the tag property. e.g. with your method:

-(IBAction)switchView:(id)sender {
    UIButton *button = (UIButton*)sender;
    if (button.tag == 1) {
        //TODO: Code here...
    } else if (button.tag == 2) {
        //TODO: Code here...
    } else {
        //TODO: Code here...
    }
}

The tag property can be set via the InterfaceBuilder. Hope this helps.

Markus
  • 585
  • 7
  • 18
  • THanks, but maybe I didn't explain the issue correctly. I know how to get the Sender inside the switchView method. The issue is after that. switchView method initiates a subview. This subview has its own secondView.XIB, secondView.h and secondView.m files. Inside secondView.m ((void)viewDidLoad method) I want to find out which button initiated the secondView.XIB – user885483 Jan 25 '12 at 12:46
  • Maybe this is not possible. A work around would be to store the Sender tag in "global variable" inside the switchView method. (I could store it as a key in NSUserDefaults). Then retrieve it in secondView subview. – user885483 Jan 25 '12 at 14:18
  • 2
    Okay, understood. Well my approach would be to call the new view controller within your - (IBAction)switchView:(id)sender; method and pass the sender.tag as a property of the (id)initWith... call. e.g. MyViewController *view = [[MyViewController alloc] initWithSenderTag:button.tag]; However, you would still have to store the button.tag as a global variable within your MyViewController to check it within your - (void)viewDidLoad; method. – Markus Jan 26 '12 at 08:23
4

I think you can solve in 2 ways:

  1. Create a property like: @property (nonatomic, strong) IBOutlet UIButton *button1, *button2, *button3; in your viewcontroller and link the buttons to them as referencing outlet on the XIB.
  2. Give a different tag to each button on your xib and ask for the tag of the sender with UIButton *b=(UIButton*)sender; b.tag; like Markus posted in detail.
Aqua
  • 76
  • 3
0

Solving my problem it all came down to transferring data between the mainView and subView.

In my mainView.h I declared an NSString and its @property

...

NSString *btnPressed;

}
@property(nonatomic, retain) NSString *btnPressed;

...

then in my mainView.m inside the switchView method I did this:

- (IBAction)switchView:(id)sender{

secondView *myViewController = [[secondView alloc] initWithNibName:@"secondView" bundle:nil];

btnPressed = [NSString stringWithFormat:@"%i", [sender tag]];

[myViewController setBtnPressed:self.btnPressed];

[self.view addSubview:myViewController.view];
}

This line in the code above actually takes care of transferring the data to the newly created subView:

[myViewController setBtnPressed:self.btnPressed];

Then in my secondView.h I declare exactly the same NSString *btnPressed and its @property (though this a completely different object than the one declared in main)

Then in my secondView.m I get the value of the button pressed I'm interested in.

- (void)viewDidLoad {
[super viewDidLoad];

int theValueOfTheButtonPressed = [self.btnPressed intValue];
}

This works well.

Don't forget to @synthesize btnPressed; as well as [btnPressed release]; in both mainView.m and secondView.m

user885483
  • 107
  • 1
  • 2
  • 10