Search code examples
iosobjective-cswiftuipageviewcontroller

How to change button name in UIPageViewController


I have UIPageViewController related app made using http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

How can i change the title of the UIButton when UI comes to last page, and the UIButton is in the UIViewController which contains UIPageViewController?

Thanks


Solution

  • Having read through the tutorial, here's what you need to do:

    1. Add a property of the PageContentViewController (header file) for your button title
    @property NSString *buttonText;
    
    1. In the PageContentViewController (implementation .m file), within the viewDidLoad method, set the title of your button:
    if (self.buttonText != nil) {    
       [self.button setTitle:self.buttonText forState:UIControlStateNormal];
    }
    
    1. In the storyboard, add an outlet for your button to your view controller, similar to what you did for the titleLabel, and call the outlet "button"
    2. Finally, in your viewControllerAtIndex method of your ViewController.m, set the pageContentViewController.buttonText to either your default button text or your SPECIAL button text.

    To do this, after the page index is set, you could do something like this:

    // other initialization
    pageContentViewController.pageIndex = index;
    if (index == [self.pageTitles count] - 1) {
       pageContentViewController.buttonText = @"Your special text";
    }
    

    Because the button's natural text will only be overwritten if it's set, it should only change the text if the user is on the last page!