具有自定义视图的UIBarButtonItem

Jör*_*örn 1 c# xamarin.ios ios

我尝试使用工具栏的自定义视图创建UIBarButtonItem.视图本身显示良好,但是当我单击BarButton时,不会执行任何操作.看起来触摸事件不会从视图转发到UIBarButtonItem实例.我检查了响应链,我认为它看起来不错.我也搜索了互联网并检查了Apple文档,但找不到任何有关我的问题的提示.

这是我的代码:

   g__objWeatherButton = new UIBarButtonItem[1];

   UIView l__objCustomView = g__objWeatherDisplay.InfoBarButton; // Returns a reference to my custom view

   UIBarButtonItem l__objButton = new UIBarButtonItem(l__objCustomView);

   l__objButton.Clicked += delegate {this.WeatherButtonEvent();}; // my action handler
   l__objButton.Width = 200;
   l__objButton.Enabled = true;

   g__objWeatherButton[0] = l__objButton;

   this.Items = g__objWeatherButton; // "this" is my UIToolbar object
Run Code Online (Sandbox Code Playgroud)

有人能给我一个暗示问题所在吗?或者一个工作代码示例(请在c#中 - 在Objective-C中找到一些示例,但显然忽略了关键技巧;-)

Dim*_*kos 6

没有特别的技巧.如果要将自定义视图添加到工具栏或导航栏,则应订阅(并响应)该视图的事件.因此,不使用UIView来保存图像,而是使用UIButtonType.Custom创建一个UIButton并订阅该按钮的TouchUpInside事件.

然后,就像使用UIView一样初始化它:

UIButton l__objCustomUIButton = UIButton.FromType(UIButtonType.Custom);
//l__objCustomUIButton.SetImage(UIImage.FromFile("your button image"), UIControlState.Normal);
l__objCustomUIButton.TouchUpInside += delegate { this.WeatherButtonEvent(); };
UIBarButtonItem l__objButton = new UIBarButtonItem(l__objCustomUIButton);
Run Code Online (Sandbox Code Playgroud)

只需确保在类范围中声明按钮即可.