Reputation: 181
I have two views. The first one is having 2 buttons and second one is having a label and a button. Am trying to change the text of the label based on the button pressed. In the code below, am calling an instance of second view and trying to change text in the label. But the problem is the text is not changing. Will appreciate if some one 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
Upvotes: 4
Views: 507
Reputation: 3324
From jrturton: The view is not been loaded in your second view controller, so the label is nil. What can you is declare a NSString property in secondview and you can set value of this property from firstview and then you can set this value to the label in viewWillAppear or viewDidLoad method.
Upvotes: 1
Reputation: 119242
At the time you are trying to set the label text, the view has not been loaded in your second view controller, so the label is nil.
Try moving the calls to after you push the view controller, or, better still (since only a view controller should change its views properties) have string properties on the second view controller for the label values, and set the label text value inside viewWillAppear.
Upvotes: 4