2017-09-24 60 views
1

我想使用相同的UIView類來錄製和播放聲音。在這個視圖類中,我通過編程添加了一個按鈕,並且我想根據視圖的目的更改按鈕文本。當視圖作爲播放器創建時,它將顯示播放圖標(或現在的文本)。如果視圖是作爲錄像機創建的,則會顯示錄像圖標。但是我無法從UIButton的視圖中訪問全局變量。從編程的uibutton屬性訪問全局變量

class AudioPlayerView: UIView { 
    var isRecorder = false 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    ... 

} 
let theButton: UIButton = { 
    let button = UIButton() 

    if isRecorder { 
     button.setTitle("▷", for: .normal) 
     button.addTarget(self, action: #selector(playPauseAudio), for: .touchUpInside) 
    } else { 
     button.setTitle("▢", for: .normal) 
     button.addTarget(self, action: #selector(recordTapped), for: .touchUpInside) 
    } 

    button.backgroundColor = .clear 
    button.layer.cornerRadius = 15.0 
    return button 
}() 
+0

你的意思是說你無法訪問isRecorder變量? –

+0

是Syed。究竟。但是,當我將「isRecorder」放在課程減速上方時,它會工作... –

+0

爲什麼要創建兩個視圖一個用於錄製,另一個用於播放,因爲您只需更改isRecorder值就可以使用單個視圖進行操作。 –

回答

0

其實,在創建視圖後,我在viewcontroller類中做了如下操作。我只需設置按鈕子視圖標題並添加相關目標。現在它似乎在運作。但作爲一名新秀,我不確定這是否是一個好的解決方案......感謝所有分享他們意見和代碼的人。

 if cell.name == "recorder" { 
      audioPlayerView?.theButton.setTitle("▢", for: .normal) 
      audioPlayerView?.theButton.addTarget(audioPlayerView, action: #selector(audioPlayerView?.recordTapped), for: .touchUpInside) 

     } else { 
      audioPlayerView?.theButton.setTitle("▷", for: .normal) 
      audioPlayerView?.theButton.addTarget(audioPlayerView, action: #selector(audioPlayerView?.playPauseAudio), for: .touchUpInside) 
     } 
0

如果您想要爲每個AudioPlayerView設置屬性,那麼將該屬性包含在類聲明中是有意義的。

唯一的變化做出是使按鈕計算的屬性來訪問屬性自:

var theButton: UIButton { 
    let button = UIButton() 

    if isRecorder { 
     button.setTitle("▷", for: .normal) 
     button.addTarget(self, action: #selector(playPauseAudio), for: .touchUpInside) 
    } else { 
     button.setTitle("▢", for: .normal) 
     button.addTarget(self, action: #selector(recordTapped), for: .touchUpInside) 
    } 

    button.backgroundColor = .clear 
    button.layer.cornerRadius = 15.0 
    return button 
} 
1

我已經重新實現您的播放器類。現在您不需要爲錄製和播放狀態創建兩個單獨的視圖。所有你需要的是改變isRecorder值,你的按鈕標題將被改變,而不需要創建另一個視圖。

class AudioPlayerView: UIView { 
    var isRecorder = false { 
     didSet { 
      if isRecorder { 
       theButton.setTitle("▷", for: .normal) 
      } else { 
       theButton.setTitle("▢", for: .normal) 
      } 
     } 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     if isRecorder { 
      theButton.setTitle("▷", for: .normal) 
     } else { 
      theButton.setTitle("▢", for: .normal) 
     } 
     //Add your button to view as subview. 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 

    } 
    let theButton: UIButton = { 
     let button = UIButton() 
     button.backgroundColor = .clear 
     button.layer.cornerRadius = 15.0 
     button.addTarget(self, action: #selector(tapButton), for: .touchUpInside) 
     return button 
    }() 

    func tapButton() { 
     if isRecorder { 
      playPauseAudio() 
     } else { 
      recordTapped() 
     } 
    } 
    func playPauseAudio() { 

    } 
    func recordTapped() { 

    } 
}