HomeiOS Developmentios - UIButton's sender.tag is all the time 0, regardless of completely...

ios – UIButton’s sender.tag is all the time 0, regardless of completely different set values


I take some UIButton on a storyboard,then i bind these Button with a IBAction operate,however the worth of sender.tag all the time 0

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    // 告诉系统等使用的时候是有值的,所以这里不需要
    var participant: AVAudioPlayer!
    let sounds = ["note1", "note2", "note3", "note4", "note5", "note6", "note7"]
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // Do any further setup after loading the view.
    }
    
    
    @IBAction func play(_ sender: UIButton) {
        print(sender.tag)
        play(tag: sender.tag)
    }
    
    // 封装函数
    func play(tag :Int) {
    
        // 确定文件有就加!
        let url = Bundle.major.url(forResource: sounds[tag], withExtension: "wav")!
        do {
            participant = strive AVAudioPlayer(contentsOf: url)
            
            participant.play()
        } catch {
            print(error)
        }
    }
}

enter picture description right here

I anticipating the worth of the sender.tag will likely be 1-8,not all the time 0

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments