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