亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

@selector()在SWIFT?

@selector()在SWIFT?

犯罪嫌疑人X 2019-06-06 11:15:54
@selector()在SWIFT?我想創造一個NSTimer在……里面Swift但我有點麻煩。NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true)test()是同一個類中的函數。我在編輯器中發現了一個錯誤:無法找到接受所提供的參數的“init”重載當我改變selector: test()到selector: nil錯誤消失。我試過:selector: test()selector: testselector: Selector(test())但什么都沒用,我在推薦信中找不到解決辦法。
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

下面是一個關于如何使用SelectorSWIFT班級:

override func viewDidLoad() {
    super.viewDidLoad()

    var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
    self.navigationItem.rightBarButtonItem = rightButton}func method() {
    // Something cool here   
}

注意,如果作為字符串傳遞的方法不起作用,它將在運行時失敗,而不是編譯時,并使應用程序崩潰。注意


查看完整回答
反對 回復 2019-06-06
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

另外,如果您的(SWIFT)類不是從Object-C類降下來的,那么在目標方法名稱字符串的末尾必須有一個冒號,并且您必須在目標方法中使用@objc屬性。

var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
@objc func method() {
    // Something cool here   
}

否則,您將在運行時得到“未識別的選擇器”錯誤。


查看完整回答
反對 回復 2019-06-06
  • 3 回答
  • 0 關注
  • 971 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號