如何解决iOS通过编程方式取决于对象ivar
| 我有一个非常有趣的问题,涉及loadView
,viewDidLoad
,viewWillAppear
和ivar的状态。我有一个基于导航的应用程序。在我的第一级视图中,我有一个表列表,然后单击一个单元格,它将带我到第二级视图(详细信息视图)。当我单击一个单元格时,我还会向被推入的视图控制器中添加一个\“Office
\”对象(其中包含诸如streetAddress
和BoxAddress
之类的字符串)。然后,我使用Office
之类的Office
对象中的内容填充详细视图(框为UILabel)。现在我想在这里实现的是,有时BoxAddress的stringValue为空,在这种情况下,我不想向UILabel添加空字符串,而是想将下一个UILabel向上移动(并替换为BoxAddress)。因此,因此我进行了条件检查,以了解是否应将ѭ5设置为带有特定坐标的ѭ9,如果不为5,则应将其设置为带有其他特定坐标的ѭ9。
我知道如果希望每次加载视图时都运行代码,则应使用ѭ2。但是由于某种原因,似乎只有在BoxAddress
字符串noy空时才运行viewWillAppear
。如果单击的单元格中有一个空的BoxAddress
,它将使用我单击的最后一个具有非空BoxAddress
的单元格中的BoxAddress
中的值。
我将在此处粘贴我的代码,以查看是否可以向我指出我在这里做错了什么。
// Implement loadView to create a view hierarchy programmatically,// without using a nib.
- (void)loadView {
//allocate the view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//set the view\'s background color
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidLoad
{
//add the labels
name = [[UILabel alloc] initWithFrame:CGRectMake(10.0,10.0,320.0,20.0)];
[name setBackgroundColor:[UIColor clearColor]];
[name setTextColor:[UIColor blackColor]];
street = [[UILabel alloc] initWithFrame:CGRectMake(10.0,30.0,20.0)];
[street setBackgroundColor:[UIColor clearColor]];
[street setTextColor:[UIColor blackColor]];
//if no Box address,move up the rest of the addresses
if ([[self.office BoxAddress] length] == 0) {
zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,20.0)];
[zip setBackgroundColor:[UIColor clearColor]];
[zip setTextColor:[UIColor blackColor]];
phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,20.0)];
[phone setBackgroundColor:[UIColor clearColor]];
[phone setTextColor:[UIColor blackColor]];
fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,20.0)];
[fax setBackgroundColor:[UIColor clearColor]];
[fax setTextColor:[UIColor blackColor]];
[self.view addSubview:name];
[self.view addSubview:street];
[self.view addSubview:zip];
[self.view addSubview:phone];
[self.view addSubview:fax];
} else {
Box = [[UILabel alloc] initWithFrame:CGRectMake(10.0,20.0)];
[Box setBackgroundColor:[UIColor clearColor]];
[Box setTextColor:[UIColor blackColor]];
zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,110.0,20.0)];
[fax setBackgroundColor:[UIColor clearColor]];
[fax setTextColor:[UIColor blackColor]];
[self.view addSubview:name];
[self.view addSubview:street];
[self.view addSubview:Box];
[self.view addSubview:zip];
[self.view addSubview:phone];
[self.view addSubview:fax];
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
// viewWillAppear method is run every time the view is loaded as opposite to the viewDidLoad method which only is run once
// in this program disclosureDetail view needs to be loaded for each detail view with different content each time
- (void)viewWillAppear:(BOOL)animated {
NSLog(@\"%@\",[self.office BoxAddress]);
[name setText:[self.office name]];
[street setText:[self.office streetAddress]];
if ([[self.office BoxAddress] length] > 0) {
[Box setText:[self.office BoxAddress]];
}
[zip setText:[Nsstring stringWithFormat:@\"%@ %@\",[self.office zipCode],[self.office city]]];
[phone setText:[self.office phoneNo]];
[fax setText:[self.office faxNo]];
[super viewWillAppear:animated];
}
解决方法
if ([[self.office boxAddress] length] > 0) {
[box setText:[self.office boxAddress]];
}
如果boxAddress
的长度为0,则box
将继续包含您上一次显示该视图时在其中设置的任何文本。
您不仅需要在加载视图时隐藏视图,而且还需要在每次显示视图时都显示ѭ20,因为视图可能已加载然后用于显示多个不同的ѭ22。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。