我有两个视图控制器。在第一个视图控制器中,我有名称列表。当我单击它时,我希望在第二个视图控制器中显示相同的名称。
我有下面的代码。
-(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];
}
and in PropDetailViewController
, 我有@property (retain, nonatomic) IBOutlet UILabel *detailLabel;
.
我期待的是当我点击说时Name 1
, 我再看看吧Name 1
作为 UILabel 和 UINavigationBar 中的文本。然而我只看到Name 1
在导航栏上而不是 UILabel 上。
不建议在程序流中的该点访问 UIView 项。设置 prop.detailLabel.text 的值时,视图可能尚未加载。当稍后加载视图时,UIView 将使用 IB 中的 XIB 文件中给出的默认设置进行更新。
你应该设置一个 NSString 属性,比如说
@property (retain, nonatomic) NSString *propName;
像您一样在推动视图控制器之前分配它。但使用此属性而不是 UILable。并在 PropDetailViewController 中的 viewDidLoad 中执行以下操作:
(void) viewDidLoad {
// call super viewDidLoad and all the works ...
self.detailLabel.text = propName;
}
您可以使用 viewWillAppear 代替 viewDidLoad。因为当您分配属性值时,viewDidLoad 可能已经被执行。
如果您想节省开支,请发明一个新的 init 方法,在该方法中您可以传递要预先设置的所有值。
但我从未与故事板结合使用(您可以使用实例化...而不是初始化...),因此我无法凭空给出任何建议。
另一种干净的方法是坚持使用 propName 属性,但实现自定义设置器-(void)setPropName:(NSString)propName;
在其中设置属性(如果自动合成,则可能是 _propName)并设置 UILable 文本以及在 viewDidLoad 中设置 UILable 文本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)