JustLikeThat
JustLikeThat

Reputation: 363

Values aren't updating in AppDelegate

I need to store ints and some floats to share between views, I know that doing so in the AppDelegate is faux pas but this is an introductory iOS course I'm taking and we haven't gotten very advanced at all, I mean AT ALL (no MVC paradigm, no Objective-C lessons, no Core Data, no errors we might encounter or how to handle them, etc). My app is for servers in a restaurant, I have 6 views loaded as subviews from the app's ViewController with different items to order with '+' and '-' buttons to increase or decrease the quantity ordered. In the AppDelegate I have int's and float's for each item quantity and the subtotal that I declared as properties.

Every the quantity of an item changes it calls a method to update the item counter, display the quantity, and update and display the subtotal for that view. Within that method I also make an instance (?) of the delegate so I can access the int and float properties I declared.

-(void)updateAppSubtotal{
    NSString *preSubString;
    NSString *subString;

    Final2AppDelegate* delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];
    delegate.eggTotal = eggs;
    delegate.craTotal = crabs;
    delegate.capTotal = caps;
    delegate.quiTotal = quis;
    delegate.bruTotal = brus;
    delegate.pepTotal = peps;
    delegate.tarTotal = tars;
    delegate.balTotal = bals;
    delegate.appSubtotal = appSub;

    appSub = (eggs*eggPrice)+(crabs*crabPrice)+(caps*capPrice)+(quis*quiPrice)+(brus*bruPrice)+
(peps*pepPrice)+(tars*tarPrice)+(bals*balPrice);

    if(appSub < 10){
        preSubString = [[NSString alloc]initWithFormat:@"%1.2f",appSub];
    }else if(appSub < 100 && appSub >= 10){
        preSubString = [[NSString alloc]initWithFormat:@"%2.2f",appSub];
    }else if(appSub < 1000 && appSub >= 100){
        preSubString = [[NSString alloc]initWithFormat:@"%3.2f",appSub];
    }else{
        preSubString = [[NSString alloc]initWithFormat:@"%4.2f",appSub];
    }
    subString = [[NSString alloc]initWithFormat:@"$%@",preSubString];
    appSubtotal.text = subString;


    [preSubString release];
    [subString release];
}

Then on the view for the 'bill' I use this to (attempt) to populate the labels with the quantities and the subtotals:

- (void)viewDidLoad {
    
    NSString *preSubString;
    NSString *subString;
    NSString *preTaxString;
    NSString *taxString;
    NSString *preTotString;
    NSString *totString;
    
    Final2AppDelegate *delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];

    subFloat = delegate.appSubtotal + delegate.bevSubtotal + delegate.entSubtotal + delegate.sidSubtotal +
delegate.desSubtotal;

    if(subFloat < 10){
        preSubString = [[NSString alloc]initWithFormat:@"%1.2f",subFloat];
    }else if(subFloat < 100 && subFloat >= 10){
        preSubString = [[NSString alloc]initWithFormat:@"%2.2f",subFloat];
    }else if(subFloat < 1000 && subFloat >= 100){
        preSubString = [[NSString alloc]initWithFormat:@"%3.2f",subFloat];
    }else{
        preSubString = [[NSString alloc]initWithFormat:@"%4.2f",subFloat];
    }
    subString = [[NSString alloc]initWithFormat:@"$%@",preSubString];
    billSubtotal.text = subString;

    taxFloat = subFloat*taxRate;
    if(taxFloat < 10){
        preTaxString = [[NSString alloc]initWithFormat:@"%1.2f",taxFloat];
    }else if(taxFloat < 100 && taxFloat >= 10){
        preTaxString = [[NSString alloc]initWithFormat:@"%2.2f",taxFloat];
    }else if(taxFloat < 1000 && taxFloat >= 100){
        preTaxString = [[NSString alloc]initWithFormat:@"%3.2f",taxFloat];
    }else{
        preTaxString = [[NSString alloc]initWithFormat:@"%4.2f",taxFloat];
    }
    taxString = [[NSString alloc ]initWithFormat:@"$%@",preTaxString];
    tax.text = taxString;

    totalFloat = subFloat + taxFloat;
    if(totalFloat < 10){
        preTotString = [[NSString alloc]initWithFormat:@"%1.2f",totalFloat];
    }else if(totalFloat < 100 && totalFloat >= 10){
        preTotString = [[NSString alloc]initWithFormat:@"%2.2f",totalFloat];
    }else if(totalFloat < 1000 && totalFloat >= 100){
        preTotString = [[NSString alloc]initWithFormat:@"%3.2f",totalFloat];
    }else{
        preTotString = [[NSString alloc]initWithFormat:@"%4.2f",totalFloat];
    }

    totString = [[NSString alloc]initWithFormat:@"$%@",preTotString];
    total.text = totString;
    
    eggFinalQty.text = [NSString stringWithFormat:@"%d",delegate.eggTotal];
    crabFinalQty.text = [NSString stringWithFormat:@"%d",delegate.craTotal];
    capFinalQty.text = [NSString stringWithFormat:@"%d",delegate.capTotal];
    quiFinalQty.text = [NSString stringWithFormat:@"%d",delegate.quiTotal];
    bruFinalQty.text = [NSString stringWithFormat:@"%d",delegate.bruTotal];
    pepFinalQty.text = [NSString stringWithFormat:@"%d",delegate.pepTotal];
    tarFinalQty.text = [NSString stringWithFormat:@"%d",delegate.tarTotal];
    balFinalQty.text = [NSString stringWithFormat:@"%d",delegate.balTotal];
    oldFinalQty.text = [NSString stringWithFormat:@"%d",delegate.oldTotal];
    vodkaFinalQty.text = [NSString stringWithFormat:@"%d",delegate.vodTotal];
    martiniFinalQty.text = [NSString stringWithFormat:@"%d",delegate.martTotal];
    redFinalQty.text = [NSString stringWithFormat:@"%d",delegate.redTotal];
    whiteFinalQty.text = [NSString stringWithFormat:@"%d",delegate.whiTotal];
    ipaFinalQty.text = [NSString stringWithFormat:@"%d",delegate.ipaTotal];
    oliFinalQty.text = [NSString stringWithFormat:@"%d",delegate.oldTotal];
    ribFinalQty.text = [NSString stringWithFormat:@"%d",delegate.ribTotal];
    ravFinalQty.text = [NSString stringWithFormat:@"%d",delegate.ravTotal];
    coqFinalQty.text = [NSString stringWithFormat:@"%d",delegate.coqTotal];
    bufFinalQty.text = [NSString stringWithFormat:@"%d",delegate.bufTotal];
    walFinalQty.text = [NSString stringWithFormat:@"%d",delegate.walTotal];
    cubFinalQty.text = [NSString stringWithFormat:@"%d",delegate.cubTotal];
    bleFinalQty.text = [NSString stringWithFormat:@"%d",delegate.bleTotal];
    sauFinalQty.text = [NSString stringWithFormat:@"%d",delegate.sauTotal];
    cacFinalQty.text = [NSString stringWithFormat:@"%d",delegate.cacTotal];
    souFinalQty.text = [NSString stringWithFormat:@"%d",delegate.souTotal];
    pesFinalQty.text = [NSString stringWithFormat:@"%d",delegate.pesTotal];
    temFinalQty.text = [NSString stringWithFormat:@"%d",delegate.temTotal];
    salFinalQty.text = [NSString stringWithFormat:@"%d",delegate.salTotal];
    lobFinalQty.text = [NSString stringWithFormat:@"%d",delegate.lobTotal];
    dreFianlQty.text = [NSString stringWithFormat:@"%d",delegate.dreTotal];
    marFinalQty.text = [NSString stringWithFormat:@"%d",delegate.marTotal];
    cheFinalQty.text = [NSString stringWithFormat:@"%d",delegate.cheTotal];
    fonFinalQty.text = [NSString stringWithFormat:@"%d",delegate.fonTotal];
    mouFinalQty.text = [NSString stringWithFormat:@"%d",delegate.mouTotal];
    pieFinalQty.text = [NSString stringWithFormat:@"%d",delegate.pieTotal];
    hazFinalQty.text = [NSString stringWithFormat:@"%d",delegate.hazTotal];
    briFinalQty.text = [NSString stringWithFormat:@"%d",delegate.briTotal];
    choFinalQty.text = [NSString stringWithFormat:@"%d",delegate.choTotal];
    iceFinalQty.text = [NSString stringWithFormat:@"%d",delegate.iceTotal];

    [preSubString release];
    [subString release];
    [preTaxString release];
    [taxString release];
    [preTotString release];
    [totString release];

    [super viewDidLoad];
}

But when the view loads all of the quantity labels for each item and the labels for tax, subtotal and total are all 0. The values don't seem to be updating and I'm not sure why. I had spoken to a friend of mine that took this class last semester (I'm doing a directed study for it so thank you so much everyone on StackOverflow for being my resource of information and ideas) and I'm doing pretty much the same thing he did.

Why aren't the values in delegate being updated?

F.Y.I. I have a similar update method for each view and each one uses Final2AppDelegate *delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];. Would this have something to do with it since there's multiple instances (?) of the delegate?


Edit


I had tried using the viewWillAppear and viewDidAppear methods also but no such luck. I couldn't get the labels to be updated with a regular string either. I made sure all of my connections were made but something still isn't working. I'm thinking of just recreating the view for this because I had to redo the other views a couple days ago just to be sure, but I don't know how that would affect this.


Edit 2


So I made labels on the beverage view that update to the value of delegate.foo and it does seem that they are updating correctly and I used the same statements that I've been trying to get to work in the 'Bill' view to display the counts and that worked correctly too. Is there something in the middle that is happening? I'm going to try and add a new label on the appetizer view that updates with the count of one of the drinks to see if that will share data between the views.


Edit 3


Well that didn't work either (I don't know why I though it might) so there's obviously some trouble trying to access the data from different views. Since all of my ViewControllers are just getting pointers to the same instance every time I don't see how this would make a difference. I can access the values in the view that I create them in but if I try from other views I just get 0. Anyone have any more insight to shed some light on this?

Would maybe using the app's ViewController that all of the other views are subviews of instead of the AppDelegate make a difference?

Also I'm creating a new pointer to the AppDelegate every time the update method is called, which is every time one of the plus or minus buttons is pressed. Would that affect it at all?


App View Controller Code


This is how I'm controlling the views. I was thinking that the viewDid/WillAppear and viewDid/WillDisappear might not be getting called because of how it's controlled, but this is beyond me. I know the code is longer than it has to be but at this point I'm just trying to get this project done and be done with this directed study! :D

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

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

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

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

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

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

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

Upvotes: 0

Views: 225

Answers (1)

jrturton
jrturton

Reputation: 119272

Your problem is probably that you are attempting to update your bill view in viewDidLoad. If you've already loaded that viewController or added it to a tab bar controller or whatever then viewDidLoad will not be called again - this is only called once, when your view controller has finished loading its views from a nib (or manually).

To update a view when it is presented by another controller, viewWillAppear is the standard method to use, though this won't be called if you are swapping views the way you do in your question. If your aim is just to get this nightmare over with, you could just manually call viewWillAppear (not sure if that has any side effects), or put your update logic into a new method and call that each time you present one of your views.

You can see this for yourself by adding log statements or setting breakpoints in various methods. This will tell you when various methods are actually executed.

Also, your calling of the app delegate is fine - you are not creating new instances, just getting pointers to the same instance each time.

I hope you've only wasted time on this course, and not any money!

Upvotes: 2

Related Questions