首先,我们需要弄清楚你创建了哪个segue?
1.从你的 tableViewCell 到一个新的 ViewController。
这意味着:点击Cell + ctrl 并拖动到新的 ViewContoller。这样就不需要手动执行PerformSegue()了。当您单击 Cell 时,它将推送到新的 Controller。
2.从你的 ViewContoller 到一个新的 ViewController
这意味着:点击 ViewContoller + ctrl 的底部栏并拖动到新的 ViewController。这样,我们需要点击我们上面创建的segue然后设置Identifier。当我们点击 Cell 时,会触发下面的事件:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
//use this method we can push to a new viewController
//the first parameter is the identifier we set above
parentVC.PerformSegue("NewPush", indexPath);
}
我无权访问父/根/包含视图控制器
表格视图
当你构造这个 Source 时,你可以像这样传递你的“父”视图控制器:
ViewController parentVC;
//You can add other parameters you want in this initial method
public MyTableViewCellSource(ViewController viewController, ...)
{
parentVC = viewController;
}
此外,两个 segues 都会在父 ViewController 中触发 PrepareForSegue()。在这种方法中,您可以将参数传递给新的 ViewController:
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
if (segue.Identifier == "NewPush")
{
SecondVC secondVC = segue.DestinationViewController as SecondVC;
...//do some configuration
}
}
关于如何使用segue,你可以阅读this official documentation了解更多详情。