[英]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。 我不確定這些視圖是否正在調用willAppear
和willDisappear
方法,因為它們被控制為:
-(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];
}
}
我已經花了幾天的時間試圖使它生效,但沒有任何顯示。 誰能在這里闡明一些想法?
我有一直嘗試使用的整數和浮點數:
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.