我正在用 MonoTouch 编写一个移动应用程序。我的一个 ViewControllers 是一个设置页面,它允许用户设置一些他们可以在他们的地图视图上自定义的东西。其中之一是引脚颜色。我有 12 种颜色的图钉并决定使用 UIPickerView 来呈现选择。客户希望用户在找到所需颜色时必须点击“完成”按钮。为此,我创建了一个 UIToolBar 并在其上放置了一个取消和完成按钮。工具栏直接显示在 UIPickerView 上方。我将 PickerView 和 Toolbar 放在 UIActionSheet 上,并在用户点击 PinColor 文本字段时显示 ActionSheet。
到目前为止,所有这些都有效。当用户单击 pin 颜色文本字段时,将出现操作表,并且选择器视图和工具栏可见,并且它们应该在哪里。问题是,我无法与任何一个按钮或选择器视图进行交互。就像在操作表前面有一个视图控制器,但我看不到我正在做的任何地方。有人可以看看下面的代码,如果你看到了什么,请告诉我?这只是我的视图控制器代码的一部分,因此可能缺少某些内容。但是,我的代码是完整的并且可以正常编译。
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());
}
}
}