I add a button on the view and bind event use addTarget , to call self.testp , but while I'm run it , an Error occured:
2015-06-19 23:08:29.237 UI[16978:1700826] -[UI.ViewController testp:]: unrecognized selector sent to instance 0x7864d4a0
2015-06-19 23:08:29.240 UI[16978:1700826] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UI.ViewController testp:]: unrecognized selector sent to instance 0x7864d4a0'
The CODE is :
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var btn:UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
btn.frame = CGRectMake(10, 150, 100, 30)
btn.setTitle("button", forState: UIControlState.Normal)
//!!!!!!DID NOT WORK
btn.addTarget(self, action: Selector("testp:"), forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn)
func testp(){
println("tttt")
}
}
}
WHY?!!
Just remove :
from your selector and your code will be:
btn.addTarget(self, action: Selector("testp"), forControlEvents: UIControlEvents.TouchUpInside)
And put your function outside viewDidLoad
method but in ViewController
class.
You can use "testp:"
if your function have argument like shown below:
func testp(yourArgument: String){
println("tttt")
}
final , I figutit out
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var btn:UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
btn.frame = CGRectMake(10, 150, 100, 30)
btn.setTitle("button", forState: UIControlState.Normal)
//!!!!!!remove ":" for testp
//btn.addTarget(self, action: Selector("testp:"), forControlEvents: UIControlEvents.TouchUpInside);
btn.addTarget(self, action: Selector("testp"), forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn)
//I should put testp out of viewDidLoad
//I should put testup UIViewController
//func testp(){
// println("tttt")
//}
}
// put testp here it's work
func testp(){
println("tttt")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.