Objective-C如何不加载在某些情况下视图?

发布时间: 2016/9/23 0:58:07
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一种方法

- (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:

有两个如何轻松地做到这一点︰

添加返回︰

- (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
}
赞助商