class UserManager {
private init() {}
static let shared = UserManager()
/// 會員名稱
var memberName: String = ""
/// 會員id
var memberId: Int = 0
}
class ViewController: UIViewController {
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
label.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
view.addSubview(label)
label.text = "fine"
}
......
func presentOptionVC() {
let vc = OptionViewController()
vc.delegate = self
present(vc, animated: true, completion: nil)
}
......
}
extension ViewController: OptionViewControllerDelegate {
func changeState(_ string: String) {
label.text = string
}
}
protocol OptionViewControllerDelegate: class {
func changeState(_ string: String)
}
class OptionViewController: UIViewController {
weak var delegate: OptionViewControllerDelegate?
lazy var changeButton: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(changeState), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
button.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
view.addSubview(button)
}
@objc func changeState() {
let stateString = "awesome"
delegate?.changeState(stateString)
}
}
class ViewController: UIViewController {
let model = VCModel()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
label.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
view.addSubview(label)
model.callApi { [weak self] (string) in
guard let self = self else { return }
self.label.text = string
}
}
}
class VCModel {
func callApi(completion: ((String)->())) {
let urlString = "某個api url 的 String"
guard let url = URL(string: urlString) else { return }
var request = URLRequest(url: url)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in
guard let self = self else { return }
if let error = error {
print(error.localizedDescription)
return
}
guard let data = data else { return }
do {
let jsonData = try JSONDecoder().decode(String.self, from: data)
DispatchQueue.main.async {
completion(jsonData)
}
}catch {
print(error.localizedDescription)
}
}
task.resume()
}
}
class OffsetToObserve: NSObject {
@objc dynamic var offset: Int = .zero
func update(_ offset: Int) {
self.offset = offset
}
}
class KVOTest {
private var observation: NSKeyValueObservation?
lazy var offsetToObserve: OffsetToObserve = {
return OffsetToObserve()
}()
func setObserveObject(on offsetValue: OffsetToObserve) {
observation = offsetValue.observe(\.offset, options: [.new]) { (object, change) in
print(change.newValue)
}
}
......
func updateValue(value: Int) {
offsetToObserve.update(value)
}
......
......
/// 停止觀察時要呼叫
func removeObserver() {
observation?.invalidate()
observation = nil
}
......
}
/// 加入觀察者
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeShown(note:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
/// 發動通知
NotificationCenter.default.post(name: Notification.Name.UIKeyboardWillShow, object: nil, userInfo: userInfo)
/// 移除觀察者
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)