ios - 如何根据 Segue 身份从 Collection View 单元格转到特定的 View Controller

标签 ios swift uiviewcontroller segue collectionview

目前正在快速开发一个小型 ios 应用程序,我已经使用 .plist 中的数据填充了一个 Collection View 单元格,每个单元格都有一个标题和按钮,我想要的是根据 Storyboard 中的多个 View Controller 按下按钮后在 segue 标识上。例如,如果 segue 将 id 设置为食物,我希望它导航到名为食物的 View ?

或者如果 segue 更容易从单元格中提取标题然后导航到与单元格具有相同标题的 View ?

我会尝试用代码来解释:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{
  if segue.identifier == "food"
  {
    //then navigate to the view controller called food
  }
  else if segue.identifier == "drink"{
  {
    //navigate to the view called drink
  }
}

最佳答案

如果食物、饮料或任何其他元素在 Storyboard中有其独特的视角,这是可以实现的。

您可以为 UICollectionView 中的每个按钮分配一个字符串,并检查集合中的哪个项目被点击,然后使用按钮字符串执行 performSegueWithIdentifier:

performSegueWithIdentifier("toComments", sender: self)

然后prepareForSegue:方法传递数据。做另一个如果

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "toFood" {
        let ExchangeViewData = segue.destinationViewController as! FoodViewController
        ExchangeViewData.foodMenuToShow = foodMenuID //This can be anything that you get your food items.
    }
}

关于ios - 如何根据 Segue 身份从 Collection View 单元格转到特定的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34004252/

相关文章:

ios - WebView Xcode 7 空白错误

iphone - 在 objective-c 中发送消息时可以转换吗?

ios - 如何保持 uicollectionview 单元格之间的间距

ios - 使用单个 ViewController 动态切换设备方向

ios - 如何在 0 处停止倒计时? - swift

arrays - TextField 作为应用程序启动时的变量粘贴到 LocalNotification 中

ios - 以编程方式在 iOS 中启动新 Activity

objective-c - 从 ObjectiveC 类引用 UIViewController

objective-c - 当键盘出现在 iOS 中时,将 UIView 上移

ios - 如何在使用 MagicalImport 导入数据后保存上下文?