传递数据以查看嵌入在容器视图中的控制器

Passing Data to view controllers that are embedded in container views

我的视图控制器只需要传递一个名为 "otherUser" 的 NSDictionary。我正在使用分段控制器方便地向使用容器视图的用户呈现其中的 4 个视图。我知道所有这些视图将同时加载并保持加载状态,这就是我想要的,即使会占用内存。我知道如何将这个值直接传递给视图控制器,但不知道如何将它传递给视图控制器,然后视图控制器将它传播到 4 个视图以加载相同的数据。 ---- 下面是我根据搜索栏操作将 "otherUser" 传递给 "BusinessProfileSwitchView"(具有容器视图的视图控制器)。

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "BusinessProfiles" {
        // gotta check if we're currently searching
        if self.searchController.isActive && searchController.searchBar.text != "" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let user = filteredUsers[indexPath.row]
                let controller = segue.destination as? BusinessProfileSwitchView
                controller?.otherUser = user
            }
        } else {
            if let indexPath = tableView.indexPathForSelectedRow {
                let user = usersArray[indexPath.row]
                let controller = segue.destination as? BusinessProfileSwitchView
                controller?.otherUser = user
            }
        }
    }
}

你们认为我应该使用什么攻击方法将 "otherUser"/NSDictionary 传递给带有容器视图的视图控制器,然后将 "otherUser" 传播到 4 个视图?下面是我连接到其他 4 个视图的视图控制器。

 import UIKit

 class BusinessProfileSwitchView: UIViewController {

@IBOutlet weak var feedView: UIView!
@IBOutlet weak var collectionView: UIView!
@IBOutlet weak var infoView: UIView!
@IBOutlet weak var socialView: UIView!

var infos: BusinessProfilesDetails!
var collections: BusinessProfilePostsCollection!
var feeds: BusinessProfilePostsFeed!
var socials: BusinessProfilesViewController!

@IBOutlet weak var switchController: UISegmentedControl!

var otherUser: NSDictionary!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    switch  switchController.selectedSegmentIndex {
    case 0:
        infoView.isHidden = false
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 1:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = false
        socialView.isHidden = true
        break
    case 2:
        infoView.isHidden = true
        feedView.isHidden = false
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 3:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = false
        break
    default:

        break;
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func viewControl(_ sender: UISegmentedControl) {
    switch  switchController.selectedSegmentIndex {
    case 0:
        infoView.isHidden = false
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 1:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = false
        socialView.isHidden = true
        break
    case 2:
        infoView.isHidden = true
        feedView.isHidden = false
        collectionView.isHidden = true
        socialView.isHidden = true
        break
    case 3:
        infoView.isHidden = true
        feedView.isHidden = true
        collectionView.isHidden = true
        socialView.isHidden = false
        break
    default:

        break;
    }
}

}

在 Storyboard 中,当您在 ContainerView 中嵌入 VC 时,您还会看到一个 "segue" 连接器。当根 VC 加载时,您将接到一个电话,为它准备 segue。

给每个storyboard-created segue一个标识符——例如"infoViewEmbedSegue"、"feedViewEmbedSegue"等

在您的根 VC 中,我猜

var infos: BusinessProfilesDetails!
var feeds: BusinessProfilePostsFeed!

是引用infoView内容的变量吗?如果是这样,在 prepare() 中你想要:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // get a reference to the embedded PageViewController on load

    if let vc = segue.destination as? BusinessProfilesDetails,
        segue.identifier == "infoViewEmbedSegue" {
        self.infos = vc
        // if you already have your data object
        self.infos.otherUser = theDataDict
    }

    if let vc = segue.destination as? BusinessProfilePostsFeed,
        segue.identifier == "feedViewEmbedSegue" {
        self.feeds = vc
        // if you already have your data object
        self.feeds.otherUser = theDataDict
    }

    // etc

}

现在您将拥有对嵌入容器视图中的实际视图控制器的持久引用,以防您希望在根目录的其他部分访问它们 VC,例如:

@IBAction func btnTapped(_ sender: Any) {
    self.feeds.otherUser = theDataDict
}