开发者

iOS build view programatically depending on object ivar

I have a pretty funny problem which involves loadView, viewDidLoad, viewWillAppearand the state of an ivar. I have a navigation based app. And from my first level view I have a table list and then I click on one of the cells it will take me to second level view (detail view). When I click on a cell I also add an "Office" object (which contains strings like streetAddressand boxAddress) to the view controller that gets pushed. I then populate the detailed view with the contents from the Office object like [box setText:[self.office boxAddress]]; (box is a UILabel). Now what I want to achieve here is that sometimes the stringValue of boxAddress is empty and in those cases I don't want to add an empty string to the UILabel, instead I want to move the next UILabel up (and take the place of the boxAddress). So therefore I've made a conditional check to see if boxAddress is empty if it is it should set up UILabels with specific coordinates and if it's no开发者_如何学Ct empty it should set up UILabels with other specific coordinates.

I understand that you should use viewWillAppear if you want the code to be run everytime the view is loaded. But for some reason it seems that viewWillAppear is only ran when the boxAddress string is noy empty. And if I click on a cell that has an empty boxAddress it will use the value from boxAddressfrom the last cell that I clicked that had a non-empty boxAddress.

I'll paste my code here to see if you can give me a pointer on what I'm doing wrong here.

// 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]];
}

If boxAddress's length is 0 then box will continue to contain whatever text you set in it the last time the view appeared.

You're going to need to hide and show box every time the view appears not just when the view is loaded because the view might be loaded and then used to display multiple different offices.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜