菜鸟教程小白 发表于 2022-12-12 10:39:35

ios - 在 UIPageViewController 中重新加载 View


                                            <p><p>我正在制作一个具有 UIPageViewController 的 iOS 应用程序,如果尚未添加任何其他页面,则该 UIPageViewController 会加载默认页面,并且用户可以在通过应用程序进行时添加页面。但是,初始 View 不会随着其他 View 的添加而改变。</p>

<p>我尝试了以下问题的答案:<br/>
<a href="https://stackoverflow.com/questions/7208871/is-it-possible-to-turn-page-programmatically-in-uipageviewcontroller" rel="noreferrer noopener nofollow">Is it possible to Turn page programmatically in UIPageViewController?</a> <br/>
<a href="https://stackoverflow.com/questions/22554877/changing-uipageviewcontrollers-page-programmatically-doesnt-update-the-uipagec" rel="noreferrer noopener nofollow">Changing UIPageViewController&#39;s page programmatically doesn&#39;t update the UIPageControl</a> <br/>
<a href="https://stackoverflow.com/questions/14220289/removing-a-view-controller-from-uipageviewcontroller" rel="noreferrer noopener nofollow">Removing a view controller from UIPageViewController</a> <br/>
<a href="https://stackoverflow.com/questions/20120995/refresh-a-uipageviewcontroller" rel="noreferrer noopener nofollow">Refresh a UIPageViewController</a> </p>

<p>它们都不适合我。在我重新加载应用程序之前, View 仍保留在初始页面,之后页面显示正常,我可以添加任意数量的页面。</p>

<p>这是我用于制作 PageView 和页面的代码</p>

<pre><code>- (void)viewDidLoad
{
    ;

    self.pageController = [ initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [ setFrame:[ bounds]];

    UIViewController *initialViewController = ;

    __block Page1ViewController *blocksafeSelf = self;

    // This doesn&#39;t work either... :(
    void (^completionBlock)(BOOL) = ^(BOOL finished)
    {
      // It seems that whenever the setViewControllers:
      // method is called with &#34;animated&#34; set to YES, UIPageViewController precaches
      // the view controllers retrieved from it&#39;s data source meaning that
      // the dismissed controller might not be removed. In such a case we
      // have to force it to clear the cache by initiating a non-animated
      // transition.
      usleep(1);
      dispatch_async(dispatch_get_main_queue(), ^(){
            
                                    direction:UIPageViewControllerNavigationDirectionForward
                                       animated:NO
                                     completion:nil];
      });
    };

   
                              direction:UIPageViewControllerNavigationDirectionForward
                               animated:YES
                           completion:completionBlock];

    ;
    [ addSubview:];
    ;
}

- (void)reloadViews
{
    [ makeObjectsPerformSelector:@selector(removeFromParentViewController)];
    [ makeObjectsPerformSelector:@selector(removeFromSuperview)];

    ;
    ;
    ;
    self.pageController = nil;
    self.pageController = [ init];

    ;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((SomethingViewController *)viewController).index;
    index++;

    if (index &gt;= .count)
    {
      return nil;
    }

    return ;
}

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@&#34;Storyboard_iPhone&#34;
                                              bundle:nil];
    if (.count == 0 &amp;&amp; index == 0)
    {
      NoSomethingViewController *vc = ;
      vc.index = index;
      return vc;
    }

    SomethingViewController *childViewController = ;
    childViewController.something = [ somethingAtIndex:index];
    childViewController.index = index;

    return childViewController;
}
</code></pre>

<p>当应该创建一个新页面时,通过在 <code>somethings</code> 数组中添加一个 <code>something</code>,我从添加 <code> 的 ViewController 调用 reloadViews一些东西</code>。然后代码会到达 <code>viewControllerAtIndex</code> 的第二部分,但这并没有显示在我在手机上看到的页面上,因为我仍然看到 NoSomethingViewController 和它的 View 。</p>

<p>如果您需要更多代码,我会提供。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在pageViewController viewWillAppear方法中添加如下代码</p>

<p>数据源 =self </p>

<p>并在 View 中添加WillDisappear</p>

<p>数据源 = 无</p>

<p>这将强制 pageViewController 重新加载其数据源</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 UIPageViewController 中重新加载 View ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/24551073/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/24551073/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 UIPageViewController 中重新加载 View