Cannot create a view with a UIScrollView - iphone

I am looking for some help for a program I am developing. Basically, it contains two views. The first view has a button and a action related to it leads me to my next view where there is a scroll view for three pages ( as in the PageControl sample given by apple.) I am using navigation controller to push the view from the first view to the second view. But on pushing I am not able to scroll the page or using page control or anything. It just comes a normal view. The code is same as pageControl code and it works fine as a separate module. But when I want to integrate this pageControl code with a first view and a click button it doesn't work. Please help.
//this function is an action set against a button in the rootviewcontroller.
-(IBAction) viewScrollPages:(id)sender{
UIViewController *controller = [[MyViewController alloc] initWithPageNumber:0];
[self.navigationController pushViewController:controller animated:NO];
[controller release];
}
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:#"MyViewController" bundle:nil]) {
pageNumber = page;
}
return self;
}
- (void)viewDidLoad {
NSMutableArray *controllers = [[NSMutableArray alloc] init];
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;
// 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];
pageNumberLabel.text = [NSString stringWithFormat:#"Page %d", pageNumber + 1];
self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
[super viewDidLoad];
}

It sounds like your pushing a normal view that is a subview of the scrollview instead of pushing the scrollview itself.

Related

Handle UIButton in Child View Controller of Container View Controller

I am working on creating custom Container ViewController class and am able to create 5 sub viewcontrollers and able to move from one child view controller to other child using the following API:
transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:0
animations:^{
}
completion:^(BOOL finished) {
}];
Child view controllers are occupying part of Parent View Controller. Each time when user swipe I am making previous controller goes off and present controller comes on displayable area.
Now I have buttons on child viewcontrollers and when i click on them I am not getting events or trigger to the Action method of Child View Controller. But Parent View Controller is getting callbacks for its buttons even currently childview is getting displayed. Adding the code snippet below (though it is little bigger, this will help you to analyze if I am doing any mistake).
Parent ViewController:
- (void) handleSwipeGestureLeft:(UISwipeGestureRecognizer *)gesture
{
if (showPopupValue) {
return;
}
NSInteger index = [arrayOfChildVCs indexOfObject:selectedViewController];
index = MIN(index+1, [arrayOfChildVCs count]-1);
UIViewController *newSubViewController = [arrayOfChildVCs objectAtIndex:index];
endFrame.origin.x = -1024;
startOfNewVCFrame.origin.x = 1024;
[self transitionFromViewController:selectedViewController toViewController:newSubViewController];
selectedViewController = newSubViewController;
}
- (void) handleSwipeGestureRight:(UISwipeGestureRecognizer *)gesture
{
if (showPopupValue) {
return;
}
NSInteger index = [arrayOfChildVCs indexOfObject:selectedViewController];
index = MAX(index-1, 0);
UIViewController *newSubViewController = [arrayOfChildVCs objectAtIndex:index];
endFrame.origin.x = 1024;
startOfNewVCFrame.origin.x = -1024;
[self transitionFromViewController:selectedViewController toViewController:newSubViewController];
selectedViewController = newSubViewController;
}
- (void) populateChildVCList
{
if (self.currentactivity == EPI_Normal)
{
arrayOfImageNames = [[NSArray alloc]initWithObjects:#"AD_Initiate_SPI_Normal.jpg", #"AD_Initiate_CPI_Normal.jpg", #"AD_Initiate_EAC_Normal.jpg", #"AD_Initiate_PV_Normal.jpg", #"AD_Initiate_EV_Normal.jpg", nil];
}
else
{
arrayOfImageNames = [[NSArray alloc]initWithObjects:#"AD_Initiate_SPI_Trigger.jpg", #"AD_Initiate_CPI_Trigger.jpg", #"AD_Initiate_EAC_Trigger.jpg", #"AD_Initiate_PV_Trigger.jpg", #"AD_Initiate_EV_Trigger.jpg", nil];
isTriggerOn = YES;
}
arrayOfChildVCs = [[NSMutableArray alloc] init];
[arrayOfChildVCs addObject:[[PopupDummyViewController alloc]initWithNibName:#"PopupDummyViewController" bundle:nil]]; // Note: Add one dummy VC to initiate swap
for (int vcIndex = 0; vcIndex < [arrayOfImageNames count]; vcIndex++)
{
PiSLAComplianceViewController *childPopupController = [[PiSLAComplianceViewController alloc]initWithNibName:#"PiSLAComplianceViewController" bundle:nil];
childPopupController.popUpImageName = [arrayOfImageNames objectAtIndex:vcIndex];
childPopupController.imageIndex = vcIndex;
childPopupController.isTriggerOn = isTriggerOn;
[arrayOfChildVCs addObject:childPopupController];
}
selectedViewController = [arrayOfChildVCs objectAtIndex:0];
selectedViewController.view.frame = CGRectMake(0, 100, 100, 511); // Took the imageview coordinates
// Add the dummy view controller to Container View Initially
[self addChildViewController:selectedViewController];
[self.view addSubview:selectedViewController.view];
// notify it that move is done
[selectedViewController didMoveToParentViewController:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[super viewDidLoad];
UISwipeGestureRecognizer *swipeGestureRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureRight:)];
[swipeGestureRecognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];
[ self.view addGestureRecognizer:swipeGestureRecognizerRight];
UISwipeGestureRecognizer *swipeGestureRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureLeft:)];
[swipeGestureRecognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[ self.view addGestureRecognizer:swipeGestureRecognizerLeft];
endFrame = deliverableimageview.frame;
startOfNewVCFrame = deliverableimageview.frame;
[self populateChildVCList]; // To Create list of child view controllers and loads one invisible dummy view controller to be ready for swiping
}
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
if (fromViewController == toViewController)
{
// cannot transition to same
return;
}
// animation setup
toViewController.view.frame = startOfNewVCFrame;
// notify
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
// transition
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:0
animations:^{
toViewController.view.frame = fromViewController.view.frame;
fromViewController.view.frame = endFrame;
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
}

How does UIScrollView work with StoryBoard and show different UIViewControllers?

I have a StoryBoard. In that there is a UIViewController - on that I have put UIScrollView and UIPageControl. And on each page (say I have 3) I am trying to load a UIViewController (that shows different labels and table values).
Problem - I am able to load everything up but the label and table is not visible. Thou when I try to change the background color for each page - it does gets changed. I have researched various posts and code but nothing seems to work / show UIScrollView with StoryBoard (with UIViewController displaying labels and table).
I am thou able to do this with loading a separate XIB file but then the whole segue gets interrupted.
Please see my code below.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//ScrollView and PageControl
// Create view controllers placeholder array
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
// Set Scroll View
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
// Set Page Control
pageControl.numberOfPages = numberOfPages;
pageControl.currentPage = 0;
// Load the visible and next page
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed)
{
// To know if scroll is valid - from Page Control
return;
}
// Changes Page Control indicator
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// Load the visible, previous and next pages
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0) return;
if (page >= numberOfPages) return;
// Load new Controller
ReportsDetailViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[ReportsDetailViewController alloc] init];
controller.pageNumber = [NSNumber numberWithInt:page];
[viewControllers replaceObjectAtIndex:page withObject: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];
}
}
Try to create your ViewControllers like this:
YourViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"YourViewController"];
You can set the identifier of your view controller in the attribute inspector.
I had the same problem today and this did the trick for me.

UIScrollView and UIViews

I want to use a UIScrollView to display multiple UIViews as the user scrolls through the UIScrollView control. I'm not worried about showing Pagination just yet.
I already managed to implement some of it, but is not working the way I want it to.
Currently:
I have 3 ViewControllers with different nib files. The root view controller is the one with the UIScrollView, and it to load the rest of the view controllers.
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
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;
[self loadScrollViewWithPage:0];
- (void)loadScrollViewWithPage:(int)page {
if (page == 0) {
PageTwo *controller2 = [viewControllers objectAtIndex:page];
if ((NSNull *)controller2 == [NSNull null]) {
controller2 = [[PageTwo alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller2];
[controller2 release];
}
// add the controller's view to the scroll view
if (nil == controller2.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller2.view.frame = frame;
[scrollView addSubview:controller2.view];
}
}
if (page == 1) {
// replace the placeholder if necessary
NewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[NewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
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];
}
}
That's all fine... However, what I want it to do is to load the root nib first. But I'm not sure how to go about this. I know I have to increase the number of pages to 3, but when I initialize the controllers I don't know how to tell it that page == 0 should be the current view.
Any ideas?
Update:
Sigh, I overlooked something.. Didn't notice that if don't specify a page at level 0 it just shows the current view hah!
Silly me.
I overlooked something.. Didn't notice that if don't specify a page at level 0 it just shows the current view hah!

UIScrollView frame size issue in Landscape Mode

I'm trying to make UIScrollView auto-resize and appear correctly in Landscape mode. Portrait mode works fine.
Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
//=== load all the custom view
NSMutableArray *controllers = [[NSMutableArray alloc] init];
int page = 0;
[controllers addObject:[self loadScrollView:[[Page1ViewController alloc] initWithNibName:#"Page1ViewController" bundle:nil] withPage:page++]];
[controllers addObject:[self loadScrollView:[[Page2ViewController alloc] initWithNibName:#"Page2ViewController" bundle:nil] withPage:page++]];
[controllers addObject:[self loadScrollView:[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil] withPage:page++]];
self.viewControllers = controllers;
[controllers release];
//=== automaticaly define number of pages
_numberOfPages = [self.viewControllers count];
// a page is the width of the scroll view
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * _numberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
// First, set the number of pages
pageControl.numberOfPages = _numberOfPages;
pageControl.currentPage = 0;
}
Then, I went into each view controller nib file and set the background to different color to test if it's working. Unfortunately, when in Landscape mode, the width and height remains the same as in Portrait Mode.
I'm not sure how to fix this problem. Hope anyone can help me.
Many thanks in advance.
I'm having the same problem, but only if I open the view in Landscape, when it was designed in the .xib in Portrait.
If I open in portrait, then rotate the device, it sizes correctly.
I've found that if I load my view controllers array in viewWillAppear, rather than in viewDidLoad, it sizes correctly all the time.

How to set the default page on the right side instead on the left (reverse) when launching App that used UIPageControl of UIScrollView?

My Case:
I have original Page Control app from Apple. I would like to modify the launch of the app on right side on page control at the bottom of UIScrollView?
By default, when launch - it is on the first page which is on the left. Say for example, i have 7 pages. So, I want the first page appear on the last page which is on the 7th page and second page page on the 6th page and so on. In short, the reverse of the default.
I know there have to be one code that do this trick. I'm not sure what it is.
Please help. Thanks!
Below is description of Page Control app from Apple:
PageControl
This application demonstrates use of UIScrollView's paging functionality to use horizontal scrolling as a mechanism for navigating between different pages of content. Each page is managed by its own view controller which is loaded only when it is needed. A UIPageControl is displayed at the bottom of the window as an alternative interface for moving between pages
The Code in AppDelegate.m
#import "AppDelegate.h"
#import "MyViewController.h"
static NSUInteger kNumberOfPages = 7;
#interface AppDelegate (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
#end
#implementation AppDelegate
#synthesize window, scrollView, pageControl, viewControllers;
- (void)dealloc {
[viewControllers release];
[scrollView release];
[pageControl release];
[window release];
[super dealloc];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
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;
// 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];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
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];
}
}
- (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 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
Taking the PageControl sample from Apple, here's the code that would do this. I tested it and it works fine, except for noticing a jump of the page control when you launch the app from the initial dot to the last one, but I don't know how to solve that. Let me know if this doesn't work for you.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
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;
// First, set the number of pages
pageControl.numberOfPages = kNumberOfPages;
// Then, tell the page control that you're on the last page (numPages - 1, since we're 0-indexed)
pageControl.currentPage = kNumberOfPages - 1;
// Now, we have to tell the frame that we're at the end of the frame
// So we set the content offset to be the last page
scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * (kNumberOfPages - 1), 0);
// 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];
// Now, instead of loading the original page, we load the last two pages
[self loadScrollViewWithPage:kNumberOfPages - 1];
[self loadScrollViewWithPage:kNumberOfPages - 2];
}
Well you can just set your scrollViews offset to be the last page and so then it will start at t he last page and you have to scroll to the right instead of the left...use UIScrollViews - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated method.