2011-11-30 54 views
0

我試圖把一個OpenGL的UIView我的導航控制器這樣的UINavigationController,推動一個OpenGL的UIView =永無止境的循環

GraphViewController *gvc = [[GraphViewController alloc] initWithTicker:[listOfItems objectAtIndex:indexPath.row]]; 
[self.navigationController pushViewController:gvc animated:YES]; 
[gvc release]; 

的initWithTicker方法看起來像這樣

-(id) initWithTicker:(NSString*)ticker{ 
self = [super initWithNibName:nil bundle:nil]; 
if (self) { 
    self.title = ticker; 
    EAGLView *eagl = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    eagl.animationInterval = 1.0/60.0; 
    [eagl startAnimation]; 
    self.view = eagl; 
} 
return self; 

}

當我回到我的UINavigationController中時,drawView方法(在EAGLView中)保持循環。此外,如果我再次pushViewController,第一個不會停止並創建一個新的!我試過讓這個實例變量,所以只有一個被創建,它具有相同的效果。

-(id) initWithTicker:(NSString*)ticker{ 
    self = [super initWithNibName:nil bundle:nil]; 
    if (self) { 
     self.title = ticker; 
    } 
    return self; 
} 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 

- (void)loadView { 
    eagl = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    self.view = eagl; 
} 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    eagl.animationInterval = 1.0/60.0; 
    [eagl startAnimation]; 
    [super viewDidLoad]; 

} 

相同的行爲:如果任何人有洞察力,爲什麼發生這種情況

塞爾吉奧建議,我將不勝感激。

---這是我固定我的drawView函數problem--循環

-(void)viewDidAppear:(BOOL)animated { 
    [eagl startAnimation]; 
    [super viewDidAppear:animated]; 
} 

-(void)viewDidDisappear:(BOOL)animated { 
    [eagl stopAnimation]; 
    [super viewDidDisappear:animated]; 

} 

--Craigs解決方案 -

if(graphView == nil){ 
     graphView = [[GraphViewController alloc] initWithTicker:[listOfItems objectAtIndex:indexPath.row]]; 
    }else{ 
     [graphView release]; 
     graphView = [[GraphViewController alloc] initWithTicker:[listOfItems objectAtIndex:indexPath.row]]; 
    } 

回答

1

你創建一個新的GraphViewController要推一個到您的導航堆棧的每一次?如果是這樣,那麼處理創建實例變量的方式並不重要,因爲無論如何您都不會再與該視圖控制器進行交互。

例如:

  1. 用戶水龍頭的東西,一個新的GraphViewController被壓入堆棧
  2. 用戶回到過去,這個視圖控制器將繼續運行
  3. 返回1,重複上(從而創建一個第二個GraphViewController,然後第三個,然後第四個......等等)

你應該做的事情是維護你的GraphViewController作爲一個實例變量,並且只創建一次。這將確保您只能創建一個EAGLView

if (_graphViewController == nil) { 
    _graphViewController = [[GraphViewController alloc] initWithTicker:[listOfItems objectAtIndex:indexPath.row]]; 
} 
[self.navigationController pushViewController:_graphViewController animated:YES]; 

然後,一定要release視圖控制器在dealloc方法,如果你將要維護其作爲伊娃。

+0

問題是我需要一個graphViewController用於每個ticker字符串,這可能是20,所以一旦它被定義,它就變成!= nil,因此每次我將它推入堆棧時都會使用相同的graphViewController。但是,我可能能夠將這些graphViewController存儲在一個NSMutableDictionary中,並以ticker作爲關鍵字。讓我試試並回報。非常感謝! :) – Submerged

+0

沒有你的應用程序的細節,我們只能推測,但它聽起來像你需要重新設計一些東西 - 要麼爲ticker字符串添加一個setter(如果你一次只顯示一個GraphViewController),或者如你所說,以股票作爲關鍵字保存字典。 –

+0

感謝您的快速響應craig。我願意辭職 - 讓我簡單地瞭解我想要做的事情。我有一個導航視圖控制器,它有一個帶有表格的RootViewController。該表包含多個股票字符串。現在讓我們說RIMM,APPL,GOOG。當按下某個按鈕時,它會使用initWithTicker方法加載GraphViewController,並將Nibless openGL視圖(EAGLVIEW)與該控制器相關聯。在那個OpenGL視圖裏面是一個drawView方法,它將圖形繪製到屏幕上。如你所知,這個循環從未停止。 – Submerged

1

你會嘗試執行你的驗證碼

EAGLView *eagl = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
eagl.animationInterval = 1.0/60.0; 
[eagl startAnimation]; 
self.view = eagl; 

裏面的loadView?我不確定你的觀點爲什麼像你說的那樣行事,但這是你應該建立你的用戶界面的地方...所以它可能會有所作爲...

此外,我只會撥打[eagl startAnimation];viewDidLoad ...

希望它可以幫助...

+0

查看我的編輯。相同的行爲不幸的是,視圖控制器只是保持堆疊並最終導致程序崩潰 – Submerged

+0

如何定義'startAnimation'?如果你評論它會發生什麼? – sergio