简体   繁体   中英

Change view in a page view controller when button is pressed in one of the controllers

I have a UIPageViewController connected to three view controllers

And three classes for each UIViewController

When the "Take Photo" button is pushed, the third view should slide into view. When "Choose From Library" is pushed the first view controller should slide into view.

I have 3 classes controlling each UIViewController and one class that controls the pages in the storyboard. Here is the pageViewController.m

@implementation pageViewController

{
NSArray *myViewControllers;
}

- (void)viewDidLoad {
  [super viewDidLoad];    

  self.delegate = self;
  self.dataSource = self;

  UIViewController *p1 = [self.storyboard instantiateViewControllerWithIdentifier:@"one"];
  UIViewController *p2 = [self.storyboard instantiateViewControllerWithIdentifier:@"two"];
  UIViewController *p3 = [self.storyboard instantiateViewControllerWithIdentifier:@"three"];


  myViewControllers = @[p1, p2, p3];
  pageTwoViewController *ptvc = [[pageTwoViewController alloc] init];
  ptvc.publicViewControllers = myViewControllers;

  [self setViewControllers:@[p2] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

The app has already been programmed to switch views when flicking to the left or right.

I have the buttons linked to their respecting classes but I don't know how to switch visible view controllers via the buttons. How can I change the visible UIViewController when one of buttons is pushed on the second view controller?

Call "setViewControllers" again to switch to a different view controller. So, for example, if you wanted to move to "p1", use something like this:

// grab a reference out of your existing NSArray
UIViewController *newVC = self.myViewControllers[0];
[self setViewControllers:@[newVC] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];

Using "animated:YES" will cause the same slide as if the user navigated manually. Adjust the "direction" based on whether you are jumping forward or backwards.

To communicate between your child view controllers and their parent, use a delegate. First, declare a delegate in your parent view controller's header, such as this:

@protocol PhotoSourceDelegate <NSObject>
  - (void)takePhoto;
  - (void)chooseFromLibrary;
@end

Update your parent view controller to implement this delegate:

@interface pageViewController : UIPageViewController <PhotoSourceDelegate>
@end

@implementation pageViewController
...
#pragma mark - PhotoSourceDelegate
- (void)takePhoto {
  // call "setViewController" to display "take photo" view controller;
}

- (void)chooseFromLibrary {
  // call "setViewController" to display "choose from library" view controller;
}

Within the header of the view controller which shows the "Take Photo" and "Choose From Library" buttons (p2?), add a property to hold a reference to the delegate:

@property(nonatomic, weak) id<PhotoSourceDelegate> delegate;

When you instantiate this controller, be sure to set its delegate:

UIViewController *p2 = [self.storyboard instantiateViewControllerWithIdentifier:@"two"];
p2.delegate = self;

Finally, within that same view controller, when your "Take Photo" button is pressed, call the delegate like this:

if (self.delegate != nil) {
  [self.delegate takePhoto];
}

And when your "Choose From Library" button is pressed, call the delegate like this:

if (self.delegate != nil) {
  [self.delegate chooseFromLibrary];
}

The use of delegates is very common in iOS, so a quick search will turn up quite a few tutorials if/when you're ready to get a better understanding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM