2011-11-30 39 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

相关问题