• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by theswagman · Apr 05, 2013 at 03:58 AM · buttoncontrollerxcodescrollview

Pagecontrol not working?

So i set up four view controllers.With uiscrollview and page control to swipe between the views. I set a uibutton on the second page. When the button is clicked it goes to a page with a web view. When you click back from the webview viewcontroller and go back to the second page with the uibutton, pagecontrol doesnt work anymore. Here is the code for the page control.

 @interface PagerViewController ()
 @property (assign) BOOL pageControlUsed;
 @property (assign) NSUInteger page;
 @property (assign) BOOL rotating;
 - (void)loadScrollViewWithPage:(int)page;
 @end
 
 @implementation PagerViewController
 
 @synthesize scrollView;
 @synthesize pageControl;
 @synthesize pageControlUsed = _pageControlUsed;
 @synthesize page = _page;
 
 
 - (void)viewDidLoad
 {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     [self.scrollView setPagingEnabled:YES];
     [self.scrollView setScrollEnabled:YES];
     [self.scrollView setShowsHorizontalScrollIndicator:NO];
     [self.scrollView setShowsVerticalScrollIndicator:NO];
     [self.scrollView setDelegate:self];
 }
 
 - (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
     return NO;
 }
 
 
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
 
     for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
         [self loadScrollViewWithPage:i];
     }
 
     self.pageControl.currentPage = 0;
     _page = 0;
     [self.pageControl setNumberOfPages:[self.childViewControllers count]];
 
     UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
     if (viewController.view.superview != nil) {
         [viewController viewWillAppear:animated];
     }
 
     self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
 }
 
 - (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
 
     if ([self.childViewControllers count]) {
         UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
         if (viewController.view.superview != nil) {
             [viewController viewDidAppear:animated];
         }
     }
 }
 
 - (void)viewWillDisappear:(BOOL)animated {
     if ([self.childViewControllers count]) {
         UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
         if (viewController.view.superview != nil) {
             [viewController viewWillDisappear:animated];
         }
     }
     [super viewWillDisappear:animated];
 }
 
 - (void)viewDidDisappear:(BOOL)animated {
     UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
     if (viewController.view.superview != nil) {
         [viewController viewDidDisappear:animated];
     }
     [super viewDidDisappear:animated];
 }
 
 - (void)loadScrollViewWithPage:(int)page {
     if (page < 0)
         return;
     if (page >= [self.childViewControllers count])
         return;
 
     // replace the placeholder if necessary
     UIViewController *controller = [self.childViewControllers objectAtIndex:page];
     if (controller == nil) {
         return;
     }
 
     // add the controller's view to the scroll view
     if (controller.view.superview == nil) {
         CGRect frame = self.scrollView.frame;
         frame.origin.x = frame.size.width * page;
         frame.origin.y = 0;
         controller.view.frame = frame;
         [self.scrollView addSubview:controller.view];
     }
 }
 
 - (void)previousPage {
     if (_page - 1 > 0) {
 
         // update the scroll view to the appropriate page
         CGRect frame = self.scrollView.frame;
         frame.origin.x = frame.size.width * (_page - 1);
         frame.origin.y = 0;
 
         UIViewController *oldViewController = [self.childViewControllers objectAtIndex:_page];
         UIViewController *newViewController = [self.childViewControllers objectAtIndex:_page - 1];
         [oldViewController viewWillDisappear:YES];
         [newViewController viewWillAppear:YES];
 
         [self.scrollView scrollRectToVisible:frame animated:YES];
 
         self.pageControl.currentPage = _page - 1;
         // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
         _pageControlUsed = YES;
     }
 }
 
 - (void)nextPage {
     if (_page + 1 > self.pageControl.numberOfPages) {
 
         // update the scroll view to the appropriate page
         CGRect frame = self.scrollView.frame;
         frame.origin.x = frame.size.width * (_page + 1);
         frame.origin.y = 0;
 
         UIViewController *oldViewController = [self.childViewControllers objectAtIndex:_page];
         UIViewController *newViewController = [self.childViewControllers objectAtIndex:_page + 1];
         [oldViewController viewWillDisappear:YES];
         [newViewController viewWillAppear:YES];
 
         [self.scrollView scrollRectToVisible:frame animated:YES];
 
         self.pageControl.currentPage = _page + 1;
         // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
         _pageControlUsed = YES;
     }
 }
 
 - (IBAction)changePage:(id)sender {
     int page = ((UIPageControl *)sender).currentPage;
 
     // update the scroll view to the appropriate page
     CGRect frame = self.scrollView.frame;
     frame.origin.x = frame.size.width * page;
     frame.origin.y = 0;
 
     UIViewController *oldViewController = [self.childViewControllers objectAtIndex:_page];
     UIViewController *newViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
     [oldViewController viewWillDisappear:YES];
     [newViewController viewWillAppear:YES];
 
     [self.scrollView scrollRectToVisible:frame animated:YES];
 
     // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
     _pageControlUsed = YES;
 }
 
 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
     UIViewController *oldViewController = [self.childViewControllers objectAtIndex:_page];
     UIViewController *newViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
     [oldViewController viewDidDisappear:YES];
     [newViewController viewDidAppear:YES];
 
     _page = self.pageControl.currentPage;
 }
 
 #pragma mark -
 #pragma mark UIScrollViewDelegate methods
 
 - (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 || _rotating) {
         // 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 = self.scrollView.frame.size.width;
     int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
     if (self.pageControl.currentPage != page) {
         UIViewController *oldViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
         UIViewController *newViewController = [self.childViewControllers objectAtIndex:page];
         [oldViewController viewWillDisappear:YES];
         [newViewController viewWillAppear:YES];
         self.pageControl.currentPage = page;
         [oldViewController viewDidDisappear:YES];
         [newViewController viewDidAppear:YES];
         _page = page;
     }
 }
 
 // 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;
 }
 
 @end
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chronos-L · Apr 05, 2013 at 04:30 AM 0
Share

Please format your code, highlight them and press Ctrl-$$anonymous$$. I did it for you this once.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Controlling GUI with Xbox 360 Controller 1 Answer

Make a button selectable for controller but not interactable 0 Answers

RPG Maker style menu with UI buttons that are selected by controller 0 Answers

Make button inside ScrollView move when ScrollView scrolls 1 Answer

How to know if finger is on joystick if it is at horizontal & vertical zero 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges