草庐IT

ios - 在方向更改时重新加载 UIPageViewController View

coder 2023-09-25 原文

对于我正在构建的应用程序,我在 UIView 中有一个 UIPageViewController,它具有 Transition Style = Scroll。每个页面都有相同的 UITableViewController 类(当然有不同的数据)。此时一切正常。

现在我想要根据设备的方向使用 UITableViewController 的横向版本或纵向版本。为此,我实现了一个 NSNotification,其中包含一个在方向更改 (orientationChanged) 时调用的方法,这也有效。但是从这里出现了一个问题,当改变设备的方向时,我不确定如何加载正确的 UITableViewController 实例。如您所见,我正在使用 UIPageViewControllerDataSource 的委托(delegate)方法 viewControllerAtIndex,我在其中确保启动 viewController 的横向或纵向版本。

问题是,当改变设备的方向时,正确的 UITableViewController 只有在向左/向右滑动几页后才会加载。我一定是忽略了一些简单的东西,但似乎找不到它!

我有以下代码片段,我认为其中的问题是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model
    _pageTitles = @[@"1",@"2",@"3",@"4",@"5"];
    _pageTables = @[@"test1",@"test2",@"test3",@"test4",@"test5"];

    // Create page view controller
    self.weightPageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageViewController"];
    self.weightPageViewController.dataSource = self;

    WeightPageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.weightPageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:_weightPageViewController];
    [self.view addSubview:_weightPageViewController.view];
    [self.weightPageViewController didMoveToParentViewController:self];

    self.edgesForExtendedLayout = UIRectEdgeNone;

    //Register for device orientation changes.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

- (void) orientationChanged:(id)object {
    [self viewControllerAtIndex:0];
}

- (WeightPageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
        NSLog(@"Orientation is now Landscape);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewControllerLandscape"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    } else if (deviceOrientation == UIDeviceOrientationPortrait) {
        NSLog(@"Orientation is now Portrait);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewController"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    } else {
        NSLog(@"Orientation is now unknown);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewController"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    }
}

最佳答案

每次方向改变你都需要重新设置 View Controller

WeightPageContentViewController *startingViewController = [self viewControllerAtIndex:currentViewControllerIndex];
NSArray *viewControllers = @[startingViewController];
[self.weightPageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

关于ios - 在方向更改时重新加载 UIPageViewController View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20856108/

有关ios - 在方向更改时重新加载 UIPageViewController View的更多相关文章

  1. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  2. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  3. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  4. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  5. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

  6. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  7. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  8. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  9. ruby - 在 Ruby 中重新分配常量时抛出异常? - 2

    我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案

  10. ruby - 更改 ActiveRecord 中对象的类 - 2

    假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。

随机推荐