Swift中的UIButton没有注册

Phi*_*lip 27 uibutton ios swift

我正在尝试使用Swift创建一个UIButton.它编译得很好,我可以在模拟器中看到我的按钮,但是当我点击它时,没有任何反应.这是我正在使用的代码:

let settings = UIButton()
settings.addTarget(self, action: "touchedSet:", forControlEvents: UIControlEvents.TouchUpInside)
settings.setTitle("Settings", forState: .Normal)
settings.frame = CGRectMake(0, 530, 150, 50)
scrollView.addSubview(settings)
Run Code Online (Sandbox Code Playgroud)

在同一个类中,这里是函数'touchingSet':

func touchedSet(sender: UIButton!) {
    println("You tapped the button")
}
Run Code Online (Sandbox Code Playgroud)

我正在使用模拟器,因为我没有iOS 8.0设备,这可能是问题吗?

谢谢!

Mer*_*ert 16

我认为这是一个老问题,但我昨天刚遇到了类似的问题.我的按钮在触摸时突出显示但未触发动作.

我有两个视图控制器.一个视图覆盖其他视图,按钮位于顶视图中.

例如.

  • Rootviewcontroller有后视图

  • Topviewcontroller有顶视图

如果我没有将topviewcontroler添加为rootviewcontroller的childviewcontroller,则顶视图上的按钮不会调用该操作.将topviewcontroller作为childviewcontroller添加后,它开始工作.

所以简而言之:只需尝试使用以下方法将按钮superview的视图控制器作为childviewcontroller添加到父视图viewcontroller中

func addChildViewController(_ childController: UIViewController)
Run Code Online (Sandbox Code Playgroud)

  • 我爱你,默特.这花了大约6个小时的时间试图找出答案,我终于找到了答案. (3认同)
  • 这个!我的UITapgestures和按钮代表没有工作.添加为childVC就可以了.谢谢你,先生! (2认同)

Fog*_*ter 8

选择器是必须创建的结构.

这样做......

针对Swift 4进行了更新

settingsButton.addTarget(self, action: #selector(showSettings), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)

应该这样做.

  • 这也没有解决它:(谢谢 (2认同)

小智 5

在模拟器中,有时它无法识别选择器.似乎有一个错误.我只是改变了动作名称(选择器),它起作用了.

let buttonPuzzle:UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
buttonPuzzle.backgroundColor = UIColor.greenColor()
buttonPuzzle.setTitle("Puzzle", forState: UIControlState.Normal)
buttonPuzzle.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
buttonPuzzle.tag = 22;
self.view.addSubview(buttonPuzzle)
Run Code Online (Sandbox Code Playgroud)

选择器功能在这里:

func buttonAction(sender:UIButton!)
{
    var btnsendtag:UIButton = sender
    if btnsendtag.tag == 22 {
        //println("Button tapped tag 22")
    }   
}
Run Code Online (Sandbox Code Playgroud)


ouv*_*ven 5

对于那些也被困在教程中的人:

当我跟随Apple的"开始开发iOS应用程序(Swift)"时,我遇到了同样的问题.事实证明,我在本教程中监督了这些代码行

override var intrinsicContentSize : CGSize {
    return CGSize(width: 240, height: 44)
}
Run Code Online (Sandbox Code Playgroud)

将它们添加到我的RatingControl修复了问题.


Boh*_*ych 5

当我的按钮的父视图有isUserInteractionEnabled == false. userInteraction所有子视图都将与其父视图相同。所以我刚刚添加了这一行 parentView.isUserInteractionEnabled = true,问题就消失了。希望这对某人有帮助。


Skw*_*ggs 5

对于遇到此问题的其他手动视图布局的人,您可能已经像这样定义了子视图:

let settingsButton: UIButton = {
    let button = UIButton(type: .system)
    // do some more setup
    button.addTarget(self, selector: #selector(openSettings), for: .touchUpInside)
    return button
}()
Run Code Online (Sandbox Code Playgroud)

错误在于您正在添加目标并self在它实际可用之前通过。

这可以通过两种方式修复

  1. 制作变量lazy并仍然在初始化块中添加目标:

    lazy var button: UIButton = {
        let button = UIButton(type: .system)
        // since this variable is lazy, we are guaranteed that self is available
        button.addTarget(self, selector: #selector(openSettings), for: .touchUpInside)
        return button
     }()
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在初始化父视图后添加目标:

    init() { 
        self.settingsButton = .init(type: .system)
        super.init(frame: .zero)
        self.settingsButton.addTarget(self, selector: #selector(openSettings), for: .touchUpInside)
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 你的答案就是黄金。我犯了同样的错误并调试了几个小时。这是常识,但很难理解这个问题与“自我”有关。谢谢 :) (3认同)
  • 我讨厌我来这里发表同样的评论后才看到你的评论! (2认同)
  • 就是这个。谢谢你!! (2认同)

Pen*_*g90 0

如果您使用故事板,请确保将按钮与代码中的 IBOutlet 实例变量链接起来。