네비게이션 컨트롤러를 사용하는 것은 다양한 함수들을 사용하진 않지만 두개의 뷰컨트롤러들의 관계를 이해하고 서로 상호작용하기위해 불리언을 사용해 활용하는 것을 배움. 기본적인 어플에 네비게이션 컨트롤러는 거의 필수적으로 들어가는 사항이라 좀더 익힐 필요가 있다.
import UIKit
class ViewController: UIViewController, EditDelegate {
let imgOn = UIImage(named: "lamp-on.png")
let imgOff = UIImage(named: "lamp-off.png")
var isOn = true
@IBOutlet var txMessage: UITextField!
@IBOutlet var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgView.image = imgOn
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let editViewController = segue.destination as! EditViewController
if segue.identifier == "editButton" {
editViewController.textWayValue = "segue : use button"
} else if segue.identifier == "editBarButton" {
editViewController.textWayValue = "segue : use Bar Button"
}
editViewController.textMessage = txMessage.text!
editViewController.isOn = isOn
editViewController.delegate = self
}
func didMessageEditDone(_ controller: EditViewController, message: String) {
txMessage.text = message
}
func didImageOnOffDone(_ controller: EditViewController, isOn: Bool) {
if isOn {
imgView.image = imgOn
self.isOn = true
} else {
imgView.image = imgOff
self.isOn = false
}
}
}
import UIKit
protocol EditDelegate {
func didMessageEditDone( controller: EditViewController, message: String)
func didImageOnOffDone( controller: EditViewController, isOn: Bool)
}
class EditViewController: UIViewController {
var textWayValue: String = ""
var textMessage: String = ""
var delegate: EditDelegate?
var isOn = false
@IBOutlet var lblWay: UILabel!
@IBOutlet var txMessage: UITextField!
@IBOutlet var swIsOn: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
lblWay.text = textWayValue
txMessage.text = textMessage
swIsOn.isOn = isOn
}
@IBAction func btnDone(_ sender: UIButton) {
if delegate != nil {
delegate?.didMessageEditDone(self, message: txMessage.text!)
delegate?.didImageOnOffDone(self, isOn: isOn)
}
_ = navigationController?.popViewController(animated: true)
}
@IBAction func swImageOnOff(_ sender: UISwitch) {
if sender.isOn {
isOn = true
} else {
isOn = false
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}