2017-09-24 65 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() { 

    } 
}