UILabel文本没有改变,但是xx.title正在工作 [英] UILabel text not changing, however xx.title is working

查看:89
本文介绍了UILabel文本没有改变,但是xx.title正在工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器。在第一个视图控制器中,我有名单。当我点击它时,我希望在第二个视图控制器中显示相同的名称。

I have two view controller. In first view controller I have list of names. When I click on that, I want the same name to be displayed in second view controller.

我有以下代码。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // PropDetailViewController is second view controller
    PropDetailViewController *prop = [self.storyboard instantiateViewControllerWithIdentifier:@"prop"];

    ListOfProperty *propList = [propListFinal objectAtIndex:indexPath.row];

    NSString *myText = [NSString stringWithFormat:@"%@", propList.addressOfFlat];
    prop.detailLabel.text = myText;
    prop.title  = myText;

    [self.navigationController pushViewController:prop animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

PropDetailViewController ,我有 @property(保留,非原子)IBOutlet UILabel * detailLabel;

我期待的是什么时候我点击说名称1 ,我会在UILabel和UINavigationBar中看到名称1 作为文本。但是我只在导航栏上看到名称1 ,而不是在UILabel上。

What I was expecting is when I click say Name 1, I will see Name 1 as text in UILabel and on the UINavigationBar too. However I only see Name 1 on navigation bar and not on UILabel.

推荐答案

不建议在程序流程中的那一点访问UIView项目。设置prop.detailLabel.text的值时,可能尚未加载视图。稍后加载视图时,将使用IB中XIB文件中给出的默认设置更新UIView。

It is not advisable to access an UIView item at that point in the program flow. When setting the value of prop.detailLabel.text the view may not have been loaded. When the view is loaded later then the UIView is updated with the default settings given in the XIB file in IB.

你应该设置一个NSString属性,假设

You should rather set an NSString property, lets say

@property (retain, nonatomic) NSString *propName;

在推送视图控制器之前分配它。但是使用此属性而不是UILable。在viewDidLoad的PropDetailViewController中执行以下操作:

assign it before pushing the view controller as you do. But use this property and not the UILable. And in PropDetailViewController in viewDidLoad do the following:

(void) viewDidLoad {
  // call super viewDidLoad and all the works ...

  self.detailLabel.text = propName;

} 

您可以使用viewWillAppear代替viewDidLoad。因为在分配属性的值时可以执行viewDidLoad。

Instead of viewDidLoad you could use viewWillAppear. Because viewDidLoad COULD be executed already when you assign the property's value.

如果你想要在保存方面,那么发明一个新的init方法,你可以移交你想要设置的所有值。
但我从来没有与故事板结合使用(你可以使用实例化而不是初始化......)因此我无法提出任何建议。

If you want to be on the save side then invent a new init method where you hand over all the values that you want to be set upfront. But I never did that in combination with storyboard (where you may use instantiate... rather than init...) and therefore I cannot give any advise out of the top of my head.

另一种干净的方法是坚持使用propName属性,但要实现自定义setter - (void)setPropName:(NSString)propName; 您设置属性的位置(如果您自动合成,可能是_propName)并设置UILable文本并在viewDidLoad中设置UILable文本。

Another clean approach would be to stick with the propName property but to implement a custom setter -(void)setPropName:(NSString)propName; where you set the property (probably _propName if you autosynthesize) AND set the UILable text plus setting the UILable text within viewDidLoad.

这篇关于UILabel文本没有改变,但是xx.title正在工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆