Khaksar Khaksar - 1 month ago 7
iOS Question

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.

This is View from Simulator.

First View Controller: TableView Controller with Rows.
Second View Controller: TableView Controller, Detail View Controller for the selected row on the First Table View 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:

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.

Answer

You need a reference to the 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

ViewController _parent;

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

then, assuming your parent VC is contained within a 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:

TourStopsTable.Source = new TourStopsTableSource(this);
Comments