Last active
November 20, 2018 19:07
-
-
Save vialyx/a7329c7b083d1be893b60c5d1e2425df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
// Define new Notification.Name | |
extension Notification.Name { | |
static let ButtonDidTap = Notification.Name(rawValue: "com.vialyx.ButtonDidTap") | |
} | |
final class MyViewController: UIViewController { | |
weak var textField: UITextField! | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
// Add UITextField | |
let textField = UITextField() | |
textField.frame = CGRect(x: 150, y: 200, width: 200, height: 20) | |
textField.placeholder = "will become first respounder" | |
textField.textColor = .black | |
self.textField = textField | |
view.addSubview(textField) | |
// Add UIButton | |
let button = UIButton(type: .custom) | |
button.frame = CGRect(x: 0, y: 0, width: 200, height: 20) | |
button.setTitle("Tap to post notification", for: UIControl.State()) | |
button.setTitleColor(.red, for: UIControl.State()) | |
button.addTarget(self, | |
action: #selector(tap), | |
for: .touchUpInside) | |
view.addSubview(button) | |
self.view = view | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Setup observing | |
let notificationCenter = NotificationCenter.default | |
notificationCenter.addObserver(self.textField, | |
selector: #selector(becomeFirstResponder), | |
name: .ButtonDidTap, object: nil) | |
} | |
@objc private func tap() { | |
print("buttonDidTap") | |
NotificationCenter.default.post(name: .ButtonDidTap, | |
object: self) | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment