UILabel does not change

I have two kinds. The first has two buttons, and the second has a label and a button. I am trying to change the label text based on the button clicked. In the code below, I call an instance of the second view and try to change the text in the shortcut. But the problem is that the text does not change. Understand if someone can help me here.

@interface firstview : UIViewController { IBOutlet UIButton *button1; IBOutlet UIButton *button2; } @property(nonatomic, retain) IBOutlet UIButton *button1; @property(nonatomic, retain) IBOutlet UIButton *button2; -(IBAction)push:(UIButton *)sender; @end #import "firstview.h" #import "secondview.h" @implementation firstview @synthesize button1; @synthesize button2; -(IBAction)push:(UIButton *)sender{ button1.tag = 1; button2.tag = 2; if(sender.tag == button1.tag){ secondview *v2 = [[secondview alloc]initWithNibName:@"secondview" bundle:Nil]; v2.title =@ "first button"; v2.l1.text = @"BUTTON1"; [self.navigationController pushViewController:v2 animated:YES]; [v2 release]; } else if(sender.tag == button2.tag){ secondview *v2 = [[secondview alloc]initWithNibName:@"secondview" bundle:Nil]; v2.title =@ "Select"; v2.l1.text = @"BUTTON2"; [self.navigationController pushViewController:v2 animated:YES]; [v2 release]; } } @end second view #import <UIKit/UIKit.h> @interface secondview : UIViewController { IBOutlet UIButton *b2; IBOutlet UILabel *l1; } @property(nonatomic, retain)IBOutlet UIButton *b2; @property(nonatomic, retain)IBOutlet UILabel *l1; -(IBAction)pop:(id)sender; @end #import "secondview.h" @implementation secondview @synthesize b2; @synthesize l1; -(IBAction)pop:(id)sender{ } @end 
+4
source share
2 answers

While you were trying to set the label text, the view was not loaded into your second view controller, so the label is zero.

Try to transfer the calls after you press the view controller, or, even better (since only the view controller needs to change its view properties), the line properties in the second view controller for the label values ​​are set on the lines and the label text value is set inside the WillAppear view.

+4
source

From jrturton: the view is not loaded into your second view controller, so the label is zero. What you can declare as an NSString property in the second view, and you can set the value of this property from the firstview, and then you can set this value in the viewWillAppear or viewDidLoad method.

+1
source

All Articles