2014-10-10 124 views
1

我想關閉頁面視圖控制器與IBAction按鈕,我可以關閉視圖(PageContentViewController),新視圖(LoginViewController)顯示,但頁面控制的「點」仍然在這裏,並且我無法與新視圖(LoginViewController)進行通信。 我在做什麼錯?removeFromSuperView與頁面視圖控制器

這裏是我的代碼:

LoginViewController.h:

#import "PageContentViewController.h" 
@interface LoginViewController : UIViewController <UIPageViewControllerDataSource> 
@property (strong, nonatomic) UIPageViewController *pageViewController; 
@property (strong, nonatomic) NSArray *pageTitles; 
@property (strong, nonatomic) NSArray *pageImages; 
@end 

LoginViewController.m:

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
     // Create the data model 
     _pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark Favorite Tip", @"Free Regular Update"]; 
     _pageImages = @[@"FIRST_img.jpg", @"FIRST_img.jpg", @"FIRST_img.jpg", @"FIRST_img.jpg"]; 

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

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



     // Change the size of page view controller 
     self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30); 

     [self addChildViewController:_pageViewController]; 
     [self.view addSubview:_pageViewController.view]; 
     [self.pageViewController didMoveToParentViewController:self]; 

    } 


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

    // Create a new view controller and pass suitable data. 
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"]; 
    pageContentViewController.imageFile = self.pageImages[index]; 
    pageContentViewController.titleText = self.pageTitles[index]; 
    pageContentViewController.pageIndex = index; 

    return pageContentViewController; 
} 


#pragma mark - Page View Controller Data Source 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex; 

    if ((index == 0) || (index == NSNotFound)) { 
     return nil; 
    } 

    index--; 
    return [self viewControllerAtIndex:index]; 
} 

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

    if (index == NSNotFound) { 
     return nil; 
    } 

    index++; 
    if (index == [self.pageTitles count]) { 
     return nil; 
    } 
    return [self viewControllerAtIndex:index]; 
} 

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController 
{ 
    return [self.pageTitles count]; 
} 

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController 
{ 
    return 0; 
} 

PageContentViewController.h:

@interface PageContentViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView; 
@property (weak, nonatomic) IBOutlet UILabel *titleLabel; 
@property NSUInteger pageIndex; 
@property NSString *titleText; 
@property NSString *imageFile; 
- (IBAction)dismissButton:(id)sender; 
@end 

PageContentView Controller.m:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.backgroundImageView.image = [UIImage imageNamed:self.imageFile]; 
    self.titleLabel.text = self.titleText; 

} 

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

- (IBAction)dismissButton:(id)sender { 
    [self.view performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO]; 
    [self performSelectorOnMainThread:@selector(removeFromParentViewController) withObject:nil waitUntilDone:NO]; 


} 
@end 

回答

2

您刪除PageContentViewController而不是PageViewController(它仍然存在)。 嘗試在dismissButton方法[self.view.superview使用...

編輯:

的好辦法,就是從父與標準代碼中刪除父:

[self willMoveToParentViewController:nil]; 
    [self.view removeFromSuperview];    
    [self removeFromParentViewController]; 

但要做到這一點,你做的,從孩子父母刪除,然後嘗試使用如下代碼:

[self.parentViewController willMoveToParentViewController:nil]; 
    [self.parentViewController.view removeFromSuperview];    
    [self.parentViewController removeFromParentViewController]; 

或[self.parentViewController.parentViewController ...它DEPE發現層次結構。

+0

首先,感謝您的回覆!我已經替換「[self.view performSelectorOnMainThread:@selector(removeFromSuperview)withObject:nil waitUntilDone:NO];」與「[self.view.superview performSelectorOnMainThread:@selector(removeFromSuperview)withObject:nil waitUntilDone:NO];」但它是一樣的問題...... :( – Viny76 2014-10-10 18:59:20

+0

更新了答案) – kabarga 2014-10-10 19:41:21

+0

謝謝你!這工作完美! – Viny76 2014-10-10 20:23:44

相關問題