views:

392

answers:

0

Hi everyone!

how can i push a view from a subview?

i've implemented a paging scroll view with custom view. each view has some button and from this buttom i want to push other view into navigation controller.

how can i do it?

thanks

this is the code: RadicalResearchViewController.h

 #import <UIKit/UIKit.h>

@class ListOneViewController;
@class ListTwoViewController;
@class ListThreeViewController;
@class ListFourViewController;

@interface RadicalResearchViewController : UIViewController<UIScrollViewDelegate> {
 UIScrollView *scrollView;
 UIPageControl *pageControl;
    NSMutableArray *viewControllers;
 ListOneViewController *lista1;
 ListTwoViewController *lista2;
 ListThreeViewController *lista3;
 ListFourViewController *lista4;

 // To be used when scrolls originate from the UIPageControl
    BOOL pageControlUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *viewControllers;
@property (nonatomic, retain) ListOneViewController *lista1;
@property (nonatomic, retain) ListTwoViewController *lista2;
@property (nonatomic, retain) ListThreeViewController *lista3;
@property (nonatomic, retain) ListFourViewController *lista4;

- (IBAction)changePage:(id)sender;

@end

RadicalResearchViewController.m

#import "RadicalResearchViewController.h"
#import "ListOneViewController.h"
#import "ListTwoViewController.h"
#import "ListThreeViewController.h"
#import "ListFourViewController.h"


static NSUInteger kNumberOfPages = 4;

@interface RadicalResearchViewController (PrivateMethods)

- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;

@end

@implementation RadicalResearchViewController
@synthesize scrollView, pageControl, viewControllers, lista1, lista2, lista3, lista4;

- (void)dealloc {
 [lista1 release];
 [lista2 release];
 [lista3 release];
 [lista4 release];
    [viewControllers release];
    [scrollView release];
    [pageControl release];
 [super dealloc];
}

- (void)viewDidLoad{
    // view controllers are created lazily
    // in the meantime, load the array with placeholders which will be replaced on demand
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
 //PageOneViewController *view1 =
    for (unsigned i = 0; i < kNumberOfPages; i++) {

        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    [controllers release];

    // a page is the width of the scroll view
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;

 pageControl.numberOfPages = kNumberOfPages;

    pageControl.currentPage = 0;
 NSArray *lista = [self.navigationController viewControllers];
 NSLog(@"num elementi lista: %d",[lista count]);


    // pages are created on demand
    // load the visible page
    // load the page on either side to avoid flashes when the user starts scrolling
    [self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
}

- (IBAction)changePage:(id)sender {
    int page = pageControl.currentPage;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

 // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];

 // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed = YES;
}


- (void)loadScrollViewWithPage:(int)page {
 NSLog(@"page =%d",page);
    if (page < 0) return;
    if (page >= kNumberOfPages) return;

 if (page == 0){
  lista1 = [viewControllers objectAtIndex:page];
  if((NSNull *)lista1 == [NSNull null]) {
   lista1 = [[ListOneViewController alloc] initWithNibName:@"ListOneViewController" bundle:nil];

   [viewControllers replaceObjectAtIndex:page withObject:lista1];
   [lista1 release];

  }

  // add the controller's view to the scroll view
  if (nil == lista1.view.superview) {
   CGRect frame = scrollView.frame;
   frame.origin.x = frame.size.width * page;
   frame.origin.y = 0;
   lista1.view.frame = frame;
   [scrollView addSubview:lista1.view];
   //[self.navigationController pushViewController:lista1 animated:YES]; //Here i can push new view into the navigation's stack ban i cannot scroll into the other pages
  }
 }else if (page == 1){
  lista2 = [viewControllers objectAtIndex:page];
  if((NSNull *)lista2 == [NSNull null]) {
   lista2 = [[ListTwoViewController alloc] initWithNibName:@"ListTwoViewController" bundle:nil];

   [viewControllers replaceObjectAtIndex:page withObject:lista2];
   [lista2 release];
  }

  // add the controller's view to the scroll view
  if (nil == lista2.view.superview) {
   CGRect frame = scrollView.frame;
   frame.origin.x = frame.size.width * page;
   frame.origin.y = 0;
   lista2.view.frame = frame;
   [scrollView addSubview:lista2.view];
  }
 }else if (page == 2){
  lista3 = [viewControllers objectAtIndex:page];
  if((NSNull *)lista3 == [NSNull null]) {
   lista3 = [[ListThreeViewController alloc] initWithNibName:@"ListThreeViewController" bundle:nil];
   [viewControllers replaceObjectAtIndex:page withObject:lista3];
   [lista3 release];
  }

  // add the controller's view to the scroll view
  if (nil == lista3.view.superview) {
   CGRect frame = scrollView.frame;
   frame.origin.x = frame.size.width *page;
   frame.origin.y = 0;
   lista3.view.frame = frame;
   [scrollView addSubview:lista3.view];
  }
 }else if (page == 3){
  lista4 = [viewControllers objectAtIndex:page];
  if((NSNull *)lista4 == [NSNull null]) {
   lista4 = [[ListFourViewController alloc] initWithNibName:@"ListFourViewController" bundle:nil];
   [viewControllers replaceObjectAtIndex:page withObject:lista4];
   [lista4 release];
  }

  // add the controller's view to the scroll view
  if (nil == lista4.view.superview) {
   CGRect frame = scrollView.frame;
   frame.origin.x = frame.size.width *page;
   frame.origin.y = 0;
   lista4.view.frame = frame;
   [scrollView addSubview:lista4.view];
  }
 }
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
    if (pageControlUsed) {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

    // A possible optimization would be to unload the views+controllers which are no longer visible
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}
/*

- (IBAction)changePage:(id)sender {
    int page = pageControl.currentPage;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];

 // update the scroll view to the appropriate page
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];

 // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
    pageControlUsed = YES;
}
*/
@end

ListOneViewController .h #import

@class RadicalListKanjiViewController;
@interface ListOneViewController : UIViewController<UIScrollViewDelegate> {
 UIScrollView *scrollView;
 RadicalListKanjiViewController *childController;
 UIButton *myButton;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) RadicalListKanjiViewController *childController;
@property (nonatomic, retain) IBOutlet UIButton *myButton;

- (IBAction)buttonPressed:(id)sender;


@end

ListOneViewController .m

    #import "ListOneViewController.h"
#import "KanjiDetailViewController.h"
#import "RadicalListKanjiViewController.h"
#import "DizionarioAppDelegate.h"
#import "RadicalResearchViewController.h"

@implementation ListOneViewController
@synthesize scrollView, childController, myButton;

- (void)viewDidLoad{
    // a page is the width of the scroll view
    scrollView.pagingEnabled = NO;
    scrollView.contentSize = CGSizeMake(320, 1600);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.scrollsToTop = YES;
    scrollView.delegate = self;
}

- (IBAction)buttonPressed:(id)sender {
 /*
 NSLog(@"buttonPressed usato");
    Radicale = [sender titleForState:UIControlStateNormal];
 NSLog(@"radicale scelto: %@", Radicale);
  */
 if (childController == nil){
  NSLog(@"è nil");
  childController = [[RadicalListKanjiViewController alloc] initWithNibName:@"RadicalListKanjiViewController" bundle:nil];
 }
 //childController.radicale = Radicale;
 NSLog(@"oggetto creato: %@", childController);

 [self.navigationController pushViewController:childController animated:YES];

}
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [scrollView release];
 [super dealloc];
}

@end