Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating the form for Agora Audio Broadcasting example
class ViewController: UIView {
/*
Implementation of the form
*/
lazy var channelField: UITextField = {
let tf = UITextField()
tf.placeholder = "channel-name"
tf.borderStyle = .roundedRect
return tf
}()
lazy var usernameField: UITextField = {
let tf = UITextField()
tf.placeholder = "username"
tf.borderStyle = .roundedRect
tf.textContentType = .username
return tf
}()
let segmentItems = ["audience", "broadcaster"]
lazy var toggleRole: UISegmentedControl = {
let seg = UISegmentedControl(items: self.segmentItems)
seg.selectedSegmentIndex = 0
return seg
}()
lazy var submitButton: UIButton = {
let btn = UIButton(type: .roundedRect)
btn.setTitle("Join", for: .normal)
btn.addTarget(self, action: #selector(joinChannel), for: .touchUpInside)
return btn
}()
func placeFields() {
[self.channelField, self.usernameField, self.toggleRole, self.submitButton]
.enumerated().forEach { (idx, field) in
self.view.addSubview(field)
field.frame = CGRect(
origin: CGPoint(
x: 25,
y: Int(self.view.safeAreaInsets.top) + 50 + idx * 55),
size: CGSize(width: self.view.bounds.width - 50, height: 50)
)
field.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
}
}
@objc func joinChannel() {
let channel = self.channelField.text ?? ""
let username = self.usernameField.text ?? ""
let role: AgoraClientRole = self.toggleRole.selectedSegmentIndex == 0 ? .audience : .broadcaster
if channel.isEmpty || username.isEmpty {
// Either channel or username fields are empty.
return
}
// AgoraAudioViewController is implemented in another file
let agoraAVC = AgoraAudioViewController(
appId: ViewController.appID, token: nil, channel: channel,
username: username, role: role
)
self.present(agoraAVC, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment