gpt4 book ai didi

ios - 传递数据以查看嵌入在容器 View 中的 Controller

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:30 25 4
gpt4 key购买 nike

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

 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 传递给带有容器 View 的 View Controller ,然后将“otherUser”传播到 4 个 View ?下面是我连接到其他 4 个 View 的 View Controller 。

 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 中,当您将 VC 嵌入 ContainerView 时,您还会看到一个“segue”连接器。当根 VC 加载时,您将接到一个电话,为它准备 segue。

为每个 Storyboard创建的 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

}

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

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

关于ios - 传递数据以查看嵌入在容器 View 中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com