c# - 执行从 UITableView 到新 View Controller Xamarin 的 segue

标签 c# ios uitableview xamarin xamarin.ios

我有一个 Xamarin.iOS 应用程序,其中包括一个导航 Controller ,其中 View Controller 中的根目录 ViewController.cs ,在 UIViewController(在单个 View 应用程序中创建的默认)内部是一个 TableView ,它与一个新的 View Controller 有一个 segue。

在下面的 Storyboard中描述。

ViewControllers

表格 View 由 StationsTableViewSource.cs 控制。继承自 UITableViewSource 的代码. TableView 的源设置在 ViewController.cs 内。 .

StationsTable.Source = new StationsTableViewSource(StationPlayer.StationsTitleList)

当我在表格 View 中按下一个单元格时,我想继续使用新的 View Controller ,但正如我所见:
  • 我无法从 TableView
  • 访问父/根/包含 View Controller
  • 我无法从 TableView 访问新 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/

    相关文章:

    c# - 使用 xml 的最佳方式是什么?

    c# - ASP.NET Core + React - 尝试访问 api 时显示 SPA

    ios - 在 UITableViewCell 中有条件地显示图像

    ios - 从 MPMediaLibrary 获取大量歌曲到 UITableView 中

    c# - C#中逻辑右移的代码是什么?

    c# - Entity Framework 3.5SP1 插入顺序错误

    ios - 从 AVPlayer 对象访问 URL?

    ios - 带有音频文件的 UNNotificationAttachment 的用例是什么?

    iphone - 为什么我的属性字符串显示得很奇怪?

    ios - UITableView 部分标题颜色行为不当