help with view controller

Discussion in 'iOS Programming' started by cinek, Dec 4, 2011.

  1. cinek macrumors 6502

    Joined:
    Mar 12, 2011
    #1
    I'm having a bit of a problem with the view controller.

    the MainWindow.xib has a view controller which is linked to MainViewViewController.h - the main view is the main view for the home screen. I'm then importing few buttons onto a scrollview from FirstPageViewController. I now want to open another view from the FirstPageViewController using:

    Code:
    [self presentModalViewController: newView animated:YES];
    but that opens it in the scroll view where FirstPageViewViewController is being added as a subview instead of opening it from MainViewViewController

    the scroll view only takes up half the screen

    it works fine if I try opening a page using:
    Code:
    [self presentModalViewController: newView animated:YES];
    from MainViewViewController - but not the FirstPageViewController

    I have no idea how to fix this

    any ideas?
     
  2. jonnymo5, Dec 4, 2011
    Last edited by a moderator: Dec 7, 2011

    jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #2
    I think I understand what you are trying to do. Try this.

    Code:
    [self.parentViewController presentModalViewController: newView animated:YES];
     
  3. cinek, Dec 4, 2011
    Last edited by a moderator: Dec 7, 2011

    cinek thread starter macrumors 6502

    Joined:
    Mar 12, 2011
    #3
    thanks. I tried that, but it doesn't do anything :S the view doesn't open

    I tried NSLogging presenting and parentviewcontrollers - both come up as null

    not sure if this helps - but I'm using ARC
     
  4. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #4
    If all you're doing is adding FirstViewController's view as a subview of MainViewController then you are breaking the view controller hierarchy that iOS expects. If you log the viewDid* and viewWill* methods in FirstViewController you'll probably see that they're never being called (or called at inappropriate times). Since you're breaking the hierarchy, calling self.parentViewController won't work since you haven't set up the parent view controller.

    If you're targeting iOS 5 you should look into the new containment API for view controllers. You'll want to make sure you add FirstViewController as a child of MainViewController and all should work.

    If you're targeting pre-iOS 5 you'll most likely need to set up a reference to MainViewController from FirstViewController and manage the view life cycle methods yourself. It's much easier and cleaner if you're targeting iOS 5 and can take advantage of the new containment methods.
     
  5. cinek thread starter macrumors 6502

    Joined:
    Mar 12, 2011
    #5
    thanks, I think I'll target iOS 5 +. How can I add the FirstViewController as a child of MainViewController?
     
  6. cinek thread starter macrumors 6502

    Joined:
    Mar 12, 2011
    #6
    I have added this in MainViewController - in viewDidLoad

    Code:
    FirstPageViewController *firstPageViewController = [[FirstPageViewController alloc] initWithNibName:@"FirstPageViewController" bundle:nil];
        [self addChildViewController:firstPageViewController];
    but it still doesn't work ;/
     
  7. cinek thread starter macrumors 6502

    Joined:
    Mar 12, 2011
    #7
    I managed to load the view, but now when the video plays from the view, and I click done, the the main view opens instead of the videoview and everything is shifted about 20px up. Any ideas what's wrong?



    here's my code for the main view


    Code:
    
    
    #import "MainViewViewController.h"
    #import "FirstPageViewController.h"
    #import "AboutViewController.h"
    
    static NSUInteger kNumberOfPages = 1;
    
    @interface MainViewViewController (PrivateMethods)
    
    -(void)loadScrollViewWithPage:(int)page;
    -(void)scrollViewDidScroll:(UIScrollView *)sender;
    
    @end
    
    @implementation MainViewViewController
    
    @synthesize pageControl, scrollView, viewControllers;
    /*
    -(void)addChildViewController:(UIViewController *)childController
    {
        FirstPageViewController *fpvc = [[FirstPageViewController alloc] initWithNibName:@"FirstPageViewController" bundle:nil];
        [self addChildViewController:fpvc];
    }
    */
    -(IBAction)showInfo:(id)sender
    {
        AboutViewController *aboutView = [[AboutViewController alloc] init];
        [aboutView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
        [self presentModalViewController:aboutView animated:YES];
    }
    
    -(IBAction)changePage:(id)sender
    {
        int page = pageControl.currentPage;
        [self loadScrollViewWithPage:page - 1];
        [self loadScrollViewWithPage:page];
        [self loadScrollViewWithPage:page - 1];
        
        
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        [scrollView scrollRectToVisible:frame animated:YES];
        
        pageControlUsed = YES;
    }
    
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        pageControlUsed = NO;
    }
    
    -(void)scrollViewDidScroll:(UIScrollView *)sender
    {
        if(pageControlUsed)
        {
            return;
        }
        
        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        pageControl.currentPage = page;
        
        [self loadScrollViewWithPage:page - 1];
        [self loadScrollViewWithPage:page];
        [self loadScrollViewWithPage:page + 1];
    }
    
    -(void)loadScrollViewWithPage:(int)page
    {
        if(page < 0) return;
        if(page >= kNumberOfPages) return;
        
        FirstPageViewController *controller = [viewControllers objectAtIndex:page];
        if((NSNull *)controller == [NSNull null]){
            controller = [[FirstPageViewController alloc] initWithPageNumber:1];
            //[viewControllers replaceObjectAtIndex:page withObject:controller];
            [self addChildViewController:controller];
        }
        
        if(nil == controller.view.superview){
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            controller.view.frame = frame;
            [scrollView addSubview:controller.view];
        }
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
       // FirstPageViewController *firstPageViewController = [[FirstPageViewController alloc] initWithNibName:@"FirstPageViewController" bundle:nil];
        //[self addChildViewController:firstPageViewController];
     
        NSMutableArray *controllers = [[NSMutableArray alloc] init];
        for(unsigned i = 0; i < kNumberOfPages; i++){
            [controllers addObject:[NSNull null]];
        }
        
        self.viewControllers = controllers;
        
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.scrollsToTop = NO;
        scrollView.delegate = self;
        
        pageControl.numberOfPages = kNumberOfPages;
        pageControl.currentPage = 0;
        
        [self loadScrollViewWithPage:0];
        [self loadScrollViewWithPage:1];
        
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    
    
    
    
     

Share This Page