我一直在研究一個有我陷害(仍然)的bug的tabbar應用程序。查看不在TabBar應用程序中更新
當我進入新選項卡時,存儲在應用模型部分的數據不被視圖使用,從而顯示過時的信息(圖形,數字等)。我試過兩種強制更新的方法,在視圖中由[self updateDisplay]
處理。 (1)在應用程序委託中,使用UITabBarControllerDelegate
方法確定選擇哪個選項卡,然後向其發送更新消息。下面是代表方法的一部分:
if (viewController == [tabBarController.viewControllers objectAtIndex:1]) {
NSLog(@"graphViewController selected, graphViewController = %@", graphViewController);
[graphViewController updateDisplay];
}
這是我想要完成這項工作的方式。但是,顯示不會更新,但NSLog確認updateDisplay已被觸發。
(2)按下視圖中的按鈕,通過執行[self updateDisplay]
來更新顯示。這工作,但不令人滿意。有趣的來了。在下面的日誌中,我們讀取(1)調用委託方法,(2)選擇graphViewController
,以及(3)調用graphViewController的updateDisplay方法。大!但barGraph
此時爲空,並且包含從浮點數ourLog
中讀取的標籤的顯示不會更新。對象ourLog
是模型的一部分,它與「磁盤」上的文件同步。 (barGraph
通過updateDisplay
從ourLog
更新)。我們現在在GraphViewController託管的視圖中,我們按下「測試」按鈕(6),調用updateDisplay
,視圖被正確更新。
請注意,在1--5中,GraphViewController
位於0x4e346e0,而在6-9位於0x4b53c80。如果你來回切換,按下「測試」按鈕,你總是得到十六進制數字。起初我以爲不知何故,有兩個漂浮在周圍的實例。但是我忽略了該類的init
方法以記錄它。它只顯示一次。所以我完全被難住了。
- 吉姆
- 1]稱爲UITabBarControllerDelegate方法,自我=
- 2] graphViewController選擇graphViewController =
- 3] GraphViewController,updateDisplay叫,自我=
- 4] ourLog:
- 5] updateDisplay,barGraph:(null)
- 6] test push
- 7] GraphViewController,updateDisplay叫,自我=
- 8] ourLog:
- 9] updateDisplay,條形圖:>
PS。我最初發布在UIVIew subclass object becomes mysteriously nil in tabbar app。我將此重新發布,因爲問題更一般,barGraph
在應用生命週期中的各個點都爲空。不知何故,當我稱之爲需要訪問的東西不是,所以我必須找出一種不同的方式來稱呼它。但它並沒有消失(我也覆蓋了dealloc
,並沒有發現barGraph
曾經發布)。
附錄1:BarGraph。
這裏是條形圖是如何在GraphViewController創建:
@interface GraphViewController : UIViewController {
BarGraph *barGraph;
}
@property (nonatomic, retain) BarGraph *barGraph;
@property (nonatomic, retain) IBOutlet UIButton *testButton;
- (void) updateDisplay;
- (IBAction) testAction: (id) sender;
@end
在@implementation
,viewDidLoad
ALLOC-inits光柱:
- (void) viewDidLoad {
[super viewDidLoad];
NSLog(@"xx:BARGRAPH: alloc-init in viewDidLoad, GraphBar");
barGraph = [[BarGraph alloc] initWithFrame:CGRectMake(15, 140, 289, 130)];
[self.view addSubview:barGraph];
[self updateDisplay];
}
最後,這裏是updateDisplay
實現:
- (void) updateDisplay {
// Check to see where GraphViewController is:
NSLog(@"GraphViewController, updateDisplay called, self = %@", self);
// Get a pointer to the current data to graph
Log *ourLog = [self theDelegate].currentLog
// Log info
NSLog(@"ourLog: %@", ourLog);
NSLog(@"updateDisplay, barGraph: %@", self.barGraph);
// Get the current data out of ourLOg
self.barGraph.data = ourLog.log;
// And ask barGraph to display it
[barGraph setNeedsDisplay];
}
附錄2:viewWillAppear。
我加viewWillAppear
到GraphViewController.m
如下:
這似乎是正常的,最好的解決方法,但這viewWillAppear
永遠不會觸發。注意該方法testAction
,其由按鈕激活:
- (IBAction) testAction: (id) sender {
NSLog(@"test pushed");
[self updateDisplay];
}
推它連接到正確地更新顯示的按鈕。但我當然想讓iOS按下按鈕,而不是我:-)
嗨,我正在GraphViewController中編程創建barGraph的一個實例(barGraph是我在其他應用中使用的自定義視圖)。我已經在附錄1中公佈了一些相關的代碼。我也嘗試過使用viewWillAppear,但似乎永遠不會被調用。我會再看一遍。謝謝! – 2011-01-29 18:09:15