【发布时间】:2016-11-23 12:20:21
【问题描述】:
我是 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)
所以,“点击”日志永远不会显示
【问题讨论】:
-
请在标签中添加 swift 版本。
-
是的,对不起,这是 Swift 3
标签: ios swift uibutton swift3 selector