未收到 UIButton TouchDown 事件
我正在以编程方式为我的应用程序创建一个 UIButton。我在网上找到的几个例子看起来很简单。按照这些示例,我可以很好地创建一个按钮,但它无法执行我为其连接的 TouchDown 事件的操作。这是父视图的 ViewDidLoad 内按钮的代码:
_searchButton = UIButton.FromType(UIButtonType.RoundedRect);
_searchButton.Frame = new RectangleF(swidth / 2 - 50f, 140f, 100f, 40f);
//_searchButton.BackgroundColor = UIColor.Clear;
_searchButton.SetTitle("Search", UIControlState.Normal);
_searchButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
_searchButton.UserInteractionEnabled = true;
_searchButton.TouchDown += delegate
{
Console.WriteLine("wicked");
};
this.View.AddSubview(_searchButton);
我唯一能想到的可能是 UserInteractionEnabled 的设置。但它们似乎默认设置为 true。 (明确地将按钮设置为 true 没有帮助。)
我在这里可能做错了什么?
我应该补充一点,当我点击按钮时,即使没有为 TouchDown 连接任何东西,该按钮也应该变成蓝色?那也不会发生。
I'm programmatically creating a UIButton for my app. The few examples I've found online seem straightforward. Following the examples, I can create a button fine but it fails to do the action for TouchDown event I have wired for it. Here is the code for the button inside the ViewDidLoad of the parent view:
_searchButton = UIButton.FromType(UIButtonType.RoundedRect);
_searchButton.Frame = new RectangleF(swidth / 2 - 50f, 140f, 100f, 40f);
//_searchButton.BackgroundColor = UIColor.Clear;
_searchButton.SetTitle("Search", UIControlState.Normal);
_searchButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
_searchButton.UserInteractionEnabled = true;
_searchButton.TouchDown += delegate
{
Console.WriteLine("wicked");
};
this.View.AddSubview(_searchButton);
The only thing I can think of possibly the setting of UserInteractionEnabled. But they seem to be set to true by default. (Explicitly setting it true for the button doesn't help.)
What could I be doing wrong here?
I should add that the button should turn blue when I click on it, even if nothing is wired for TouchDown? That doesn't happen either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更多上下文然后答案。搜索按钮有一个文本字段同级,两者都由容器 UIViewController 保存。该父容器是 TableViewController 的同级容器。 uivc和tvc都由一个外容器来容纳。
我的代码位于
最外面的容器中。
表工作正常,uivc 控件则不行。如果我颠倒顺序,两者都可以正常工作!
去算算吧。
More context then the answer. The search button has a textfield sibling, both held by a container UIViewController. That parent container is a sibling of a TableViewController. Both the uivc and the tvc are held by an outer container.
My code was
in the outermost container.
Table works fine, uivc controls do not. If I reverse the order, both work fine!
Go figure.
处理 TouchDown 事件的方法是使用目标操作方法。下面是一个示例:
简而言之,按钮声明中的第二行表示:“触摸按钮时,触发‘favoriteTapped 方法’
The way to handle a TouchDown event is to use a target-action method. Here is an example:
In a nutshell, the second line in the button declaration says: "When the button is touched, fire the 'favoriteTapped method'