我有一个 Xamarin.iOS 应用程序,其中包括一个导航 Controller ,其中 View Controller 中的根目录 ViewController.cs
,在 UIViewController(在单个 View 应用程序中创建的默认)内部是一个 TableView ,它与一个新的 View Controller 有一个 segue。
在下面的 Storyboard中描述。
表格 View 由 StationsTableViewSource.cs
控制。继承自 UITableViewSource
的代码. TableView 的源设置在 ViewController.cs
内。 .
StationsTable.Source = new StationsTableViewSource(StationPlayer.StationsTitleList)
当我在表格 View 中按下一个单元格时,我想继续使用新的 View Controller ,但正如我所见:
我的问题是,通过上述设置,如何从嵌套在 View Controller 中的 tableview 内的单元格执行 segue 到另一个 View Controller?
请更正术语 - Xamarin/iOS 术语不太热。
谢谢大家。
最佳答案
首先,我们需要弄清楚你创建了哪个segue?
1.从你的 tableViewCell 到一个新的 ViewController。
这意味着:点击 Cell
+ ctrl 并拖动到新的 ViewContoller。这样就不用执行PerformSegue()
了手动。当您单击单元格时,它将推送到一个新的 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);
}
I have no access to the Parent/Root/Containing View Controller from the Table View
当您构造此 Source 时,您可以传递您的“父” View Controller ,例如:
ViewController parentVC;
//You can add other parameters you want in this initial method
public MyTableViewCellSource(ViewController viewController, ...)
{
parentVC = viewController;
}
此外,两个 segues 都会触发
PrepareForSegue()
在父 View Controller 中。在此方法中,您可以将参数传递给新的 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更多细节。
关于c# - 执行从 UITableView 到新 View Controller Xamarin 的 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48867545/