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

时间:2016-10-15 18:50:45

标签: ios uitableview xamarin xamarin.ios

我想从我的第一个视图是Table View Controller打开新的View Controller。

This is View from Simulator.

第一个视图控制器:带行的TableView控制器。 第二个视图控制器:TableView控制器,第一个表视图控制器上所选行的详细信息视图控制器。

    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);
            }

        }
    }

我的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 ());
        }
    }
}

Exception after following the steps of Jason.

1 个答案:

答案 0 :(得分:1)

您需要对NavigationController的引用。有多种方法可以做到这一点,但一个常见的模式是当你创建你的Source时,传入对它的父VC的引用

ViewController _parent;

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

然后,假设您的父VC包含在NavigationController中,

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

编辑:

在上面的修改示例中,您正在进行

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

而是需要传递对ALREADY EXISTING VC的引用:

TourStopsTable.Source = new TourStopsTableSource(this);