๋ฉ์ธ ํ๋ฉด -- (present) -> ๋ก๊ทธ์ธ๋ทฐ
๋ก๊ทธ์ธ๋ทฐ -- (push) --> ํ์๊ฐ์ ๋ทฐ
๋ก๊ทธ์ธ ๋ทฐ์์ ๋ก๊ทธ์ธ -- (dismiss) --> ๋ฉ์ธ์ ๋ฐ์ดํฐ ์ ๋ฌ
present๋ก ๋ชจ๋ฌ์ ๋์ฐ๋ ๋ฐฉ์์, ๋ณดํต ์ฑ UI๋ฅผ ์ค๊ณํ ๋, ํ์ฌ ๋ณด๊ณ ์๋ ๋ด์ฉ์์ ์๋ก์ด ๋ด์ฉ์ ๊น์ด๊ฐ ๊น์ง ์์ ๋ฐฉ์์ผ๋ก ๋ณด์ฌ์ค ๋ ์ฌ์ฉํ๋ ๋ฐฉ์์ ๋๋ค.
๋์ค์ ์ฑ์ ์ค๊ณํ ๋, modal๋ก ์ฒ๋ฆฌํ ์ง push ๋ก ์ฒ๋ฆฌํ ์ง๋ ์ฌ์ฉ์ ์ ์ฅ์์๋ ์ค์ํ ์์์ด๊ธฐ ๋๋ฌธ์ ์ฌ๋ฌ ์ฑ์ ๋ณด๋ฉด์ ์๊ฐ์ ํด๋ณด๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ ๋๋ค!!
(์ ์ฒด์ฝ๋)
@IBAction func loginButtonClicked(_ sender: Any) {
guard let loginVC = self.storyboard?.instantiateViewController(identifier: "loginNavi") else {return}
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: true, completion: nil)
}
guard let loginVC = self.storyboard?.instantiateViewController(identifier: "loginNavi") else {return}
ํ์ฌ ๊ฐ์ ์คํ ๋ฆฌ๋ณด๋๋ฅผ ์ฌ์ฉํ๋ ๋ทฐ์ปจํธ๋กค๋ฌ์ค์์ "loginNavi"๋ฅผ ์ฌ์ฉํ๋ ์คํ ๋ฆฌ๋ณด๋๋ฅผ ์ ์ธํ๋ค!!
ํ์ฌ ์คํ ๋ฆฌ ๋ณด๋ ๊ตฌ์ฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ๋ฉ์ธ์์ ๋ก๊ทธ์ธํ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด
์ด๋ ๊ฒ loginNavi ์คํ ๋ฆฌ๋ณด๋ ID๋ฅผ ๊ฐ์ง navigation Controller๋ก ์ ํํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์ต๋๋ค!
loginVC.modalPresentationStyle = .fullScreen
modal์ ๋์ธ ๋ ์ ์ฒด๋ก ๋์ฐ๊ธฐ ์ํด์ .fullscreen ์ ์ฌ์ฉํ๊ณ ,
self.present(loginVC, animated: true, completion: nil)
์ค์ ํด๋ VC๋ฅผ present ๋ฅผ ์ด์ฉํด ํ๋ฉด์ ์ ํํ์ต๋๋ค
push ๋ ๋ค๋น๊ฒ์ด์ ์ ํ์ฉํด stack ํํ๋ก ๋ทฐ๋ฅผ ์์๋๊ฐ๋ ๋ฐฉ์์ ๋๋ค.
๋ค์ ํ๋ฉด์ผ๋ก ์ ํ๋๋ฉด ๋ทฐ์ปจํธ๋กค๋ฌ๊ฐ ์์ด๊ณ pop์ ํ๊ฒ๋๋ฉด ํ์ฌ ๋ทฐ์ปจ์ด ๋ฉ๋ชจ๋ฆฌ๊ฐ ํด์ ๋๊ณ , ์ด์ ํ๋ฉด์ด ๋ณด์ด๊ฒ ๋๋ ๋ฐฉ์์ ๋๋ค.
์ฐ์ ์๊น present๋ฅผ ํ ๋ ์คํ ๋ฆฌ๋ณด๋๋ฅผ ์ ์ธํ ๊ฒ ์ฒ๋ผ
guard let signupVC = self.storyboard?.instantiateViewController(identifier: "SignupViewController" ) as? SignupViewController else {return}
๋ค์๊ณผ ๊ฐ์ด SignupViewController ๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ๋ทฐ์ปจ์ ์ ์ธํ๊ณ
self.navigationController?.pushViewController(signupVC, animated: true)
push ๋ฅผ ํด์ฃผ๊ฒ ๋๋ฉด ๋์ด๊ฐ๊ฒ ๋ฉ๋๋ค!!
๋ค๋ก๊ฐ๋ pop์
self.navigationController?.popViewController(animated: true)
์ด๋ฏธ ์คํ์ ๋ทฐ์ปจํธ๋กค๋ฌ๊ฐ ์์ธ ์ํ์ด๊ธฐ ๋๋ฌธ์, ์๋ก์ด ๋ทฐ์ปจ์ ์ ์ธํ๋ ๊ฒ์ด ์๋ ๊ฐ๋จํ๊ฒ pop์ผ๋ก ๊ฐ๋ฅํฉ๋๋ค!!
ํ์ฌ ๊ตฌ์กฐ๊ฐ ๋ฉ์ธ -> ๋ก๊ทธ์ธ ๋ทฐ๋ก present ๋์ด์๋ ์ํ๊ณ , dismiss ํ๋ฉด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํด์ผํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
ํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ์์๋ ์ง์
guard let signupVC = self.storyboard?.instantiateViewController(identifier: "SignupViewController" ) as? SignupViewController else {return}
signupVC.a = "๋ฐ์ดํฐ ๋ฃ์๊ฒ์ฌ"
self.navigationController?.pushViewController(signupVC, animated: true)
์ด๋ฐ์์ผ๋ก ์ง์ ํ๋กํผํฐ์ ์ ๊ทผํด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์์ push ๋ present ๊ฒฝ์ฐ์๋ง ๊ฐ๋ฅํฉ๋๋ค. ์๋ก์ด ๋ทฐ์ปจํธ๋กค๋ฌ๋ฅผ ํ ๋นํด์ ์ ๋ฌํ๋ ๊ฒ์ ๊ฐ๋ฅํ์ง๋ง, dismiss ๋ pop ๊ฐ์ ๊ฒฝ์ฐ์๋ ์ด๋ฏธ ์์ฑ๋ ๋ทฐ์ปจ ์, ํน์ stack ์๋ค๊ฐ ์์๋๊ณ ์๋ก์ด ๋ทฐ์ปจ์ ์ฌ๋ฆฌ๋ ๋ฐฉ์์ด๋ผ ์ ๋ด์ฉ๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก๋ ๋ฐ์ดํฐ ์ ๋ฌ์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค
๋ฐฉ๋ฒ์ ์ด 4๊ฐ์ง๊ฐ ์๋๋ฐ
- ํ๋กํผํฐ์ ํจ์๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ
- Delegation ํ์ฉํ๊ธฐ
- Closure ํ์ฉํ๊ธฐ
- NotificationCenter ํ์ฉํ๊ธฐ
์ฐ์ ์ NotificationCenter๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ด๋ฒ ๊ณผ์ ์์๋ ์ฌ์ฉํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค
NotificationCenter๋ผ๋ ๊ฒ์ ์๋ก ์ฐ๊ฒฐ๋์ด์์ง ์์ ๋ทฐ์ปจ๋ค ์ฌ์ด์์ ์ ํธ๋ฅผ ์ ๋ฌํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ธ๋ฐ,
ํ ๋ทฐ์ปจ์์๋ ์ ํธ๋ฅผ post ํ๊ณ , ๋ฐ๋ ๋ทฐ์ปจ์์๋ ๋ฏธ๋ฆฌ observer๋ฅผ ๋ฌ์๋์ด์ ์ ํธ๋ฅผ ๋ฐ์ ์ ์๋๋ก ๋๊ธฐํ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ์๋ฉด ๋ฉ๋๋น
(ํ๋์ ๋ฐฉ์ก๊ตญ ๊ฐ๋
!!)
๊ทธ๋์ ์ ๋ ์ด๋ฒ ๋ก๊ทธ์ธ ๋ทฐ์ปจ์์ ๋ฐ์ดํฐ๋ฅผ post ํ๊ณ ,
๋ฉ์ธ์์ observer๋ฅผ ๋ฌ์๋ฌ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ต๋๋ค
(๋ก๊ทธ์ธ๋ทฐ ์ ์ฒด์ฝ๋)
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var partTextField: UITextField!
@IBOutlet weak var nameTextField: UITextField!
var personalInfo : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func loginButtonClicked(_ sender: Any) {
self.personalInfo.append(partTextField.text ?? "")
self.personalInfo.append(nameTextField.text ?? "")
NotificationCenter.default.post(name: NSNotification.Name("dataReceived"), object: self.personalInfo)
dismiss(animated: true, completion: nil)
}
@IBAction func signupButtonClicked(_ sender: Any) {
guard let signupVC = self.storyboard?.instantiateViewController(identifier: "SignupViewController" ) as? SignupViewController else {return}
self.navigationController?.pushViewController(signupVC, animated: true)
}
}
var personalInfo : [String] = []
์ฐ์ ํด๋น ๋ทฐ์ปจ ํด๋์ค์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ personalInfo ๋ผ๋ ์คํธ๋ง ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด๋์ด์ ์ฌ๊ธฐ์ ํํธ/์ด๋ฆ์ ์ ์ฅํ๋ ค๊ณ ํ์ต๋๋ค
self.personalInfo.append(partTextField.text ?? "")
self.personalInfo.append(nameTextField.text ?? "")
๊ทธ ๋ค์ personalInfo ๋ฆฌ์คํธ์ ํ์ฌ ํ ์คํธ ํ๋์ ์๋ ๋ด์ฉ์ append ํด์ฃผ๊ณ ,
NotificationCenter.default.post(name: NSNotification.Name("dataReceived"), object: self.personalInfo)
NotificationCenter ์๋ค๊ฐ dataReceived ๋ผ๋ ์ ํธ ์ด๋ฆ์ ๊ฐ์ง๊ณ , personalInfo ๋ฐ์ดํฐ๋ฅผ ๋ ๋ ค๋ณด๋ ๋๋ค!!
๊ทธ ๋ค์ ์ด์ ํ ํ๋ฉด์ ๋ณด๋ฉด์ ์ฐ์ ์ ์ฒด์ฝ๋ ๋ถํฐ,,, (๋ฐ๋ก ์ ๋ฆฌํ์ง๋ ์์๊ฑฐ๋ผ ๊ฐ๋ ์ฑ์ ๋จ์ด ์ง ์ ์์ต๋๋ค ^_^)
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var partTitleLabel: UILabel!
@IBOutlet weak var partLabel: UILabel!
@IBOutlet weak var welcomeMessageLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
addObserver()
self.partTitleLabel.isHidden = true
self.partLabel.isHidden = true
self.welcomeMessageLabel.isHidden = true
}
func addObserver()
{
NotificationCenter.default.addObserver(self, selector: #selector(settingLabel), name: NSNotification.Name("dataReceived"), object: nil)
}
@objc func settingLabel(_ noti : NSNotification)
{
let dataList = noti.object as? [String] ?? []
self.partTitleLabel.isHidden = false
self.partLabel.isHidden = false
self.welcomeMessageLabel.isHidden = false
self.partLabel.text = dataList[0]
self.welcomeMessageLabel.text = "\(dataList[1]) ๋ ์๋
ํ์ธ์ฌ ^~^"
}
@IBAction func loginButtonClicked(_ sender: Any) {
guard let loginVC = self.storyboard?.instantiateViewController(identifier: "loginNavi") else {return}
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: true, completion: nil)
}
}
func addObserver()
{
NotificationCenter.default.addObserver(self, selector: #selector(settingLabel), name: NSNotification.Name("dataReceived"), object: nil)
}
๋ค์๊ณผ ๊ฐ์ด ๋ฏธ๋ฆฌ dataReceived ๋ผ๋ ์ ํธ ์ด๋ฆ์ ์์ ํ ์ ์๋ NotificationCenter ํ์ฉํด์ observer๋ฅผ ๋ง๋ค์ด ๋์ด์ ๋ฉ๋ชจ๋ฆฌ ์์ ์ฌ๋ ค๋์์ต๋๋ค. ํด๋น dataReceived ๋ผ๋ ์ ํธ๊ฐ ๋ค์ด์ค๋ฉด settingLabel ์ด๋ผ๋ ํจ์๊ฐ ์คํ๋๋ ํํ์ ๋๋ค!!
@objc func settingLabel(_ noti : NSNotification)
{
let dataList = noti.object as? [String] ?? []
self.partTitleLabel.isHidden = false
self.partLabel.isHidden = false
self.welcomeMessageLabel.isHidden = false
self.partLabel.text = dataList[0]
self.welcomeMessageLabel.text = "\(dataList[1]) ๋ ์๋
ํ์ธ์ฌ ^~^"
}
settingLabel ํจ์๊ฐ ์คํ๋๋ฉด, ํด๋น noti๋ฅผ [String] ํํ๋ก ํ์ ์ ์ ํด์ฃผ๊ณ , ์จ๊ฒจ๋๋ ๋ผ๋ฒจ์
self.partTitleLabel.isHidden = false
self.partLabel.isHidden = false
self.welcomeMessageLabel.isHidden = false
๋ณด์ด๊ฒ ํ๋ค์์
self.partLabel.text = dataList[0]
self.welcomeMessageLabel.text = "\(dataList[1]) ๋ ์๋
ํ์ธ์ฌ ^~^"
๋ผ๋ฒจ ํ ์คํธ๋ฅผ ์ค์ ํด์ค๋๋ค์~~~~
์ด ๋ฐฉ๋ฒ ๋ง๊ณ ๋ ์์ ์ค๋ช ํ 3๊ฐ์ง ๋ฐฉ๋ฒ์ด ๋ ์๋๋ฐ์! ๋ค์์ ์ค๋ช ํ๊ฒ ์ต๋๋ค ^_^