gpt4 book ai didi

swift - 检查方法: Managing UIPageViewController page data using UITableView cells

转载 作者:行者123 更新时间:2023-11-30 12:54:13 26 4
gpt4 key购买 nike

我实现了一个 UIPageViewController,允许用户通过单独的 UITableView 添加/删除/删除新的“页面”,类似于 Apple 在 iPhone 天气应用程序中实现城市/天气位置的方式。项目发布如下。此时其中没有天气或花哨的 UI – 我只是想专注于下面的 UIPageViewController 管理。希望如果有人想知道如何使用通过 UITableView 管理的页面来实现 UIPageViewController ,这会很有用: https://github.com/gallaugher/PageViewControllerDemo

它似乎工作正常,但我对此很陌生,并且非常不确定我是否使用推荐的方法或者它是否是“Swifty”来完成此操作。

enter image description here

现在我有:PageViewController [初始 View Controller ]- 将委托(delegate)和数据源设置为自身- 实例化第一个 UIViewController(想象一个用于本地天气的 CityViewController,即使在此示例中未添加详细信息和 UI)并设置初始值。- 城市(天气位置)保存在 [String] 数组中:cityArray

在 CityViewController 中 - 当单击右下角的“城市”按钮(在界面生成器中创建)时,如 Apple Weather,它会打开一个 UITableView (CityListViewController)。
- 为了到达这个 CityListViewController,我触发了一个直接通过界面构建​​器从 CityViewController 中的“城市”按钮绘制到 CityListViewController 的 Segue,以模态方式呈现。- preapareForSegue 将城市数组传递到 objective-c ityListViewController (UITableView)。

在CityListViewController(UITableView)中- 用户可以在 UITableView 中添加城市、移动、删除更新 tableView 和数组- 单击tableView行(城市名称或“本地天气”)会触发执行segue,展开到CityViewController,获取源CityListViewController并使用它将数据传递回CityViewController(例如citiesArray =controller.citiesArray)。- 这个 @IBAction 展开函数调用 PageViewController 的展开(实际上只是将数据从 CityListViewController 中的 TableView 传递到 UIPageViewController PageViewController)。

在 PageViewController 中放松- 获取源代码(作为 CityViewController)- 传回关键数据(例如cityArray =controller.citiesArray)- 调用函数来实例化当前页面的 View Controller 并设置PageControl索引等。

问题一:虽然这似乎有效并且我在测试期间没有设法打破它,但从 UITableView 展开到 ViewController 是否是一种合理的方法,它只是触发另一个到 UIPageViewController 的展开,除了传递数据之外什么都不做?

第二季度:我通过在 PageViewController 中以编程方式构建 UIPageControl 来实现它,但连接到 CityListViewController(UITableView)的按钮是使用 Interface Builder 在 CityViewController 中创建的。这是正确的方法吗?我似乎无法在同一个 VC 中创建这两个项目。

非常感谢那些有耐心费力解释这个复杂的解释的人。仍在尝试处理 VC 之间的数据传递,以及这与 PageController 和 TableView 的关系。

最佳答案

对于对此感兴趣的解决方案,我已发布到 GitHub: https://github.com/gallaugher/PageViewControllerDemo我发布了更通用的名称 - PageVewController、DetailViewController 和 ListViewController,因此这更容易重用,也许也更容易遵循。

感谢 Sazan Dauti 在 StackOverflow 之外回答这个问题。可以直接在 PageController 和 ListController 之间进行连接,后者包含一个用于管理页面的 TableView 和一组相似的页面(例如,就像 Apple 在天气应用程序中所做的那样,您可以在其中添加/删除/移动城市),保留详细信息(城市中的城市)原始示例)不知道它在 PageView 中。- 将 Segue 直接从 UIPageViewController 拖到具有 TableView 的 UIViewController,在我的例子中,该 TableView 包含位置列表(不是由 PageView 管理的详细信息)。 Segue 应该以模态方式呈现。关键变量应该在 PageViewController 中的准备过程中传递给 ListViewController,如下所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ToListViewController" {
let controller = segue.destination as! ListViewController
controller.listArray = listArray
controller.currentPage = currentPage
}
}
  • 确保传递的值(上述情况下的 listArray 和 currentPage)在 ListViewController 中声明。

  • 将 Unwind 方法添加到 PageViewController 中,与此类似:

    @IBAction func unwindFromListViewController(sender: UIStoryboardSegue) { pageControl.numberOfPages = listArray.count pageControl.currentPage = 当前Page setViewControllers([createDetailViewController(currentPage)],方向:.forward,动画:false,完成:nil)}

上面的setViewControllers指的是PageViewController中编写的函数,其中包含实例化当前页面的DetailViewController的逻辑。

  • 返回转场是通过从 ListView 中的 TableViewCell 拖动到“退出”按钮(此 UIViewController 顶部/标题 View 的三个按钮的最右侧)来创建的。系统会询问您的名称应该在 PageViewController 中的展开方法(在上面的示例中,我将其称为 unwindFromListViewController),因此请务必在尝试进行转场之前添加此方法。

  • 向ListViewController添加prepare for segue函数:

    覆盖函数准备(用于segue:UIStoryboardSegue,发件人:任何?){ if segue.identifier == "ToPageViewController"{ 让controller = segue.destination为!页面 View Controller Controller .currentPage = 当前Page Controller .listArray = listArray }}

  • 并在tableView didSelectRowAt中,触发执行segue:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { currentPage = indexPath.row//将当前页设置为所选行 PerformSegue(withIdentifier:“ToPageViewController”,发件人:self)}

我在网上找不到任何合适的例子来演示这是如何完成的,所以希望这是可以理解和高效的。欢迎任何指正。干杯!

关于swift - 检查方法: Managing UIPageViewController page data using UITableView cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40616056/

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