ios - UILabel 没有改变

标签 ios uilabel

我有两种看法。第一个有 2 个按钮,第二个有一个标签和一个按钮。我正在尝试根据按下的按钮更改标签的文本。在下面的代码中,我调用了第二个 View 的实例并尝试更改标签中的文本。但问题是文本没有改变。如果有人能在这里帮助我,我将不胜感激

@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

最佳答案

在您尝试设置标签文本时, View 尚未加载到您的第二个 View Controller 中,因此标签为零。

尝试在推送 View Controller 后将调用移动到在 viewWillAppear 里面。

关于ios - UILabel 没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966883/

相关文章:

ios - 如何在 iOS 中通过 map-kit 在两点之间绘制折线?

objective-c - 如何使CGContextShowTextAtPoint()自动缩小某个矩形中的文本

ios - 在 Swift 中使用 hidesBackButton 隐藏导航栏中的后退按钮

ios - 从 UIViewController 获取代码到 UIView

ios - 在 uiLabel 的下划线部分和链接带下划线的部分

ios - 如何使用用户输入将行添加到 TableView ?

ios - 来自 xcode 可视化编辑器的 UILabel 中的大写文本

iphone - 当大小不成比例时如何在 UILabel 中调整文本

ios - 如何从 RSS 获取图像到单元格

swift - 部分标签可点击并同时加粗