简体   繁体   English

如何从UITableViewRow按钮单击事件打开新视图?

[英]How to open new view from UITableViewRow button click event?

I want to open the new View Controller from my first view that is Table View Controller. 我想从第一个视图即Table View Controller打开新的View Controller。

这是来自模拟器的视图。

First View Controller: TableView Controller with Rows. First View Controller:具有行的TableView Controller。 Second View Controller: TableView Controller, Detail View Controller for the selected row on the First Table View Controller. 第二个视图控制器:TableView Controller,第一个表视图控制器上选定行的详细信息视图控制器。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Foundation;
    using UIKit;

    namespace TourStops.iOS {

        class TourStopsTableSource : UITableViewSource {
            private List<TourLib.TourStop> _stops;
            NSString _cellID = new NSString("TableCell");
FirstViewController _fvc;
            public TourStopsTableSource(FirstViewController fvc) {
                _stops = TourLib.TourSource.GetAllTourStops();
_fvc = fvc;
            }

            public override nint RowsInSection(UITableView tableview, nint section)
            {
                // tell the TableView how many rows to create
                return _stops.Count;
            }
            public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
                TourLib.TourStop currentTourStop = _stops[indexPath.Row];

                var cell = tableView.DequeueReusableCell(_cellID) as TourStopTableCell;
                if (cell == null) { cell = new TourStopTableCell(_cellID); }


                cell.UpdateCellControlsWithTourData(currentTourStop.Name, currentTourStop.Phone);

                #region SetupMapButton
                string mapUrl = String.Format("http://maps.google.com/maps?q={0}+{1}",
                                                                                    currentTourStop.Latitude,
                                                                                    currentTourStop.Longitude);
                cell.MapButton.TouchUpInside += delegate (object sender, EventArgs e)
                {
                    UIApplication.SharedApplication.OpenUrl(new NSUrl(mapUrl));
                }; 
                #endregion
                cell.CallButton.TouchUpInside += OpenDetailView;

                return cell;
            }

            private void OpenDetailView(object sender, EventArgs e) {
                 var view = new SecondDetailController();
  _parent.NavigationController.PushViewController(view, true);
            }

        }
    }

My FirstViewController Class: 我的FirstViewController类:

using Foundation;
using System;
using UIKit;

namespace TourStops.iOS
{
    public partial class FirstViewController : UIViewController
    {
        public FirstViewController (IntPtr handle) : base (handle)
        {
        }

        public FirstViewController ()
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            TourStopsTable.Source = new TourStopsTableSource(new FirstViewController ());
        }
    }
}

遵循Jason的步骤后发生异常。

You need a reference to the NavigationController. 您需要对NavigationController的引用。 There are multiple ways to do this, but one common patter is when you create your Source, pass in a reference to it's parent VC 有多种方法可以执行此操作,但是一种常见的模式是在创建Source时,将对它的引用传递给其父VC

ViewController _parent;

public TourStopsTableSource(UIViewController parent) {
  _stops = TourLib.TourSource.GetAllTourStops();
  _parent = parent;
}

then, assuming your parent VC is contained within a NavigationController, 然后,假设您的父VC包含在NavigationController中,

private void OpenDetailView(object sender, EventArgs e) {
  var view = new SomeDetailController();
  _parent.NavigationController.PushViewController(view, true);
}

Edit: 编辑:

In your amended example above, you are doing 在上面的修改示例中,您正在

TourStopsTable.Source = new TourStopsTableSource(new FirstViewController ());

instead you need to pass a reference to the ALREADY EXISTING VC: 相反,您需要传递对ALREADY EXISTING VC的引用:

TourStopsTable.Source = new TourStopsTableSource(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM