【问题标题】:how create a segue from view controller to itself如何创建从视图控制器到自身的 segue
【发布时间】:2015-07-27 07:58:18
【问题描述】:

我的应用有类别,而这个类别可以有无限的子类别或视频。我的主要视图控制器有一个 uicollection vith 单元格。我需要这样,当我选择一个类别时,如果该类别有子类别,那么首先创建其他视图控制器,但如果该类别有视频,则转到其他不同的视图控制器。

我认为我的主视图控制器中的单元格需要两个 segues:

  • 其中一个具有显示视频视图控制器的目的地
  • other go it self to show all the subcategories.

我想将一个 segue 从我的 voew 控制器的单元格中拖到其自身。所以我可以推送那个特定视图控制器的“无限”实例(对于所有可能的子类别)。

但我不知道如何将 segue 从视图控制器拖到自身。当我尝试拖动第二个 segue 时,xcode 删除了我的第一个 segue。

谢谢你的朋友。

【问题讨论】:

  • 您应该在代码中创建视图控制器并在您的代码中显示/推送它,而不是创建segue。
  • 我不需要转场?当转场时,在我的 prepareforsegue 函数中?
  • 你昨天不是也问过同样的问题吗?见:stackoverflow.com/questions/31640508/…
  • 请不要问两次。我们都有一些没有答案的问题。尊重他人,你不是唯一一个要求的人。

标签: ios xcode swift


【解决方案1】:

只需从代码中创建自定义 segue。这样您就不会仅限于使用情节提要时的一种转场。

 UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
 MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
 [self prepareForSegue:segue sender:sender];
 [segue perform];

或者创建一个带有 ID 的视图控制器并推送它

UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyController"];
[self.navigationController pushViewController: myController animated:YES];

【讨论】:

  • 如何在 Swift 中做到这一点?
【解决方案2】:

这是基于@MaciekWrocław 的 Swift 答案

let destinationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationViewController")

let segue = UIStoryboardSegue(identifier: "SegueToProfileViewController",
                                      source: self,
                                      destination: anotherProfileVC,
                                      performHandler: {
                                                self.navigationController?.show(anotherProfileVC, sender: self)
        })

        self.prepare(for: segue, sender: user)
sege.perform()

【讨论】:

    【解决方案3】:

    斯威夫特 4:

    首先,在代码库中的某处的 UIViewController 上添加此类扩展:

    extension UIViewController {
        class func storyboardInstance(storyboardId: String, restorationId: String) -> UIViewController {
            let storyboard = UIStoryboard(name: storyboardId, bundle: nil)
            return storyboard.instantiateViewController(withIdentifier: restorationId)
        }
    }
    

    然后实例化 YourViewController 类的一个实例(使用上面的扩展),并从您的 navigationController 推送到它:

    let destinationVC = YourViewController.storyboardInstance(storyboardId: "YourStoryboardName", restorationId: "idYouSetInStoryboard") as! YourViewController
    self.navigationController?.pushViewController(destinationVC, animated: true)
    

    【讨论】:

    • 别忘了嵌入导航控制器
    【解决方案4】:

    您需要删除segue并添加代码以创建VC并在didSelectRowAtIndexPath中点击单元格时推送它:

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
    
        // Create your view controller and push it
        // pass data if needed
        self.navigationController.pushViewController(yourViewController, animated: YES);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多