I run into a small problem (Not surprisingly, ’cause I’ve just started with xcode). I tried to solve it with if-statemens, but they were clearly the wrong way to go.
Here is what I am trying to do: In the first ViewController I have for example 4 Buttons. If the user presses the first button he gets to ViewController2 and the label says “You pressed the first button”. If the user presses the second button he gets to ViewController2 and the label says “You pressed the second button” and so on.
I tried to solve it with Tag Statements, like:
FirstViewController.m
- (IBAction)switch:(id)sender;
{
UIButton *buttonPressed = (UIButton *)sender;
SecondViewController *second =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
second.buttonTag = buttonPressed.tag;
[self.navigationController pushViewController:second animated:YES];
(button.tag = 9001);
- (IBAction)switch2:(id)sender2;
{
UIButton *buttonPressed = (UIButton *)sender2;
SecondViewController *third =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:third animated:YES];
second.buttonTag = buttonPressed.tag;
[self.navigationController pushViewController:third animated:YES];
(button2.tag = 9002);
And here what I did in SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if (buttonTag == 9001) {
self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext"];
self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext"];
self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext?"];
if (buttonTag == 9002) {
self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext2"];
self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext2"];
self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext2?"];
He always gives me the labels from ButtonTag 9001 – Someone any idea why?
Here is a handy little trick for you: tags.
Every
UIViewcan have a propertytag. It is a simple integer, and you can assign it in code (button.tag = 456;) or in Interface Builder. In yourswitchmethod, simply use:So just to make sure: your statement
is completely wrong. If the new view controller has a @property (which you define in the
.h-file and@synthesizein the.m-file), you can simply assign these properties before pushing the new view controller. That’s what we did in the above code snippet.