swift - swift 中的选择器出现问题

标签 swift

我是 UIKit 的初学者。我尝试启动此代码,该代码必须在按下按钮时更改标签的文本。但是我得到了错误(

导入UIKit

ViewController 类:UIViewController {

var button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))

var label = UILabel(frame: CGRect(x: 100, y: 300, width: 200, height: 100))

override func viewDidLoad() {
    super.viewDidLoad()

    button.backgroundColor = UIColor.green
    button.addTarget(self, action: #selector(actionButton(label:)), for: .touchUpInside)
    self.view.addSubview(button)

    label.text = "Button is not pressed!"
    self.view.addSubview(label)


}

@objc func actionButton(label: UILabel){
    label.text = "Button is pressed!"
}

}

我的错误:

019-08-18 07:28:41.281998-0700 测试[4211:230478] -[UIButton setText:]:无法识别的选择器发送到实例 0x7fd04451f360

2019-08-18 07:28:41.288350-0700 测试[4211:230478] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIButton setText:]:无法识别的选择器发送到实例 0x7fd04451f360 '

*** 第一个抛出调用堆栈: ( 0 CoreFoundation 0x000000010a53e29b 异常预处理 + 331 1 libobjc.A.dylib 0x0000000108b94735 objc_异常_抛出 + 48 2 CoreFoundation 0x000000010a55cfa4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 UIKitCore 0x000000010cda4163 -[UIResponder doesNotRecognizeSelector:] + 287 4 核心基础 0x000000010a542fb6 ___forwarding_ + 1446 5 核心基础 0x000000010a544e88 _CF_forwarding_prep_0 + 120 6 测试 0x000000010826d871 $S7Testing14ViewControllerC12actionButton5labelySo7UILabelC_tF + 113 7 测试 0x000000010826d8cc $S7Testing14ViewControllerC12actionButton5labelySo7UILabelC_tFTo + 60 8 UIKitCore 0x000000010c9be7c3 -[UIApplication sendAction:to:from:forEvent:] + 83 9 UIKitCore 0x000000010caf6e85 -[UIControl sendAction:to:forEvent:] + 67 10 UIKitCore 0x000000010caf71a2 -[UIControl _sendActionsForEvents:withEvent:] + 450 11 UIKitCore 0x000000010caf60e6 -[UIControl TouchsEnded:withEvent:] + 583 12 UIKitCore 0x000000010d1d1334 - [UIWindow _sendTouchesForEvent:] + 2729 13 UIKitCore 0x000000010d1d2a30 - [UIWindow发送事件:] + 4080 14 UIKitCore 0x000000010c9d8e10 - [UIApplication发送事件:] + 352 15 UIKitCore 0x000000010c9110d0 dispatchPreprocessedEventFromEventQueue + 3024 16 UIKitCore 0x000000010c913cf2 __handleEventQueueInternal + 5948 17 核心基础 0x000000010a4a1b31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 核心基础 0x000000010a4a13a3 __CFRunLoopDoSources0 + 243 19 核心基础 0x000000010a49ba4f __CFRunLoopRun + 1263 20 核心基础 0x000000010a49b221 CFRunLoopRunSpecific + 625 21 图形服务 0x00000001127111dd GSEventRunModal + 62 22 UIKitCore 0x000000010c9bd115 UIApplicationMain + 140 23 测试 0x000000010826eb37 主 + 71 24 libdyld.dylib 0x000000010bab1551 开始 + 1 ) libc++abi.dylib:终止于

最佳答案

您不应将标签传递给 actionButton 函数,您可以只调用不带 () 的 actionButton,请参阅下面的代码。

import UIKit

class ViewController: UIViewController {

  var button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))

  var label = UILabel(frame: CGRect(x: 100, y: 300, width: 200, height: 100))


  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    button.backgroundColor = UIColor.green
    button.addTarget(self, action: #selector(actionButton), for: .touchUpInside)
    self.view.addSubview(button)

    label.text = "Button is not pressed!"
    self.view.addSubview(label)

  }

  @objc func actionButton(){
    label.text = "Button is pressed!"
  }


}

关于swift - swift 中的选择器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545673/

相关文章:

swift - 如何向以编程方式制作的按钮添加操作?

ios - 将 UIButton 放在 TabBar 的前面

swift - 在 swift 4 中使用 Base64 将图像转为字符串

swift - 关于函数和闭包的一个小问题

ios - swift 3 : kill the previous scene

ios - 如何在 mapbox map 中使用动画移动注释?

ios - Swift 3 numberOfRows TableView 函数错误

swift - Swift 中的抛出和重新抛出有什么区别?

ios - 登录/注销用户出现推送通知问题

ios - 快速将 UIFont 转换为 CGFont?