0
votes

ill try to explain this as best i can. i have a UIViewController. inside it there is a 'SKIP' button and theres also a UIContainerView. embedded in that container view is a UIPageViewController. the Page View Controller have 4 pages.

i want to be able to make the 'SKIP' button (in the parent UIViewController) to have a different color for each page in the PageViewController. Example: if page == 1, SKIP.color = white. if page == 2, SKIP.color = blue...

i dont understand the proper way to call a method inside the parent from the child PageViewController.

any help would be appreciated

2
Check out delegates in iOS/ NSNotificationCentre7vikram7

2 Answers

1
votes

You can either use delegation pattern or NSNotification.

Delegation

Set the parentVC as the pageVC's delegate and remember that the parentVC must conform to page view controller's delegate protocol

class ParentClass: UIViewController, UIPageViewControllerDelegate {
    // ...
    pageInstanceVC.delegate = self
}

and then implement its delegate method (this is where you change the button's color), you might want to implement it in - pageViewController:willTransitionToViewControllers: or - pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: Complete docs can be found here

Notification

Set parentVC to listen to page change notification and implement the required method when the notification is received

// Parent VC 
override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeButtonColor", name: "kPageChangeNotif", object: nil)
}

func changeButtonColor(notification: NSNotification) {
    let userInfo = notification.userInfo as Dictionary
    let pageNumber = userInfo["PageNumber"]

    // Change the button color
    // .....
}

Then sent out notification when page is changed

// PageVC
NSNotificationCenter.defaultCenter().postNotificationName("kPageChangeNotif", object: nil, userInfo: ["PageNumber" : 2])

Remember to remove parentVC from observing NSNotificationCenter's (removeObserver) when appropriate

0
votes

You can use Post Notification to accomplish this,

and you will trigger it when the user changes the page.