[英]UILabel not changing
我有兩個觀點。 第一個是2個按鈕,第二個是標簽和按鈕。 我試圖根據按下的按鈕更改標簽的文本。 在下面的代碼中,我調用第二個視圖的實例並嘗試更改標簽中的文本。 但問題是文字沒有改變。 如果有人可以幫助我,我將不勝感激
@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
在您嘗試設置標簽文本時,視圖尚未加載到第二個視圖控制器中,因此標簽為零。
在推動視圖控制器之后嘗試將調用移動到,或者更好的是(因為只有視圖控制器應該更改其視圖屬性)在第二個視圖控制器上為標簽值具有字符串屬性,並在viewWillAppear中設置標簽文本值。
來自jrturton:視圖未在第二個視圖控制器中加載,因此標簽為零。 你可以在secondview中聲明一個NSString屬性,你可以從firstview設置這個屬性的值,然后你可以在viewWillAppear或viewDidLoad方法中將這個值設置為標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.