【问题标题】:Issue with a selector in swiftswift中的选择器问题
【发布时间】:2019-12-24 00:29:00
【问题描述】:

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

导入 UIKit

类视图控制器: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 核心基础 0x000000010a53e29b 异常预处理 + 331 1 libobjc.A.dylib 0x0000000108b94735 objc_exception_throw + 48 2核心基础0x000000010a55cfa4-[NSObject(NSObject)不识别选择器:]+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 touchesEnded:withEvent:] + 583 12 UIKitCore 0x000000010d1d1334-[UIWindow_sendTouchesForEvent:] + 2729 13 UIKitCore 0x000000010d1d2a30 -[UIWindow 发送事件:] + 4080 14 UIKitCore 0x000000010c9d8e10 -[UIApplication sendEvent:] + 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:终止于

【问题讨论】:

  • 对不起!在等待回答时,我打开了 UIKit 文档并自己解决。
  • actionButton(label: UILabel) 更改为actionButton(sender: UIButton)。

标签: swift


【解决方案1】:

你不应该将标签传递给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!"
  }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    相关资源
    最近更新 更多