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 但在调试模式下调用此字符串,然后访问下一个字符串并导致异常。如何停止更新视图并显示根视图控制器?

4

1 回答 1

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
}
于 2013-08-21T17:33:19.450 回答