【问题标题】:Values aren't updating in AppDelegateAppDelegate 中的值未更新
【发布时间】:2012-01-20 17:01:42
【问题描述】:

我需要存储整数和一些浮点数以在视图之间共享,我知道在 AppDelegate 中这样做是失礼的,但这是我正在学习的介绍性 iOS 课程,我们还没有变得非常先进,我完全意味着(没有 MVC 范式,没有 Objective-C 课程,没有核心数据,没有我们可能遇到的错误或如何处理它们等)。我的应用程序适用于餐厅的服务器,我从应用程序的 ViewController 加载了 6 个视图作为子视图,其中包含要订购的不同项目,使用“+”和“-”按钮来增加或减少订购的数量。在 AppDelegate 中,我有 int's 和 float's 用于每个项目数量和我声明为属性的小计。

每一个项目的数量发生变化,它都会调用一个方法来更新项目计数器,显示数量,并更新和显示该视图的小计。在那个方法中,我也 创建委托的实例 (?),以便我可以访问我声明的 int 和 float 属性。

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

然后在“账单”视图上,我使用它(尝试)用数量和小计填充标签:

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

但是当视图加载每个项目的所有数量标签并且税、小计和总计的标签都为 0 时。这些值似乎没有更新,我不知道为什么。我和一个上学期上过这门课的朋友谈过(我正在为此做一个定向研究,所以非常感谢 StackOverflow 上的每个人成为我的信息和想法的资源),我也在做几乎相同的事情他做了什么。

为什么delegate 中的值没有更新?

仅供参考。我对每个视图都有一个类似的更新方法,每个视图都使用Final2AppDelegate *delegate = (Final2AppDelegate *)[[UIApplication sharedApplication]delegate];。由于委托有多个实例(?),这是否与它有关?


编辑


我也尝试过使用viewWillAppearviewDidAppear 方法,但没有这样的运气。我也无法使用常规字符串更新标签。我确保我的所有连接都已建立,但仍然无法正常工作。我正在考虑为此重新创建视图,因为我必须在几天前重做其他视图才能确定,但​​我不知道这会如何影响这一点。


编辑 2


所以我在饮料视图上制作了标签,更新为 delegate.foo 的值,看起来它们确实在正确更新,我使用了我一直试图在“账单”中使用的相同语句查看以显示计数并且也可以正常工作。中间有什么事情正在发生吗?我将尝试在开胃菜视图上添加一个新标签,该标签会根据其中一种饮料的计数进行更新,以查看这是否会在视图之间共享数据。


编辑 3


这也不起作用(我不知道为什么我会这样做),因此尝试从不同的视图访问数据显然会遇到一些麻烦。因为我所有的 ViewController 每次都只是获取指向同一个实例的指针,所以我看不出这会有什么不同。我可以访问创建它们的视图中的值,但如果我从其他视图尝试,我只会得到 0。任何人有更多的见解来阐明这一点?

也许使用所有其他视图都是子视图的应用的 ViewController 而不是 AppDelegate 会有所不同吗?

此外,每次调用更新方法时,我都会创建一个指向 AppDelegate 的新指针,即每次按下加号或减号按钮之一。这会影响它吗?


应用视图控制器代码


这就是我控制视图的方式。我在想viewDid/WillAppearviewDid/WillDisappear 可能不会因为它的控制方式而被调用,但这超出了我的范围。我知道代码比它必须的要长,但在这一点上,我只是想完成这个项目并完成这个有针对性的研究! :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];
    }
}

【问题讨论】:

    标签: iphone ios ipad uiviewcontroller uiapplicationdelegate


    【解决方案1】:

    您的问题可能是您试图在 viewDidLoad 中更新您的账单视图。如果您已经加载该 viewController 或将其添加到选项卡栏控制器或其他任何内容,则不会再次调用 viewDidLoad - 当您的视图控制器完成从笔尖(或手动)加载其视图时,只会调用一次。

    要在另一个控制器呈现视图时更新视图,viewWillAppear 是使用的标准方法,但如果您按照问题中的方式交换视图,则不会调用此方法。如果您的目标只是结束这个噩梦,您可以手动调用 viewWillAppear (不确定这是否有任何副作用),或者将更新逻辑放入一个新方法中,并在每次呈现一个视图时调用它。

    您可以通过添加日志语句或在各种方法中设置断点来亲眼看到这一点。这将告诉您实际执行各种方法的时间。

    此外,您对应用程序委托的调用很好 - 您不是在创建新实例,而是每次都获取指向同一个实例的指针。

    我希望你只是在这门课程上浪费了时间,而不是任何金钱!

    【讨论】:

    • 我不需要使用NSInteger's 和NSDecimal's,对吗?
    • 不,您所拥有的一切都很好,与您的问题无关。您是否按照我的建议完成并在您尝试更新账单的方法中设置断点?您如何显示各种视图 - 它们是在选项卡视图中、导航控制器中还是您自己完成的?
    • 我试图做断点,但他们从来没有工作过,我会更多地修补它们,看看我能做什么。这些视图不在选项卡视图中,它们由工具栏控制,我将在我的问题中为您发布代码。我不确定我一直在尝试使用的方法是否有效,因为它们是受控的,从昨天开始我就一直在想。代码在上面,非常感谢你帮助我!
    • 我已经更新了答案的第二段。如果您要呈现这样的视图,则必须手动调用 update 方法。我希望这门课程没有让你放弃 iOS 开发,让你从头开始!那里有很多好的资源,iTunes U 上的 stanford 是一个很好的起点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多