2016-07-16 89 views
-1

我在Xcode中啓動了一個Master-Detail iOS類型的項目。主從式流量控制

我有MasterViewController和DetailViewController按照我的預期工作。

以下是我想知道如何使用一個良好的做法。

通常的行爲是,當在主表格視圖中的某個項目上點擊時,DetailViewController啓動並完成其工作。

但有些情況下,事情還沒有準備好,我不想讓DetailViewController顯示出來。 我只是不想要發生任何事情,或者我想要發生其他事情。我怎樣才能做到這一點?什麼是最好的(標準)方法呢?

在僞代碼,我想是這樣的:

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

只是做到這一點。有什麼問題? – matt

+0

您必須先嚐試一下,並在發生問題時發佈問題。 –

+0

「只要這樣做」:什麼是「那個」? 「Vladimir Nul」:感謝您對我自己的想法,我沒有自己嘗試任何事情。 如果我花時間寫一篇文章,這正是因爲我嘗試了幾件事情,並沒有奏效。當你寫「因爲我有問題」。 – Michel

回答

1

既然你開始與主從模板,您使用的是賽格瑞標識符爲"showDetail"過渡到詳細視圖控制器。 iOS爲您提供了一個鉤子,讓您決定是否在選擇該行時執行該segue。

重寫shouldPerformSegueWithIdentifier(_: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 
} 
+0

非常感謝,它的工作原理。有趣的部分是,通過重寫另一個方法,我發現了一種不同的解決方案:'func tableView(tableView:UITableView,willSelectRowAtIndexPath indexPath:NSIndexPath) - > NSIndexPath?'。如果情況不太好,我會返回零,否則返回indexPath。 – Michel

+0

有趣。你可以在你自己的問題上發表答案。這些信息可以幫助未來的程序員找到這個問題。 – vacawama

+0

是的,我有時候會這樣做。在這種特殊情況下,您的解決方案與我的解決方案一樣簡單明瞭。我不知道是否可以將其視爲最佳做法。 – Michel

1

這裏是一個可能的解決方案:

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 
    } 
}