Xcode 6 - Push segue to same view controller

こ雲淡風輕ζ 提交于 2019-12-01 16:10:52

问题


I have a table view controller and tapping on a cell can trigger one of many different types of push segues based on the type of data in the cell. The identifier of the correct segue is determined in tableView:didSelectRowAtIndexPath: and then the segue is triggered with self.performSegueWithIdentifier:

For one of those segues, I'd like to push another instance of the same view controller on to the navigation stack. Is this possible without having to drag a new view controller object of the same type onto the storyboard? So far, I have only been able to wire up a segue to the same view controller if I Ctrl + Drag from the table cell to the view controller. This is not what I want.


回答1:


As suggested, I don't think a segue will work. Instead you can push a new instance of the same view controller programmatically, which will have the same effect as performing the segue. The difference is that segue delegate methods like prepareForSegue won't be called.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewController *vc = [[MyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
} 



回答2:


Complemeting answer above you can do this in Swift 3 using:

 let vc = ViewController()          
 self.navigationController?.show(vc, sender: nil)

If you are using storyboard you can use this:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! YourViewController
self.navigationController?.show(vc, sender: nil)



回答3:


  1. Create a Storyboard Reference in IB.
  2. Refer it to the view controller.
  3. Then you can create a segue from the view controller to the reference, which refers to itself.


来源:https://stackoverflow.com/questions/26532896/xcode-6-push-segue-to-same-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!