2016-10-15 77 views
0

我想從我的第一個視圖即表視圖控制器打開新的視圖控制器。如何從UITableViewRow按鈕點擊事件打開新視圖?

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

您需要的NavigationController參考。有多種方法可以做到這一點,但一個共同的行話是,當你創建你的源代碼,傳遞一個引用到它的父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()); 

而不是你需要傳遞一個參考到已有的VC:

TourStopsTable.Source = new TourStopsTableSource(this); 
+0

感謝您的幫助。 – Khaksar

+0

但這並沒有爲我工作。 我做到了,你說的方法相同,但它給了異常,異常的詳細信息如下: System.NullReferenceException:在Navigation.Application.Main(系統未將對象引用設置到對象 的一個實例。 String []參數)[0x00008]在/Users/Admin/Projects/TourStops/TourStops/Main.cs: 我更新我的問題,你告訴我的步驟。 謝謝, – Khaksar

+0

如果您有任何問題,請您提供一個可用的示例! – Khaksar