我有一个应用程序,从主应用程序“汉堡”菜单中选择了一个选项,我想显示一个PickerView供用户从中选择一个号码。
因为需要从该菜单访问整个应用程序,所以我在AppDelegate.cs中构建了UIPickerView(因为UINavigationController代码位于该菜单中,并且从菜单中开始)。
一切正确显示:用户选择的菜单按钮->菜单显示->用户选择“显示选择器”按钮->显示所有项目的选择器。但是,一旦显示选择器,您就无法滚动选项,我添加的“完成”按钮也不会注册点击。实际上,可以在弹出窗口的[[through上单击ViewController中来自ViewController的文本字段。
我不确定为什么这个UIPickerView是非交互式的,有人有什么想法吗?public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow(UIScreen.MainScreen.Bounds);
// instantiate the navigation controller
nav = new UINavigationController(new SplashController());
vwNav = new UIView(nav.NavigationBar.Bounds);
var pickerView = new UIPickerView(new CGRect(275f, (UIScreen.MainScreen.Bounds.Size.Height / 2) - (375f / 2), 275f, 375f));
pickerView.ShowSelectionIndicator = true;
var myPickerViewModel = new PickerViewModel(itemList);
pickerView.Model = myPickerViewModel;
myPickerViewModel.PickerChanged += (sender, e) => {
var temp = myPickerViewModel.SelectedItem;
};
// Set up toolbar
var toolbar = new UIToolbar();
toolbar.SizeToFit();
toolbar.Hidden = false;
UILabel titleLabel = new UILabel();
titleLabel.Text = "Select an Item";
titleLabel.Frame = new RectangleF(75, 13, 200, 20);
UIButton doneButton = new UIButton(UIButtonType.Custom);
doneButton.Frame = new RectangleF(40, 335, (float)200, 30);
doneButton.SetTitle("Done", UIControlState.Normal);
doneButton.TouchDown += (sender, e) =>
{
pickerView.Hidden = true;
};
toolbar.AddSubview(titleLabel);
toolbar.AddSubview(doneButton);
pickerView.AddSubview(toolbar);
pickerView.Hidden = true;
btnMenu = new UIButton(UIButtonType.Custom);
btnMenu.Frame = new CGRect(vwNav.Frame.Right - 45, 0, 45, nav.NavigationBar.Bounds.Height);
btnMenu.SetImage(imgMenu, UIControlState.Normal);
btnMenu.SetImage(imgMenu, UIControlState.Selected);
btnMenu.TouchUpInside += (object sender, EventArgs e) =>
{
UIAlertView alert = new UIAlertView();
alert.Title = "Settings";
alert.AddButton("Open PickerView");
alert.AddButton("Exit");
alert.Dismissed += delegate (object alertSender, UIButtonEventArgs args)
{
if (args.ButtonIndex == 0)
{
pickerView.Hidden = false;
}
else
return;
}
}
vwNav.AddSubviews(btnMenu, pickerView);
nav.NavigationBar.Layer.BorderWidth = 2f;
nav.NavigationBar.Layer.BorderColor = (UIColor.FromPatternImage(imgNavBar)).CGColor;
nav.NavigationBar.AddSubviews(vwNav);
// If you have defined a root view controller, set it here:
this.window.RootViewController = nav;
this.window.MakeKeyAndVisible();
return true;
}
我删除了一些不相关的代码(为不同的屏幕和其他菜单选项添加了一些nav.PushViewController(),以使其尽可能清晰。
vwNav
的帧为nav.NavigationBar.Bounds
,pickerView
的帧为new CGRect(275f, (UIScreen.MainScreen.Bounds.Size.Height / 2) - (375f / 2), 275f, 375f)
,如果将pickView
添加到vwNav
,则pickerView不在vwNav
的范围内。请记住
视图在其视图范围之外不会收到触摸事件。
这是您问题的原因
。解决方案:
我不认为您应该在UIPickerView
中构建AppDelegate.cs
,而在ViewController
中创建它。您也可以在Navigationbar
中的ViewController
中添加按钮:
UIBarButtonItem btn = new UIBarButtonItem();
btn.Image = UIImage.FromFile("Image");
btn.Clicked += (sender, e) => { System.Diagnostics.Debug.WriteLine("show picker"); };
NavigationItem.RightBarButtonItem = btn;
并且记得将选择器添加到Viewcontroller的视图。参考:uinavigationitem和add-uibarbuttonitem-to-navigation-bar-in-xamarin-ios
随时问我任何问题:)。>