简体   繁体   English

将数据传递给嵌入在容器视图中的视图控制器

[英]Passing Data to view controllers that are embedded in container views

I have view controllers that just need to get passed a NSDictionary called "otherUser".我有视图控制器,只需要传递一个名为“otherUser”的 NSDictionary。 I am using a segmented controller to conveniently present 4 of these views to a user using container views.我正在使用分段控制器方便地向使用容器视图的用户呈现其中 4 个视图。 I know all of these views will be loaded at the same time and will stay loaded, which is what I want even though the toll on memory.我知道所有这些视图都将同时加载并保持加载状态,即使会占用内存,这也是我想要的。 I know how to directly pass this value to the view controller but don't know how to pass it to a view controller that would then spread it to 4 views to load the same data.我知道如何直接将此值传递给视图控制器,但不知道如何将其传递给视图控制器,然后将其传播到 4 个视图以加载相同的数据。 ---- Below is me passing "otherUser" to "BusinessProfileSwitchView"(View Controller with container views) based on the search bar actions. ---- 下面是我根据搜索栏操作将“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
            }
        }
    }
}

What is the method of attack do you guys think I should use to pass "otherUser"/NSDictionary to the view controller with container views that would then spread "otherUser" to 4 views?你们认为我应该使用什么攻击方法将“otherUser”/NSDictionary 传递给带有容器视图的视图控制器,然后将“otherUser”传播到 4 个视图? Below is my view controller that connect to the other 4 views.下面是我连接到其他 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;
    }
}

} }

In your Storyboard, when you embed a VC in a ContainerView, you also see a "segue" connecter.在您的 Storyboard 中,当您在 ContainerView 中嵌入 VC 时,您还会看到一个“segue”连接器。 When the root VC loads, you will get a call to prepare for segue for that.当根 VC 加载时,您将收到一个电话,准备为此进行转场。

Give each storyboard-created segue an Identifier - such as "infoViewEmbedSegue", "feedViewEmbedSegue", etc.给每个故事板创建的 segue 一个标识符——例如“infoViewEmbedSegue”、“feedViewEmbedSegue”等。

In your root VC, I'm guessing that在你的根 VC 中,我猜

var infos: BusinessProfilesDetails!
var feeds: BusinessProfilePostsFeed!

are variables to reference the content of infoView ?是引用infoView内容的infoView吗? If so, in prepare() you want to:如果是这样,在 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

}

Now you'll have persistent references to the actual View Controllers embedded in your Container Views, in case you want access to them in other parts of your root VC, eg:现在,您将拥有对嵌入在容器视图中的实际视图控制器的持久引用,以防您想在根 VC 的其他部分访问它们,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM