swift按钮点击后出错
代码没提示错误,点击按钮后程序崩溃,但不知道问题出在哪里,求大神解答。importUIKitclassViewController:UIViewController{ov...
代码没提示错误,点击按钮后程序崩溃,但不知道问题出在哪里,求大神解答。
import UIKit
class ViewController: UIViewController {
override func
viewDidLoad() {
super.viewDidLoad()
var playScore:UILabel
var buy:UIButton
var score:Int
= 1000
playScore = UILabel()
playScore.text = String(score)
playScore.frame = CGRectMake(100,100,100,20)
self.view.addSubview(playScore)
buy = UIButton()
buy.frame = CGRectMake(100,150,100,20)
buy.backgroundColor = UIColor.blackColor()
buy.setTitle("buyAdd", forState: UIControlState.Normal)
buy.addTarget(self, action: Selector.convertFromExtendedGraphemeClusterLiteral("buyAdd"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buy)
func buyAdd(){
score = score +
100
playScore.text = String(score)
}
// Do any
additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//
Dispose of any resources that can be recreated.
}
} 展开
import UIKit
class ViewController: UIViewController {
override func
viewDidLoad() {
super.viewDidLoad()
var playScore:UILabel
var buy:UIButton
var score:Int
= 1000
playScore = UILabel()
playScore.text = String(score)
playScore.frame = CGRectMake(100,100,100,20)
self.view.addSubview(playScore)
buy = UIButton()
buy.frame = CGRectMake(100,150,100,20)
buy.backgroundColor = UIColor.blackColor()
buy.setTitle("buyAdd", forState: UIControlState.Normal)
buy.addTarget(self, action: Selector.convertFromExtendedGraphemeClusterLiteral("buyAdd"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(buy)
func buyAdd(){
score = score +
100
playScore.text = String(score)
}
// Do any
additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//
Dispose of any resources that can be recreated.
}
} 展开
1个回答
展开全部
我就说一点,你仔细看看func buyAdd()方法是由self调用,你写到viewDidLoad里肯定运行时viewController是找不到该方法的,肯定要报错的。
追问
方法在下方申明了啊,求标准答案
追答
buy.addTarget(self, action: Selector.convertFromExtendedGraphemeClusterLiteral("buyAdd"), forControlEvents: UIControlEvents.TouchUpInside)
buyAdd这个方法是由self来调用的,而你把该方法的定义写到viewDidLoad里,就成了作用域只在viewDidLoad里生效,所以运行时,self要调用buyAdd是找不到该方法的,不知道你懂没。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询