Ios UILabel未更改
我有两种看法。第一个有两个按钮,第二个有标签和按钮。我试图根据按下的按钮更改标签的文本。在下面的代码中,我调用了second view的一个实例,并试图更改标签中的文本。但问题是文本没有改变。如果有人能在这里帮助我,我将不胜感激Ios UILabel未更改,ios,uilabel,Ios,Uilabel,我有两种看法。第一个有两个按钮,第二个有标签和按钮。我试图根据按下的按钮更改标签的文本。在下面的代码中,我调用了second view的一个实例,并试图更改标签中的文本。但问题是文本没有改变。如果有人能在这里帮助我,我将不胜感激 @interface firstview : UIViewController { IBOutlet UIButton *button1; IBOutlet UIButton *button2; } @property(nonatom
@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
在尝试设置标签文本时,视图尚未加载到第二个视图控制器中,因此标签为零
在按下视图控制器后,尝试将调用移动到,或者更好,因为只有视图控制器才应该更改其视图属性,在第二个视图控制器上为标签值设置字符串属性,并在ViewWillDisplay中设置标签文本值 来自jrturton:视图未加载到第二个视图控制器中,因此标签为nil。您可以在secondview中声明NSString属性,可以从firstview中设置此属性的值,然后可以在ViewWillDisplay或viewDidLoad方法中将此值设置为标签