移除 PageViewController

Posted

技术标签:

【中文标题】移除 PageViewController【英文标题】:Remove PageViewController 【发布时间】:2013-11-28 14:55:17 【问题描述】:

我在 AppCoda 上使用了一个教程来添加一种教程样式 UIPageViewController - 它的效果非常好 - 只是我无法在单击按钮时将其删除。

这是我的 PageViewController:

#import "startupViewController.h"
#import "SKSlideViewController.h"
#import "APPChildViewController.h"

@interface startupViewController ()
@end
@implementation startupViewController
@synthesize outletWallpaper;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    [self mymethod];


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


- (void)mymethod 

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

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

    APPChildViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];



- (APPChildViewController *)viewControllerAtIndex:(NSUInteger)index 

    APPChildViewController *childViewController = [[APPChildViewController alloc] initWithNibName:@"APPChildViewController" bundle:nil];
    childViewController.index = index;

    return childViewController;



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

    NSUInteger index = [(APPChildViewController *)viewController index];

    if (index == 0) 
        return nil;
    

    // Decrease the index by 1 to return
    index--;

    return [self viewControllerAtIndex:index];



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

    NSUInteger index = [(APPChildViewController *)viewController index];

    index++;

    if (index == 3) 
        return nil;
    

    return [self viewControllerAtIndex:index];



- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController 
    // The number of items reflected in the page indicator.
    return 3;


- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController 
    // The selected item reflected in the page indicator.
    return 0;


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    NSLog(@"Switching View Controllers...");

        SKSlideViewController *slideController=(SKSlideViewController *)[segue destinationViewController];
        [slideController setStoryBoardIDForMainController:@"MainVC" leftController:@"LeftVC" rightController:@"RightVC"];
        [slideController reloadControllers];



@end

此代码将我发送到另一个文件,我需要从那里删除整个文件。

【问题讨论】:

我尝试从 SuperView 中删除所有内容,但没有任何效果。但是我尝试将 self.view.alpha 设置为 0(在它链接到的新 .m 文件上),这似乎可行,但是下面的任何东西(按钮等)我出于某种原因无法使用? 遍历你的视图中的所有视图,superview,也许是childview,然后首先找到你的pageController。 我刚刚尝试将此代码添加到 UIPageController.m: - (IBAction)closeAll:(id)sender UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"MainVC"]; vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizo​​ntal; [self presentViewController:vc动画:YES完成:NULL];它现在说:尝试在 上呈现 其视图不在窗口层次结构中!? 下面的按钮不起作用的原因是 UIPageController 还在。 尝试将框架设置为 CGRectZero 【参考方案1】:

我想我在appcoda 上做了同样的教程,并且有同样的问题!

我在another question 上偶然发现了一个对我有用的解决方案:

// Replace deleteVC with your UIPageViewController
[deleteVC willMoveToParentViewController:nil];
[deleteVC.view removeFromSuperview];
[deleteVC removeFromParentViewController]; 

【讨论】:

【参考方案2】:
var movingIndex = 0
var forwardDirection = false

    if selectedIndex == images.count //ur pages dataSource You may not need this
            movingIndex = selectedIndex - 1
            forwardDirection = false
         else 
            movingIndex = selectedIndex
            forwardDirection = true
        
    
    controllers.remove(at: selectedIndex)
    capturedInspectionPhotoView.imagePageController?.dataSource = self
    capturedInspectionPhotoView.imagePageController?.setViewControllers([controllers[movingIndex]],
                                            direction: forwardDirection ? .forward : .reverse, animated: true)

【讨论】:

以上是关于移除 PageViewController的主要内容,如果未能解决你的问题,请参考以下文章

移除元素---原地移除

移除元素---原地移除

NSNotificatinonCenter iOS9以后不再需要移除观察者-备忘

移除元素

PHP 中怎样移除节制字符

重构方法并移除额外的局部变量(移除绑定)