如何向我的 UIPageViewController 添加多个 ViewController?

2024-02-02

所以我对 Obj-C 很陌生,并尝试查看示例代码和其他在线资源来回答我的问题,但我似乎找不到任何真正有帮助的东西。本质上,我想做的是将我在 Storyboard 中创建的多个 UIViewController 添加到 UIPageViewController - 我首先寻求帮助的地方是 Xcode 本身,使用 PageBased 应用程序的模板。这实际上很有帮助,我启动并运行了一个 PageController。然后我转向在线资源,但大多数(如果不是全部)使用单个 viewController (例如this http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/)。然后我转向 StackOverflow 并发现了以下内容 -如何实现利用多个 ViewController 的 UIPageViewController https://stackoverflow.com/questions/21641373/how-to-implement-uipageviewcontroller-that-utilizes-multiple-viewcontrollers。 上述资源让我到目前为止: 在 PageViewController.h 中:

    #import <UIKit/UIKit.h>

@interface PageViewController : UIPageViewController <UIPageViewControllerDataSource>

@end

在 PageViewController.m 中:

    #import "PageViewController.h"

@interface PageViewController ()

@end

@implementation PageViewController {
    NSArray *viewControllers;
    UIViewController *first;
    UIViewController *second;
    UIViewController *third;
    UIViewController *fourth;
    UIViewController *fifth;
}

- (UIViewController *)first {

        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        first = [sb instantiateViewControllerWithIdentifier:@"1"];

    return first;
}

- (UIViewController *)second {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    second = [sb instantiateViewControllerWithIdentifier:@"2"];

    return second;
}
- (UIViewController *)third {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    third = [sb instantiateViewControllerWithIdentifier:@"3"];

    return third;
}
- (UIViewController *)fourth {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    fourth = [sb instantiateViewControllerWithIdentifier:@"4"];

    return fourth;
}
- (UIViewController *)fifth {

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    fifth = [sb instantiateViewControllerWithIdentifier:@"5"];

    return fifth;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = self;

    // Aggancio il view controller iniziale.
    [self setViewControllers:@[self.first]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    UIViewController *nextViewController = nil;

    if (viewController == self.first) {
        nextViewController = self.second;
    }
    if (viewController == self.second) {
        nextViewController = self.third;
    }
    if (viewController == self.third) {
        nextViewController = self.fourth;
    }
    if (viewController == fourth) {
        nextViewController = self.fifth;
    }

    return nextViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    UIViewController *prevViewController = nil;

    if (viewController == self.fifth) {
        prevViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        prevViewController = self.third;
    }
    if (viewController == self.third) {
        prevViewController = self.second;
    }
    if (viewController == self.second) {
        prevViewController = self.first;
    }

    return prevViewController;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

所有这些代码所做的就是加载第一个视图控制器,它是now可以滑动,但实际上并没有滑动到任何东西 - 这是为什么?我究竟做错了什么?上述资源使用标题和页面来创建视图,我真的不知道如何去做。有人介意引导我或推动我朝正确的方向前进吗?任何帮助将不胜感激。谢谢!


如果有人感兴趣,这里是完整的工作代码。我花了一段时间才把它做好,但最终效果很完美!

#import "PageViewController.h"

@interface PageViewController ()

@property (nonatomic, retain) UIViewController *first;
@property (nonatomic, retain) UIViewController *second;
@property (nonatomic, retain) UIViewController *third;
@property (nonatomic, retain) UIViewController *fourth;
@property (nonatomic, retain) UIViewController *fifth;

@end

@implementation PageViewController {
    NSArray *viewControllers;
}

- (UIViewController *)first {
    if (!_first) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        _first = [sb instantiateViewControllerWithIdentifier:@"1"];
    }
    return _first;
}

- (UIViewController *)second {
    if (!_second) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _second = [sb instantiateViewControllerWithIdentifier:@"2"];
    }
    return _second;
}
- (UIViewController *)third {
    if (!_third) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _third = [sb instantiateViewControllerWithIdentifier:@"3"];
    }
    return _third;
}
- (UIViewController *)fourth {
    if (!_fourth) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _fourth = [sb instantiateViewControllerWithIdentifier:@"4"];
    }
    return _fourth;
}
- (UIViewController *)fifth {
    if (!_fifth) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _fifth = [sb instantiateViewControllerWithIdentifier:@"5"];
    }
    return _fifth;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = self;

    // Aggancio il view controller iniziale.
    [self setViewControllers:@[self.first]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    UIViewController *nextViewController = nil;

    if (viewController == self.first) {
        nextViewController = self.second;
    }
    if (viewController == self.second) {
        nextViewController = self.third;
    }
    if (viewController == self.third) {
        nextViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        nextViewController = self.fifth;
    }

    return nextViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    UIViewController *prevViewController = nil;

    if (viewController == self.fifth) {
        prevViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        prevViewController = self.third;
    }
    if (viewController == self.third) {
        prevViewController = self.second;
    }
    if (viewController == self.second) {
        prevViewController = self.first;
    }

    return prevViewController;
}

@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何向我的 UIPageViewController 添加多个 ViewController? 的相关文章

  • Objective-C 中是否有命名初始化方法的约定?

    在可以通过不同的 init 方法初始化的 Objective C 类中 将所有初始化程序共用的初始化代码收集到一个从其他 init 方法 有时也从从 Nib 唤醒 是否有关于如何命名该方法的约定 初始化器 初始化公共 根据 Apple 的说
  • 如何重新定位或移动 Google Maps SDK 上的当前位置按钮?

    如何将 Objective C 中的当前位置按钮移至我的偏好 现在 我已启用它 但底角有东西挡住了它 Thanks 您可以使用 padding 将按钮向上移动 self mapView padding UIEdgeInsets top 0
  • 沙盒尝试恢复消耗性 IAP

    我一直在尝试在 iOS 上测试一些消耗性 IAP 但遇到了一个奇怪的错误 弹出一条警报 其中包含以下文本 此应用内购买已被购买 它将恢复为 自由的 环境 沙盒 我已经检查过 并且确定我的 IAP 可以在 iTunesConnect 中使用
  • ios 在后台处理推送通知

    我想保存应用程序处于后台状态时到达的推送通知 我知道关于 void application UIApplication application didReceiveRemoteNotification NSDictionary userIn
  • 高效创建 x 层深度的嵌套 for 循环

    这可能是一件简单的事情 但我需要创建一个循环结构 使其循环 y x 次以创建 x 和 y 的所有可能组合 例如 如果有 4 个 x 每个 x 有 2 个 y 我想做这样的事情 for int a 0 a lt y a for int b 0
  • ios 导航 堆栈操作

    我在尝试从 iOS 应用程序操作导航堆栈时遇到问题 或者至少是由于这种操纵而产生的行为 我的情况 我有 3 个 ViewController 控制器a显示多个级别 控制器 b 是游戏视图 控制器 c 是某种分数 显然 我将在控制器 a 中选
  • 自定义 UITableViewCell 选择样式?

    当我点击我的UITableViewCell 当我单击单元格时 背景部分 我的背景图像未覆盖的区域 会变成蓝色 另外 所有的UILabel单击时单元格上的 s 变为白色 这就是我想要的 然而 我不想要的是当我点击它时的蓝色背景 但如果我这样做
  • iPhone UINavigationBar 使用 [UINavigationBar 外观] 更改所有控制器的字体样式

    我知道我可以单独更改导航栏的字体 如本答案所述 更改导航栏的字体 https stackoverflow com questions 5832036 change the navigation bars font 目前我正在使用一种更全局的
  • 来自 iPhone/iPad 的 json Web 服务

    有人可以帮助我解决如何从 iphone 或 ipad 使用 json Web 服务的问题吗 这里我的要求是使用 API 密钥实现 json webservice 如果可能的话发布一些教程或示例链接 谢谢 规范的 JSON 处理库是here
  • iphone:如何停止快门动画?

    我有两个问题 1 我想知道如何在相机加载时停止快门动画 我正在使用 UIImagePickerController 我已经参考了堆栈溢出的许多答案 但没有成功 2 我在相机中有一个自定义按钮 使用cameraOverlayView并想通过单
  • 使用基于约束的布局自动调整 NSTokenField 的大小

    有没有办法自动调整大小height使用约束的 NSTokenField 保持宽度恒定 sizeToFit应该有效 但事实并非如此 如果我设置一个约束来保持宽度不变并调用此方法 它将忽略约束并仅调整宽度大小 当我想要的是仅调整高度大小时 基于
  • 无法在 Windows 上使用 Gnustep 编译 Objective C

    嗨 我是初学者 学习目标c 我发现错误 hello m 1 34 Foundation Foundation h 没有这样的文件或目录 我开始知道我需要制作一个 make 文件 我可以知道如何制作 make 文件吗 无需创建 makefil
  • 如何在iOS应用程序中捕获用户的手写签名[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 在我的应用程序中 用户将请求客户提供数字化的手写签名 我需要能够在用户在触摸屏上 书写 签名时捕获签名
  • CBPeripheral 名称有时为 null

    我正在开发一个应用程序来与蓝牙 LE 外围设备进行通信 我目前正在测试的外围设备是其中之一these http www ti com tool cc2540dk mini 有趣的是 有时当我发现它时 我会得到它的正确名称 SimpleBLE
  • 如何在 ios 7 上更改重新排序控制图像

    我正在寻找一种更改重新排序控件图像和大小的方法 我使用此代码来更改重新排序图像 void tableView UITableView tableView willDisplayCell UITableViewCell cell forRow
  • iCloud loadFrom Contents - 如何处理 UIDocumentStateSavingError 和 UIDocumentStateEditingDisabled

    我在我的应用程序中使用 iCloud 来加载文本文件 加载文本文件时 当我调用时 iCloud 会调用此方法 UIDocument openWithCompletionHandler BOOL success etc BOOL loadFr
  • NSURL URLWithString:引发异常

    简而言之 NSURL URLWithString 似乎引发了一个例外 根据文档 如果字符串格式错误 则返回 nil 没有提到在任何情况下都会引发异常 除此之外 我还对 URL 进行编码并检查nil在将字符串转换为 URL 之前 任何人都可以
  • UISplitViewController - 推送模态视图

    使用 UISplitViewController 时推送模态视图的最佳实践是什么 您会从 RootViewController DetailViewController 还是直接从应用程序委托推送 理想情况下 我想要实现的功能是在基于某些条
  • 使用超类初始化器初始化类

    我有两个类 一个是另一个的子类 比如说Animal and Dog 超类有一些初始化器 比如initAnimal 子类有一些初始化器 比如initDog 问题是 从编译器的角度来看 做类似的事情是完全合法的Dog adog Dog allo
  • 使用 Storyboard 时获取 NSManagedObjectContext

    目标是获取当前的 NSManagedObjectContext 以便使用 Core Data 在 iOS 4 3 中 我将 UINavigationController 的委托设置为 AppDelegate 如下所示 在 AppDelega

随机推荐

  • XSLT 复制所有节点,并按分隔符分割

    我正在寻找一个执行以下操作的 xslt 以输入 xml 为例
  • 无法与任何提供的主机建立套接字

    我正在努力解决 android 中的文件传输问题 我正在使用 smack 4 1 连接到 openfire 服务器 我的问题是 当我使用 Spark 到 Spark 文件传输时 它工作正常 但是当我从Spark 到 Android 或 An
  • 如何在 django 自定义身份验证后端访问请求?

    我想用 django 的身份验证执行以下操作 记录错误的登录尝试 在 x 次错误登录尝试后暂时锁定帐户 记录成功登录 我认为自定义身份验证后端将是解决方案 我可以做我想做的大部分事情 但我想记录进行尝试的用户的 IP 和 REMOTE HO
  • Excel Yield 函数的.NET 实现

    Excel 的名为 分析工具库 的插件提供了 收益率 函数 用于计算定期支付利息的证券的收益率 函数运行良好并返回正确的数据 我的理解是基于迭代的函数 在我的代码中实现它并不容易 我的问题是有人知道 见过 C 最终是其他语言 的实现并可以分
  • 在 Groovy 中获取由字符分隔的子字符串

    考虑下面的字符串 String names Bharath Vinayak Harish Punith 我想以它仅包含的字符串形式获得输出Bharath 字符串直到第一次出现 运算符 任何人都可以告诉我 我们该怎么做 在一般情况下 我同意s
  • Python 列表理解代价高昂

    我试图找到列表理解的效率 但它看起来比普通函数操作更昂贵 有人可以解释一下吗 def squares values lst for x in range values lst append x x return lst def main t
  • 如何在没有 root 访问权限的情况下在本地安装 CPAN 模块(DynaLoader.pm 第 229 行错误)?

    不能与其他模块一起使用 但举个例子 我使用 CPAN 设置安装了 Text CSV XS makepl arg gt q PREFIX lib 当我尝试运行 test pl 脚本时 perl 测试 pl usr bin perl use l
  • 计算n的最佳方法选择k?

    评估 价值 最有效的方法是什么 n choose k 我认为的蛮力方法是找到n k n k 通过单独计算每个阶乘 更好的策略可能是根据这个使用DP递归公式 https i stack imgur com Kq3OH png nCk n 1
  • WHERE IN问题中的SQL占位符,插入字符串失败

    作为我工作的一部分 我需要编写 SQL 查询来连接到我们的 PI 数据库 要生成查询 我需要传递一个array标签 本质上是主键 但这些必须作为字符串插入 由于这将是一个模块化查询并用于多个标签 因此使用了占位符 该查询依赖于 WHERE
  • OpenGL - ARB 扩展

    我使用的是 MacBook Pro 13 英寸 2010 年中 并且使用 OpenGL 我注意到 库中缺少一些功能 我在互联网上找到了有关我的硬件的规格 上面写着 支持OpenGL 3 3 这很奇怪 所以我打印了我的 OpenGL 版本并这
  • 使用deathbycaptcha服务处理Google recaptcha v2时如何控制scrapy中的请求流?

    你好 我正在使用 python 使用 scrapy 网络爬行框架 抓取网站并使用 Deathbycaptcha 服务解决我在其页面上遇到的验证码 我的下载延迟设置为 30 秒 我只抓取几页来获取基本信息 这样我就不会过多地占用网站带宽或任何
  • 中断当前正在执行的所有 asyncio.sleep

    where 这是在 Linux Python 3 5 1 上 what 我正在开发一个监控流程asyncio 他们在不同地方的任务await on asyncio sleep不同时长的呼叫 有时我希望能够打断所有所说的话asyncio sl
  • 在哪里放置与 IPython“冻结模块”调试器警告相关的 Python 配置代码?

    我刚刚在 Macintosh 上使用了 brew 来升级我的设置 现在 当我运行时 我收到此 调试器警告 jupyter notebook 其他文字被剪掉 I 09 03 00 955 NotebookApp Jupyter Noteboo
  • Flux:如何让一个动作等待存储?

    我正被一个 React 问题困住了 我确信这个问题不会像我现在看起来那么困难 我正在针对 RESTful 服务器 API 构建一个单页应用程序 该 API 返回资源以及描述可以使用该资源执行的操作的链接 我试图确保我的客户端的 ajax 调
  • 将 DataMemberAttribute 放在接口成员上意味着什么?

    放置一个是什么意思数据成员属性 http msdn microsoft com en us library system runtime serialization datamemberattribute aspx在接口成员上 这对派生类有
  • 绘制位图 C# [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在尝试使用位图类在屏幕上绘制图像
  • 标准库算法是否允许复制谓词参数?

    假设我们想从向量中删除重复值ints 通常的解决方案是对向量进行排序并使用擦除删除惯用语删除重复项 但我们需要保持不会被移除的元素的顺序 所以我们无法排序 所以人们可能会想出这样的谓词并使用 with withremove if算法 str
  • 任意或自定义 URL 的 Rails 功能测试

    我的 Rails 应用程序中有一个名为 Photo 的 RESTful 资源 我在用着回形针 http www thoughtbot com projects paperclip为我的照片提供不同的 样式 缩略图等 并且我使用自定义路由来以
  • 是否可以通过 AJAX 加载 tumblr 帖子?

    我只是想知道是否可以通过 AJAX 加载 tumblr 帖子 我知道可以使用注释 但我想内联加载帖子的内容 我不是在谈论无限滚动 Thanks 对的 这是可能的 我编写了一些代码来读取帖子的标题并在我的网站上创建一个菜单 您可以访问帖子的全
  • 如何向我的 UIPageViewController 添加多个 ViewController?

    所以我对 Obj C 很陌生 并尝试查看示例代码和其他在线资源来回答我的问题 但我似乎找不到任何真正有帮助的东西 本质上 我想做的是将我在 Storyboard 中创建的多个 UIViewController 添加到 UIPageViewC