在某些情况下如何加载视图?

时间:2013-08-21 17:02:34

标签: objective-c uiviewcontroller uinavigationcontroller

我有方法

- (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];
}

我不需要在caseIndex == NSNotFound的情况下加载视图,但在调试模式下调用此字符串,然后下一个字符串正在访问并导致异常。如何停止更新视图并显示根视图控制器?

1 个答案:

答案 0 :(得分: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
}