2017-09-24 7 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の値を変更するだけで、単一のビューを使用して行うことができるように、なぜ記録用と再生用の2つのビューを作成していますか? –

答えて

0

実際に、私はviewを作成した後、私の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

:作る

唯一の変更は、ボタン、自己のアクセスプロパティに計算されたプロパティにすることです。これで、録画と再生の2つのビューを別々に作成する必要はありません。 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() { 

    } 
} 
関連する問題