簡體   English   中英

無法通過多個視圖訪問appdelegate的值

[英]Trouble accessing values from appdelegate with multiple views

我正在嘗試從作為應用程序視圖控制器的子視圖創建的視圖訪問作為AppDelegate中的屬性創建的int和float的方法(我知道這是不行的,我應該使用MVC范例,但這適用於一個類,我們這個學期沒有涵蓋)。 我可以從每個視圖中設置值,然后在同一視圖中獲取它們,但是當我想將所有值聚合到一個視圖中時,我什么也無法訪問。 在每個視圖的viewWillDisappear方法中,我具有:

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

接下來是我要設置的值: ... delegate.oldTotal = olds; ... ... delegate.oldTotal = olds; ...

然后在聚合所有值的視圖上,我有一些標簽,這些標簽的文本應該設置為存儲在delegate的值,但我得到的都是0。 我不確定這些視圖是否正在調用willAppearwillDisappear方法,因為它們被控制為:

-(IBAction)loadBeverageView:(id)sender{
    [self clearView];
    [self.view insertSubview:beverageViewController.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];
    }
}

我已經花了幾天的時間試圖使它生效,但沒有任何顯示。 誰能在這里闡明一些想法?


AppDelegate代碼和我用來訪問它的代碼


我有一直嘗試使用的整數和浮點數:

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

然后將它們全部synthesized到實現文件中。 我一直在分配這樣的值:

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

我一直在嘗試像這樣訪問它們:

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

我認為-(void) clearView不需要這么復雜。 如果您只想刪除以前設置的視圖,請嘗試以下操作:

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

請注意,我只是輸入了此內容,因此請注意輸入錯誤。

如果self.view僅包含您要添加的視圖,則這也變得更加簡單。 您可以執行以下操作,而不是使用索引0:

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

和...

-(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