iOS构建视图以编程方式取决于对象ivar
我有一个非常有趣的问题,涉及loadView
,viewDidLoad
,viewWillAppear
和伊娃的状态。我有一个基于导航的应用程序。从我的第一层次看,我有一个表格列表,然后点击其中一个单元格,它会将我带到二级视图(细节视图)。当我点击一个单元格时,我还向被推入的视图控制器添加了一个“Office
”对象(其中包含像streetAddress
和boxAddress
这样的字符串)。然后,我使用Office
对象的内容填充详细视图,如[box setText:[self.office boxAddress]];
(框是UILabel)。现在我想在这里实现的是,有时boxAddress的stringValue是空的,在这种情况下,我不想向UILabel添加空字符串,而是要将下一个UILabel向上移动(并取代boxAddress)。因此,我已经做了一个有条件检查,看看boxAddress
是否为空,如果它应该设置为UILabel
s与特定的坐标,如果它不是空的,它应该设置UILabel
s与其他特定的坐标。iOS构建视图以编程方式取决于对象ivar
我知道你应该使用viewWillAppear
如果你希望每次加载视图时运行代码。但由于某种原因,viewWillAppear
似乎只在boxAddress
字符串为空时才会运行。如果我点击一个空格为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,320.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,320.0,20.0)];
[zip setBackgroundColor:[UIColor clearColor]];
[zip setTextColor:[UIColor blackColor]];
phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)];
[phone setBackgroundColor:[UIColor clearColor]];
[phone setTextColor:[UIColor blackColor]];
fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.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,50.0,320.0,20.0)];
[box setBackgroundColor:[UIColor clearColor]];
[box setTextColor:[UIColor blackColor]];
zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)];
[zip setBackgroundColor:[UIColor clearColor]];
[zip setTextColor:[UIColor blackColor]];
phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)];
[phone setBackgroundColor:[UIColor clearColor]];
[phone setTextColor:[UIColor blackColor]];
fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,110.0,320.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
将继续包含任何文本你在它设定的最后一次视图中显示。
每次出现视图时,您都需要隐藏并显示box
,而不仅仅是当视图加载时,因为视图可能已加载,然后用于显示多个不同的office
s。
但我认为viewWillAppear方法每次都显示视图时运行,因此如果'boxAddress'为空,它不应该显示?我将如何去隐藏和显示'box'?如果我隐藏它,它下面的UILabel会被放置在它“可以说”的位置吗? – 2011-06-15 14:51:26
'viewWillAppear'在你期望的时候被调用,但是没有什么会为你重新设置你的视图的内容,所以如果你不更新'box',它会继续显示上次视图出现时包含的内容。如果你隐藏'box',其他标签都不会发生,你需要自己重新定位它们。这种接口的一种常见解决方案是在表格视图中将每个字段设置为一个单元格,以便您可以调整显示哪些单元格并且它们将垂直堆叠。 – Jonah 2011-06-15 14:54:44
所以最简单的解决方案是使用tableview?一些伪代码会寻找“cellForRowAtIndexPath”?它会在“行”上使用开关,然后根据行显示不同的数据? – 2011-06-15 15:20:54