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,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
将继续包含您上一次显示该视图时在其中设置的任何文本。 您不仅需要在加载视图时隐藏视图,而且还需要在每次显示视图时都显示ѭ20,因为视图可能已加载然后用于显示多个不同的ѭ22。     

要回复问题请先登录注册