2013-04-11 81 views
1

這是我的選擇器的模型類MonoTouch的Custon UIPicker獲得所選項目

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Mobile.iOS 
{ 
    public class PickerDataModel : UIPickerViewModel 
    { 
     public event EventHandler<EventArgs> ValueChanged; 

     public List<string> Items 
     { 
      get { return this._items;} 
      set { this._items = value;} 
     } 
     List<string> _items = new List<string>(); 

     public string SelectedItem 
     { 
      get { return this._items[this._selectedIndex]; } 
     } 
     protected int _selectedIndex = 0; 

     public PickerDataModel() 
     { 
     } 

     public override int GetRowsInComponent (UIPickerView picker, int component) 
     { 
      return this._items.Count; 
     } 

     public override string GetTitle (UIPickerView picker, int row, int component) 
     { 
      return this._items[row]; 
     } 

     public override int GetComponentCount (UIPickerView picker) 
     { 
      return 1; 
     } 

     public override void Selected (UIPickerView picker, int row, int component) 
     { 
      this._selectedIndex = row; 
      if (this.ValueChanged != null) 
      { 
       this.ValueChanged (this, new EventArgs()); 
      } 
     } 
    } 
} 

然後,我創建一個動作片,以顯示UIPicker這樣

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Mobile.iOS 
{ 
    public class ActionSheetChildAgePicker 
    { 
     #region -= declarations =- 

     UIActionSheet actionSheet; 
     UIButton doneButton = UIButton.FromType (UIButtonType.RoundedRect); 
     UIView owner; 
     UILabel titleLabel = new UILabel(); 

     #endregion 

     #region -= properties =- 
     public UIPickerView AgePicker 
     { 
      get { return agePicker; } 
      set { agePicker = value; } 
     } 
     //create agepicker 
     UIPickerView agePicker = new UIPickerView(); 
     /// <summary> 
     /// The title that shows up for the date picker 
     /// </summary> 
     public string Title 
     { 
      get { return titleLabel.Text; } 
      set { titleLabel.Text = value; } 
     } 

#endregion 

     #region -= constructor =- 

     /// <summary> 
     /// 
     /// </summary> 
     public ActionSheetChildAgePicker (UIView owner) 
     { 
      // save our uiview owner 
      this.owner = owner; 

      // configure the title label 
      titleLabel.BackgroundColor = UIColor.Clear; 
      titleLabel.TextColor = UIColor.LightTextColor; 
      titleLabel.Font = UIFont.BoldSystemFontOfSize (18); 

      // configure the done button 
      doneButton.SetBackgroundImage(UIImage.FromBundle ("ZurfersMovil-LoginBar-SignButton.png"), UIControlState.Normal); 
      doneButton.SetTitleColor(MonoTouch.UIKit.UIColor.White, UIControlState.Normal); 
      doneButton.SetTitle ("done", UIControlState.Normal); 
      doneButton.TouchUpInside += (s, e) => { actionSheet.DismissWithClickedButtonIndex (0, true); }; 

      // create + configure the action sheet 
      actionSheet = new UIActionSheet() { Style = UIActionSheetStyle.BlackTranslucent }; 
      actionSheet.Clicked += (s, e) => { Console.WriteLine ("Clicked on item {0}", e.ButtonIndex); }; 



      //Set picker Model and items 
      PickerDataModel agePickerDataModel = new PickerDataModel(); 
      agePickerDataModel.Items.Add("<2"); 
      agePickerDataModel.Items.Add("3"); 
      agePickerDataModel.Items.Add("4"); 
      agePickerDataModel.Items.Add("5"); 
      agePickerDataModel.Items.Add("6"); 
      agePickerDataModel.Items.Add("7"); 
      agePickerDataModel.Items.Add("8"); 
      agePickerDataModel.Items.Add("9"); 
      agePickerDataModel.Items.Add("10"); 
      agePickerDataModel.Items.Add("11"); 
      agePickerDataModel.Items.Add("12"); 
      agePickerDataModel.Items.Add("13"); 
      agePickerDataModel.Items.Add("14"); 
      agePickerDataModel.Items.Add("15"); 
      agePickerDataModel.Items.Add("16"); 
      agePickerDataModel.Items.Add("17"); 
      agePicker.Source = agePickerDataModel; 
      agePicker.UserInteractionEnabled = true; 
      agePicker.ShowSelectionIndicator = true; 


      // add our controls to the action sheet 
      //actionSheet.AddSubview (datePicker); 
      actionSheet.AddSubview(AgePicker); 
      actionSheet.AddSubview (titleLabel); 
      actionSheet.AddSubview (doneButton); 
     } 

#endregion 

     #region -= public methods =- 

     /// <summary> 
     /// Shows the action sheet picker from the view that was set as the owner. 
     /// </summary> 
     public void Show() 
     { 
      // declare vars 
      float titleBarHeight = 40; 
      SizeF doneButtonSize = new SizeF (71, 30); 
      SizeF actionSheetSize = new SizeF(owner.Frame.Width, agePicker.Frame.Height + titleBarHeight); 
      RectangleF actionSheetFrame = new RectangleF (0, owner.Frame.Height - actionSheetSize.Height 
                  , actionSheetSize.Width, actionSheetSize.Height); 

      // show the action sheet and add the controls to it 
      actionSheet.ShowInView (owner); 

      // resize the action sheet to fit our other stuff 
      actionSheet.Frame = actionSheetFrame; 

      // move our picker to be at the bottom of the actionsheet (view coords are relative to the action sheet) 
      agePicker.Frame = new RectangleF(agePicker.Frame.X, titleBarHeight, agePicker.Frame.Width, agePicker.Frame.Height); 

      // move our label to the top of the action sheet 
      titleLabel.Frame = new RectangleF (10, 4, owner.Frame.Width - 100, 35); 

      // move our button 
      doneButton.Frame = new RectangleF (actionSheetSize.Width - doneButtonSize.Width - 10, 7, doneButtonSize.Width, doneButtonSize.Height); 
     } 

     /// <summary> 
     /// Dismisses the action sheet date picker 
     /// </summary> 
     public void Hide (bool animated) 
     { 
      actionSheet.DismissWithClickedButtonIndex (0, animated); 
     } 

     #endregion 
    } 
} 

現在我的視圖控制器上我試圖讓該項目選擇像這樣,但它不起作用,因爲我沒有ValueChanged方法。

actionSheetChildAgePicker.AgePicker.ValueChanged += (s, e) => { 
       this.txtAge1Room1 = f.ToString ((s as UIPickerView).Select(1,0,true)); 
      }; 

任何幫助將不勝感激。提前致謝。

回答

0

在ActionSheetChildAgePicker中公開您的agePickerDataModel並將其公開。

我還要讓你的UIButton doneButton公衆以及在ActionSheetChildAgePicker ..

public UIButton doneButton = UIButton.FromType (UIButtonType.RoundedRect); 

註釋掉按鈕事件中ActionSheetChildAgePicker,所以我們可以在視圖控制器處理這件事......

doneButton.SetTitleColor(MonoTouch.UIKit.UIColor.White, UIControlState.Normal); 
    doneButton.SetTitle ("done", UIControlState.Normal); 
    //doneButton.TouchUpInside += (s, e) => { actionSheet.DismissWithClickedButtonIndex (0, true); }; 

然後在你的視圖控制器,你可以做這樣的事情

actionSheetChildAgePicker.doneButton.TouchUpInside += (s, e) => { 
    string sPickedAge = actionSheetChildAgePicker.agePickerDataModel.SelectedItem; 
    actionSheetChildAgePicker.Hide(true); 
        };