기존에 작성하던 MVC 아키텍쳐에서 MVVM 으로 바꿔보는 과정에서
네비게이션 바 아이템이 원하는 뷰에 추가되지 않는 문제를 발견했다
class RxSubjectViewModel: UIViewController {
var contactData = [
Contact(name: "Father", age: 21, address: "Seoul in Korea"),
Contact(name: "Mother", age: 23, address: "Seoul in Korea"),
Contact(name: "Sister", age: 25, address: "Seoul in Korea"),
Contact(name: "Friend1", age: 21, address: "Seoul in Korea"),
Contact(name: "Friend2", age: 21, address: "Seoul in Korea")
]
var list = PublishSubject<[Contact]>()
func navigationSetting() {
let resetButton = UIBarButtonItem(title: "리셋", style: .plain, target: self, action: #selector(resetData))
let addButton = UIBarButtonItem(title: "추가", style: .plain, target: self, action: #selector(fetchData))
let newContactButton = UIBarButtonItem(title: "새로운 연락처", style: .plain, target: self, action: #selector(newData))
navigationItem.leftBarButtonItem = newContactButton
navigationItem.rightBarButtonItems = [addButton, resetButton]
}
}
-적용할 뷰-
class RxSubjectViewController: UIViewController {
let mainView = RxSubjectView()
let viewModel = RxSubjectViewModel()
override func loadView() {
view = mainView
view.backgroundColor = .white
}
override func viewDidLoad() {
super.viewDidLoad()
viewModel.navigationSetting()
}
}
1. target을 해당 뷰 컨트롤러로?
func navigationSetting() {
let resetButton = UIBarButtonItem(title: "리셋", style: .plain, target: self, action: #selector(resetData))
let addButton = UIBarButtonItem(title: "추가", style: .plain, target: self, action: #selector(fetchData))
let newContactButton = UIBarButtonItem(title: "새로운 연락처", style: .plain, target: self, action: #selector(newData))
}
######################################################################
-> 해당 코드에서 target: self 이 부분에 주목했음
- 수정
class RxSubjectViewModel: UIViewController {
func navigationSetting(viewName: UIViewController) {
let resetButton = UIBarButtonItem(title: "리셋", style: .plain, target: viewName, action: #selector(resetData))
let addButton = UIBarButtonItem(title: "추가", style: .plain, target: viewName, action: #selector(fetchData))
let newContactButton = UIBarButtonItem(title: "새로운 연락처", style: .plain, target: viewName, action: #selector(newData))
navigationItem.leftBarButtonItem = newContactButton
navigationItem.rightBarButtonItems = [addButton, resetButton]
}
}
이 방법 역시 적용되지 않았고
네비게이션 바 아이템이 적용되는 뷰 시점에 대해서 생각해보게됐다.
2. 아...이런
target에 주목했던 이유가 뭘까...
결국 해당 네비게이션 아이템이 적용되는 시점을 파악하면 될 문제였는데
class RxSubjectViewModel: UIViewController {
func navigationSetting(view: UIViewController) {
let resetButton = UIBarButtonItem(title: "리셋", style: .plain, target: self, action: #selector(resetData))
let addButton = UIBarButtonItem(title: "추가", style: .plain, target: self, action: #selector(fetchData))
let newContactButton = UIBarButtonItem(title: "새로운 연락처", style: .plain, target: self, action: #selector(newData))
view.navigationItem.leftBarButtonItem = newContactButton
view.navigationItem.rightBarButtonItems = [addButton, resetButton]
}
}
-적용
class RxSubjectViewController: UIViewController {
let mainView = RxSubjectView()
let viewModel = RxSubjectViewModel()
override func loadView() {
view = mainView
view.backgroundColor = .white
}
override func viewDidLoad() {
super.viewDidLoad()
viewModel.navigationSetting(view: self)
}
}
MVVM에 익숙치 않아서 생긴 문제라 생각하고 싶다..
[SwiftUI] CustomTextField + ToolBar 사용방법과 문제점 (0) | 2024.08.28 |
---|---|
[iOS - SwiftUI] 프로필 이미지 설정 페이지 (0) | 2024.01.17 |
[iOS/Swift ] FSCalendar 날짜선택 관련 에러해결 (0) | 2022.08.28 |
[Swift - iOS] TableView 헤더 높이설정 (0) | 2022.08.06 |