Ios 主细节流控制

Ios 主细节流控制,ios,swift,master-detail,Ios,Swift,Master Detail,我在Xcode中启动了一个主细节iOS类型的项目 我让MasterViewController和DetailViewController按预期工作 下面是我想知道如何使用良好的实践 通常的行为是,在主表视图中点击某个项时,DetailViewController启动并执行其工作 但是有些情况下事情还没有准备好,我不希望DetailViewController出现。 我只是不希望任何事情发生,或者我希望其他事情发生。我该怎么做?最好的(标准的)方法是什么 在伪代码中,我希望类似于: if situ

我在Xcode中启动了一个主细节iOS类型的项目

我让MasterViewController和DetailViewController按预期工作

下面是我想知道如何使用良好的实践

通常的行为是,在主表视图中点击某个项时,DetailViewController启动并执行其工作

但是有些情况下事情还没有准备好,我不希望DetailViewController出现。 我只是不希望任何事情发生,或者我希望其他事情发生。我该怎么做?最好的(标准的)方法是什么

在伪代码中,我希望类似于:

if situation-is-not-good { 
    do-some-other-things
} else {
    Start-DetailViewController-Normally
}

由于您从主详细信息模板开始,因此正在使用标识符为“showDetail”的segue转换到详细信息视图控制器。iOS提供了一个钩子,让您在选择行时插入是否应执行该序列的决策

重写
shouldPerformSegueWithIdentifier(u:sender:)
并将逻辑放在其中。如果要继续执行该步骤,请返回
true
;如果要跳过该步骤,请返回
false

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    if identifier == "showDetail" {
        if situation-is-not-good { 
            // do-some-other-things

            // if you don't let the segue proceed, then the cell remains
            // selected, so you have to turn off the selection yourself
            if let cell = sender as? UITableViewCell {
                cell.selected = false
            }

            return false  // tell iOS not to perform the segue
        }
    }

    return true  // tell iOS to perform the segue
}

以下是一种可能的解决方案:

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    let theCell = self.tableView.cellForRowAtIndexPath(indexPath)
    if situation-is-not-good for theCell {
        // Do-Whatever-Is-Needed
        return nil
    } else {
        return indexPath
    }
}

就这么做吧。有什么问题吗?你必须先自己尝试一下,当你对某事有问题时,在这里发布一个问题。“就这么做”:什么是“那”?致“弗拉基米尔·努尔”:感谢你自己认为我没有亲自尝试任何事情。如果我花时间写一篇文章,这正是因为我尝试了几件事,但都没有成功。当你写“因为我有问题”的时候,非常感谢,它很有效。有趣的是,通过重写另一个方法,我自己发现了一个不同的解决方案:
func tableView(tableView:UITableView,willSelectRowAtIndexPath-indepath:nsindepath)->nsindepath?
。当情况不好时,我返回0,否则返回0。有趣。你可以发布自己问题的答案。这些信息可以帮助未来的程序员找到这个问题。是的,我有时会这样做。在这种情况下,你的解决方案和我的一样简单明了。我不知道这是否是最佳实践。我只是添加了我的解决方案。