2016-08-01 54 views
0

這是我遇到的場景。我有一個用戶名作爲其中一個字段的登錄屏幕。用戶可以使用下拉列表選擇一個用戶(通過按鈕實現 - 模擬下拉箭頭,彈出窗口控制器和表格視圖)。我有一個控制器(UserNameController),它具有獲取用戶名並將其綁定到其中的表視圖的邏輯。在tableview中選擇一行後更新textfield Xamarin.IOS

var content = this.Storyboard.InstantiateViewController("UserNameLookUp") as UserNameController; 
      UIPopoverController popover = new UIPopoverController(content); 

      //popover.SetPopoverContentSize(new SizeF(80, 80), true); 

      popover.PresentFromRect(new RectangleF(float.Parse((sender.Frame.X + 115).ToString()), 
                float.Parse((sender.Frame.Y + 180).ToString()) 
                , 80, 80), View, UIPopoverArrowDirection.Up, true); 

和在UserNameController:所述UserNameController經由具有文本字段,並使用下面的代碼的下拉按鈕ViewController.cs稱爲

public override void ViewDidLoad() 
     { 
      string[] userName = new string[10]; 

       tblVwUserName.Source = new TableSource(userName); 
      } 
     } 

和TabelSource.cs看起來像這樣:

public class TableSource : UITableViewSource 
    { 
     string[] TableItems; 
     string CellIdentifier = "TableCell"; 

     public TableSource(string[] items) 
     { 
      TableItems = items; 
     } 

     public override nint RowsInSection(UITableView tableview, nint section) 
     { 
      return TableItems.Length; 
     } 

     public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
     { 
      UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier); 
      string item = TableItems[indexPath.Row]; 

      //---- if there are no cells to reuse, create a new one 
      if (cell == null) 
      { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); } 

      cell.TextLabel.Text = item; 

      return cell; 
     } 

     public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
     { 
      // HOW TO SET BACK THE USERNAME TEXT FIELD IN VIEWCONTROLLER? AND CLOSE THE POPUP 
      tableView.DeselectRow(indexPath, true); 
     } 
    } 

現在我該如何在viewcontroller的textfield中顯示選定的用戶名並關閉彈出窗口?

謝謝! Sid

回答

0

我通常用TableViewSource和TableViewController的屬性來解決這個需求。您的來源將更改爲:

public class TableSource : UITableViewSource 
{ 
    string[] TableItems; 
    string CellIdentifier = "TableCell"; 

    public string SelectedItem {get; set;} 

    public TableSource(string[] items) 
    { 
     TableItems = items; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     return TableItems.Length; 
    } 

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier); 
     string item = TableItems[indexPath.Row]; 

     //---- if there are no cells to reuse, create a new one 
     if (cell == null) 
     { cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); } 

     cell.TextLabel.Text = item; 

     return cell; 
    } 

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
    { 
     SelectedItem = items[indexPath.Row];    

     tableView.DeselectRow(indexPath, true); 
    } 
} 

現在您需要一種方法來獲得該值。有兩種可能的方式:

1)UserNameController是類型的UITableViewController的,那將是比(content.TableView.Source as TableSource).SelectedItem

2)UserNameController不是類型的UITableViewController的,比我想補充一個屬性也返回了TableViewSource的財產。

最後要做的就是關閉Popover並查詢選定的項目。對於這個任務,我會將UserNameController包裝在UINavigationController中,然後在其中添加Cancel和Done按鈕,如下所示:

var navigationController = new UINavigationController(content); 
var popover = new UIPopoverController(navigationController); 

content.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (s, e) => 
{ 
    parentController.DismissViewController(true, null); 
}), true); 
content.NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Done, (s, e) => 
{ 
    parentController.DismissViewController(true, null); 

    var selectedItem = (content.Source as TableSource).SelectedItem; 
    }), true);