2013-08-21 62 views
0

我有一个方法如何不在某些情况下加载视图?

- (void)viewDidAppear:(BOOL)animated 
{ 
    [self updateViews]; 
} 

- (void) updateViews 
{ 
    NSInteger itemIndex = [[DataController sharedInstance] indexFromObjectProperty:itemUUID]; 
    if (itemIndex == NSNotFound) { 
    [self.navigationController popViewControllerAnimated:YES]; 
    } 
    NSDictionary *item = [[[DataController sharedInstance] getItems] objectAtIndex:itemIndex]; 
} 

我没有必要的情况下,加载视图的ItemIndex == NSNotFound但在调试模式下这串被称为再下串访问,并导致异常。如何停止更新视图并显示根视图控制器?

+1

在'if'块的末尾添加'return',或使用'else'-block? –

+0

@MartinR今天我太累了,看不到简单的东西..谢谢! – ShurupuS

回答

1

有两种方法可以很容易地做到这一点:

添加回报:

- (void) updateViews 
{ 
    NSInteger itemIndex = [[DataController sharedInstance] indexFromObjectProperty:itemUUID]; 
    if (itemIndex == NSNotFound) { 
     [self.navigationController popViewControllerAnimated:YES]; 
     return; // exits the method 
    } 
    NSDictionary *item = [[[DataController sharedInstance] getItems] objectAtIndex:itemIndex]; 
} 

,或者您有想要在这个方法来完成(其他的事情主要是,如果这是不是视图弹出):

- (void) updateViews 
{ 
    NSInteger itemIndex = [[DataController sharedInstance] indexFromObjectProperty:itemUUID]; 
    // nil dictionary 
    NSDictionary *item; 
    if (itemIndex == NSNotFound) { 
     [self.navigationController popViewControllerAnimated:YES]; 
    } else { 
     // setup the dictionary 
     item = [[[DataController sharedInstance] getItems] objectAtIndex:itemIndex]; 
    } 
    // continue updating 
} 
相关问题