未收到 UIButton TouchDown 事件

发布于 2024-09-16 00:46:46 字数 879 浏览 2 评论 0原文

我正在以编程方式为我的应用程序创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2024-09-23 00:46:46

更多上下文然后答案。搜索按钮有一个文本字段同级,两者都由容器 UIViewController 保存。该父容器是 TableViewController 的同级容器。 uivc和tvc都由一个外容器来容纳。

我的代码位于

View.AddSubview(uivc);
View.AddSubview(tvc);

最外面的容器中。

表工作正常,uivc 控件则不行。如果我颠倒顺序,两者都可以正常工作!

View.AddSubview(tvc);
View.AddSubview(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

View.AddSubview(uivc);
View.AddSubview(tvc);

in the outermost container.

Table works fine, uivc controls do not. If I reverse the order, both work fine!

View.AddSubview(tvc);
View.AddSubview(uivc);

Go figure.

若言繁花未落 2024-09-23 00:46:46

处理 TouchDown 事件的方法是使用目标操作方法。下面是一个示例:

// button action method declaration
UIButton *buttonTemp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonTemp addTarget:self action:@selector(favoriteTapped:event:) 

//method to fire when the button is touched

- (void)favoriteTapped:(id)sender event:(id)event
{
    NSLog(@"favoriteTapped");
}

简而言之,按钮声明中的第二行表示:“触摸按钮时,触发‘favoriteTapped 方法’

The way to handle a TouchDown event is to use a target-action method. Here is an example:

// button action method declaration
UIButton *buttonTemp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonTemp addTarget:self action:@selector(favoriteTapped:event:) 

//method to fire when the button is touched

- (void)favoriteTapped:(id)sender event:(id)event
{
    NSLog(@"favoriteTapped");
}

In a nutshell, the second line in the button declaration says: "When the button is touched, fire the 'favoriteTapped method'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文