我正在MonoTouch中编写移动应用程序。我的一个ViewControllers是一个设置页面,允许用户设置他们可以在地图视图上自定义的一些内容。其中之一是Pin颜色。我有12种颜色的引脚,并决定使用UIPickerView来呈现选择。客户希望用户在找到他们想要的颜色时必须点击“完成”按钮。为此我创建了一个UIToolBar并在其上放置了取消和完成按钮。工具栏直接显示在UIPickerView上方。我将PickerView和工具栏放在UIActionSheet上,并在用户点击PinColor文本字段时显示ActionSheet。
到目前为止,所有这一切都有效。当用户单击引脚颜色文本字段时,将显示操作工作表,并且选取器视图和工具栏可见,以及它们应在何处。问题是,我无法与任何按钮或选择器视图进行交互。这就像在行动表前面有一个viewcontroller,但我无法看到我在做的任何地方。有人可以看看下面的代码,如果你看到了什么让我知道吗?这只是我的viewcontroller代码的一部分,因此可能会缺少某些内容。但是,我的代码已经完成,编译得很好。
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
tmpRect = new RectangleF(0,-220,this.View.Bounds.Width,220);
myActionSheet = new UIActionSheet(tmpRect);
myActionSheet.Style = UIActionSheetStyle.Default;
myActionSheet.DestructiveButtonIndex = 0;
myActionSheet.DismissWithClickedButtonIndex(0,true);
settingsScrollView.AddSubview(myActionSheet);
tmpRect = new RectangleF(0,-220,this.View.Bounds.Width,40);
myToolbar = new UIToolbar(tmpRect);
myToolbar.BarStyle = UIBarStyle.Black;
myToolbar.Translucent = true;
myToolbar.UserInteractionEnabled = true;
myToolbar.SizeToFit();
UIBarButtonItem btnLeft = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, PickerButtonCancelHandler);
UIBarButtonItem btnFlexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
UIBarButtonItem btnDone = new UIBarButtonItem(UIBarButtonSystemItem.Done, PickerButtonDoneHandler);
UIBarButtonItem[] items = new UIBarButtonItem[] { btnLeft, btnFlexibleSpace, btnDone };
myToolbar.SetItems(items, true);
myActionSheet.AddSubview(myToolbar);
tmpRect = new RectangleF(0,-176, this.View.Bounds.Width,200);
pvPinColor = new UIPickerView(tmpRect);
MyPicker _pinPicker = new MyPicker();
_pinPicker.Items.Add("Azure");
_pinPicker.Items.Add("Black");
_pinPicker.Items.Add("Blue");
_pinPicker.Items.Add("Chartreuse");
_pinPicker.Items.Add("Green");
_pinPicker.Items.Add("Grey");
_pinPicker.Items.Add("Orange");
_pinPicker.Items.Add("Pink");
_pinPicker.Items.Add("Red");
_pinPicker.Items.Add("Violet");
_pinPicker.Items.Add("White");
_pinPicker.Items.Add("Yellow");
pvPinColor.Source = _pinPicker;
pvPinColor.UserInteractionEnabled = true;
pvPinColor.ShowSelectionIndicator = true;
myActionSheet.AddSubview(pvPinColor);
settingsScrollView.AddSubview(txtPinColor);
txtPinColor.EditingDidBegin += delegate {
txtPinColor.ResignFirstResponder();
txtPinColor.InputView = pvPinColor;
myActionSheet.ShowInView(settingsScrollView);
myActionSheet.UserInteractionEnabled = true;
myActionSheet.BecomeFirstResponder();
settingsScrollView.BringSubviewToFront(myActionSheet);
_pinPicker.ValueChanged += (sender, e) => {
if (File.Exists (pinFilePath)) { File.Delete(pinFilePath); }
File.WriteAllText(pinFilePath, _pinPicker.SelectedItem);
txtPinColor.Text = _pinPicker.SelectedItem;
//pvPinColor.Hidden = true;
};
};
}
public class MyPicker : 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 MyPicker()
{
}
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());
}
}
}