gpt4 book ai didi

ios - touchUpInside 事件未触发

转载 作者:搜寻专家 更新时间:2023-11-01 06:04:43 27 4
gpt4 key购买 nike

我是 IOS 编程的新手,但奇怪的是,相同的代码在不同的类中工作,所以我不知道为什么它不起作用它在 Swift 3 中

class BottomMenu : NSObject{

//Container view
var bottomView: UIView!

//Button
var buttonContents : UIButton!


init(bottomView : UIView) {
super.init()
self.bottomView = bottomView

buttonContents = UIButton(frame: getFrame(index: 0))
buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen
bottomView.addSubview(buttonContents)

buttonContents.addTarget(self, action: #selector(onClick), for: .touchUpInside)
}

func getFrame(index : CGFloat!) -> CGRect{
let screenW = UIScreen.main.bounds.width
return CGRect(x: (screenW/3) * index,
y: 0,
width: screenW/3,
height: self.bottomView.frame.size.height)
}
func onClick(sender: UIButton!){
NSLog("click")
}

}

这是这个类的实现:

let bottomMenu = BottomMenu(bottomView: bottomNavBarContainer)

因此,“点击”日志永远不会显示

最佳答案

您的代码在 Playground 中运行良好。错误必须在类的使用中。父 View 已禁用 userInteraction 等。

import UIKit
import Foundation
import PlaygroundSupport

class BottomMenu : NSObject{

//Container view
var bottomView: UIView!

//Button
var buttonContents : UIButton!


init(bottomView : UIView) {
super.init()
self.bottomView = bottomView

buttonContents = UIButton(frame: getFrame(index: 0))
buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen
bottomView.addSubview(buttonContents)

buttonContents.addTarget(self, action: #selector(onClick), for: .touchUpInside)
}

func getFrame(index : CGFloat!) -> CGRect{
let screenW = UIScreen.main.bounds.width
return CGRect(x: (screenW/3) * index,
y: 0,
width: screenW/3,
height: self.bottomView.frame.size.height)
}
func onClick(sender: UIButton!){
NSLog("click")
}

}

var container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
container.addSubview(view)
let buttonContents = BottomMenu(bottomView: view)

PlaygroundPage.current.liveView = container

关于ios - touchUpInside 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40764424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com