UILabel没有变化

时间:2012-06-10 06:52:08

标签: ios uilabel

我有两个观点。第一个是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

2 个答案:

答案 0 :(得分:4)

当您尝试设置标签文本时,视图尚未加载到第二个视图控制器中,因此标签为零。

在推动视图控制器之后尝试将调用移动到,或者更好的是(因为只有视图控制器应该更改其视图属性)在第二个视图控制器上为标签值具有字符串属性,并设置标签文本值在viewWillAppear里面。

答案 1 :(得分:1)

来自jrturton:视图未在第二个视图控制器中加载,因此标签为零。你可以在secondview中声明一个NSString属性,你可以从firstview设置这个属性的值,然后你可以在viewWillAppear或viewDidLoad方法中将这个值设置为标签。