我有一个带有名单列表的tableview,以及我希望在按下tableview中的一个单元格时将单元格的名称内容传递给包含2个不同视图的即将到来的页面视图控制器。我正在使用一个名为“滑轮”的图书馆,我无法通过推动模态进行细分,因此在“选择行”中方法我这样做
let pagevc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PageVC") as! PageViewController
(parent as? PulleyViewController)?.setDrawerContentViewController(controller: pagevc, animated: true)
完成对viewviewcontroller的segue。这应该是正确的。 但是在viewviewcontroller视图中的2个视图中,它们都需要所选单元格的名称。所以我这样做了:
pagevc.name = selBarName
并且确保我对页面控制器的2个视图执行相同的操作(仍在上一个tableview类的didSelectRow方法中):
let detvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
detvc.barname = selBarName
let speisevc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SpeisekarteVC") as! SpeisekarteVC
speisevc.barname = selBarName
但是在每个类中打印var barname表明var仍然是一个空字符串。只有pagevc的var名称已更改为所需的名称。 所以我在PageViewController类的viewdidload中实例化了两个视图,但它仍然没有将数据从pagevc传递给pagevc中包含的其他2个视图。
我希望我能把问题弄清楚。如果您需要更多输入,请告诉我缺少什么。基本上我只需要知道如何将PageViewController中的String传递给它包含的Views。到目前为止,我能够将所选单元格的名称传递给pageviewcontroller,但不能传递给pageviewcontroller提供的视图。
修改
PageController本身应该是正确的,因为在其中仅使用2个空视图,并不要求var名称工作正常。
这是类PageViewController的一部分,只是为了完成任务:
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
var name = ""
var pageControl = UIPageControl()
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "DetailVC"),
self.newVc(viewController: "SpeisekarteVC")]
}()
override func viewDidLoad() {
print(name, "hierpageview") //THIS NAME IS PRINTED CORRECTLY AS WISHED
super.viewDidLoad()
self.dataSource = self
self.delegate = self
let speisevc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SpeisekarteVC") as! SpeisekarteVC
speisevc.barname = name
let detvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
detvc.barname = name
print(detvc.barname, "dhdhfhdah") //THIS ONE IS ALSO PRINTED RIGHT BUT PRINTING IT INSIDE THE ACTUAL CLASS RETURNS AGAIN AN EMPTY STRING !!!
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
func newVc(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
}
答案 0 :(得分:0)
您可以通过以下方式执行此操作:
let barName : String = ""
)view.barName //name of your string = // value you want to store, for example if it is an array then maybe myArr[indexPath.row]
)yourLbl.text = barName
)