我需要幫助解決我長期以來一直想解決的問題。它是從父級獲取資訊的視圖控制器(子級)之間的導航。如果有通知,則更新父級。我希望孩子們與父母一起立即更新。這將是一個 Home 控制器,我在其中顯示基本資訊。然后是 Home 控制器中的一個按鈕,它將采用具有我提到的導航的 View Controller。
我附上一個基本方案,以便您更好地理解它

我想收到一個想法,即使它是以編程方式開始思考邏輯的基本方法。TOP NAVIGATION BAR如何在步驟之間(第 1 步、第 2 步 ...)以及當我單擊打開與該步驟相對應的子項的任何步驟時,它也會對我有很大幫助。
The User type is nothing more than a dictionary with Strings, Ints and some data. (I can did the notification system and works, i only mention that can work in this way)
I have also thought that when I press the home controller button it will load 4 view controllers (one for each step) and it will consume load time, right?
Any help would get me out of stress Thanks a lot!!
// Home controller
class HomeController: UIViewController {
let showSteps: UIButton = {
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(showSteps), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
addSubview(showSteps)
}
@objc func showSteps() {
let controller = ShowSteps()
self.present(controller, animated: true, completion: nil)
}
}
// UIViewController with Navigation Bar
class ShowSteps: UIViewController {
private var user: User?
override func viewDidLoad() {
// update the User variable
Service.shared.fetchUserData(uid: "XXX") { user in
self.user = user
}
// listen notifications to update the user
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(updateUser), name: NSNotification.Name(rawValue: "updateUser"), object: nil)
/// 1. HERE I NEED TO ADD A NAVIGATION BAR IN THE TOP OF THE VIEW, THAT APPEAR IN ALL CHILDS
/// 2. I NEED TO NAVIGATE TO OTHER STEPS WITH A FUNCTION
}
@objc func dissmissView(){
/// 3. how can i remove from view all childs 4 in this case. but can be 6 o 7.... And then dismiss actual ShowSteps
self.dismiss(animated: true, completion: nil)
}
@objc func updateUser() {
// update user if notification called
Service.shared.fetchUserData(uid: "XXX") { user in
self.user = user
}
}
}
// that is a child example
class VCexample1: UIViewController {
/// 4. how can i see the navigation bar that has the parent? How can i send to other step from here?
/// 5. how can i get the user variable from ShowSteps?
/// 6. how can i navigate to other step from here?
///
let BarButton: UIButton = {
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(goToOtherStep), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
/// show user data
}
}
// that is a other child example
class VCexample2: UIViewController {
...
}
// that is a other child example
class VCexample3: UIViewController {
...
}
// that is a other child example
class VCexample4: UIViewController {
...
}
IS BASIC BASIC CODE. ITS NOT PERFECT. I HAVE PUT IT WITH ERRORS BUT SHORT SO THAT IT IS BETTER UNDERSTOOD
uj5u.com熱心網友回復:
委托的簡單示例:
// User data
struct UserData {
var name: String
}
// this how child get info from parentVC
protocol childVCDelegate {
// delegete user func or var (only one may be usefull)
func getUserData() -> UserData
func getUserName() -> String
var userData: UserData {get set}
}
// the parent view controller who keep the user data
class ParentVC: UIViewController, childVCDelegate {
var userData: UserData = UserData(name: "nacho111")
func getUserData() -> UserData {
userData
}
func getUserName() -> String {
userData.name
}
}
// child VC with a label updated with user name from parentVC
class ChildVC: UIViewController {
var label = UILabel()
var delegate: childVCDelegate?
// either a copy of user in child view
var userInChildVC: UserData?
// either access user in Parent VC
var userInParentVC: UserData? {
delegate?.userData
}
// fonction to register in notification center to get userData changes from parentVC
func userDataChanged() {
// only one of the 2 options is usefull
userInChildVC = delegate?.getUserData()
label.text = userInChildVC?.name
label.text = delegate?.userData.name
label.text = delegate?.getUserName()
}
}
// the tab bar which create a childVC and connect it to the parent
// via delegate property of ChildVC
class TabBar: UIViewController {
var childVC: [ChildVC] = []
// visible child VC
var visibleChildVC: Int? {
willSet {
if let index = visibleChildVC {
childVC[index].view.isHidden = true
}
}
didSet {
if let index = visibleChildVC {
childVC[index].view.isHidden = false
}
}
}
func createChildVC(withParentVC parentVC: ParentVC,
visible: Bool = false) {
let ch1 = ChildVC()
ch1.delegate = parentVC
childVC.append(ch1)
// adding child VC view in view hierarchy : optional
view.addSubview(ch1.view)
if visible {
visibleChildVC = childVC.count - 1
} else {
ch1.view.isHidden = true
}
}
func displayChildVC(atIndex index: Int) {
visibleChildVC = index
}
}
uj5u.com熱心網友回復:
對我來說,您可以使用 PagingKit 組件:
PagingKit 是您可以在一個視圖控制器中使用大量視圖控制器的解決方案之一。
https://cocoapods.org/pods/PagingKit
這個組件幫助我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/427056.html
