2013-08-21 70 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 
} 
相關問題