简体   繁体   中英

Trouble accessing values from appdelegate with multiple views

I am trying to access int's and float's that I created as properties in my AppDelegate from views that are created as subviews of the apps view controller (I know it's a no-no and I should use MVC paradigm but this is for a class and we didn't cover that this semester). I can set the values from each view and then get them in the same view, but when I want to aggregate all the values into one view I can't access anything. In the viewWillDisappear method of each view I have:

Final2AppDelegate* delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];

Followed by the values I want to set: ... delegate.oldTotal = olds; ... ... delegate.oldTotal = olds; ...

Then on the view that aggregates all the values I have labels whose text is supposed to be set to the values stored in delegate but all I get are 0's. I'm not sure if the views are calling the willAppear and willDisappear methods since they are being controlled as:

-(IBAction)loadBeverageView:(id)sender{
    [self clearView];
    [self.view insertSubview:beverageViewController.view atIndex:0];
}

and

-(void)clearView{
    if(beverageViewController.view.superview){
        [beverageViewController.view removeFromSuperview];
    }else if(appetizerViewController.view.superview){
        [appetizerViewController.view removeFromSuperview];
    }else if(entreeViewController.view.superview){
        [entreeViewController.view removeFromSuperview];
    }else if(sideViewController.view.superview){
        [sideViewController.view removeFromSuperview];
    }else if(dessertViewController.view.superview){
        [dessertViewController.view removeFromSuperview];
    }else{
        [billViewController.view removeFromSuperview];
    }
}

I have spent days just trying to get this to work and have nothing to show. Can anyone shed some light here?


AppDelegate code and code I'm using to access it


I have the integers and floats I've been trying to use as so:

int eggTotal; and @property(nonatomic,assign)int eggTotal;

Then they are all synthesized in the implementation file. I've been assigning the values like this:

Final2AppDelegate* delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];
delegate.oldTotal = olds;

and I've been trying to access them like this:

eggFinalQty.text = [NSString stringWithFormat:@"%d",delegate.eggTotal];

I don't think -(void) clearView needs to be that complicated. If you simply want to remove the view you have previously set, try this:

-(void) clearView {
    if ([[self.view subviews] count] > 0) {
        [[[self.view subviews] objectAtIndex:0] removeFromSuperview];
    }
}

Note I just typed this in, so watch out for typos.

This also becomes much simpler if self.view only contains the view you are adding. Rather than using index 0, you can do this:

-(IBAction)loadBeverageView:(id)sender{
    [self clearView];
    [self.view addSubview:beverageViewController.view];
}

and ....

-(void) clearView {
    if ([[self.view subviews] count] > 0) {
        [[[self.view subviews] lastObject] removeFromSuperview];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM