简体   繁体   English

无法通过多个视图访问appdelegate的值

[英]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). 我正在尝试从作为应用程序视图控制器的子视图创建的视图访问作为AppDelegate中的属性创建的int和float的方法(我知道这是不行的,我应该使用MVC范例,但这适用于一个类,我们这个学期没有涵盖)。 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: 在每个视图的viewWillDisappear方法中,我具有:

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

Followed by the values I want to set: ... delegate.oldTotal = olds; ... 接下来是我要设置的值: ... 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. 然后在聚合所有值的视图上,我有一些标签,这些标签的文本应该设置为存储在delegate的值,但我得到的都是0。 I'm not sure if the views are calling the willAppear and willDisappear methods since they are being controlled as: 我不确定这些视图是否正在调用willAppearwillDisappear方法,因为它们被控制为:

-(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 AppDelegate代码和我用来访问它的代码


I have the integers and floats I've been trying to use as so: 我有一直尝试使用的整数和浮点数:

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

Then they are all synthesized in the implementation file. 然后将它们全部synthesized到实现文件中。 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. 我认为-(void) clearView不需要这么复杂。 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. 如果self.view仅包含您要添加的视图,则这也变得更加简单。 Rather than using index 0, you can do this: 您可以执行以下操作,而不是使用索引0:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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