ios - Change view in a page view controller when button is pressed in one of the controllers -
i have uipageviewcontroller
connected 3 view controllers
and 3 classes each uiviewcontroller
when "take photo" button pushed, third view should slide view. when "choose library" pushed first view controller should slide view.
i have 3 classes controlling each uiviewcontroller
, 1 class controls pages in storyboard. here 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 been programmed switch views when flicking left or right.
i have buttons linked respecting classes don't know how switch visible view controllers via buttons. how can change visible uiviewcontroller
when 1 of buttons pushed on second view controller?
call "setviewcontrollers" again switch different view controller. so, example, if wanted move "p1", use this:
// grab reference out of existing nsarray uiviewcontroller *newvc = self.myviewcontrollers[0]; [self setviewcontrollers:@[newvc] direction:uipageviewcontrollernavigationdirectionreverse animated:yes completion:nil];
using "animated:yes" cause same slide if user navigated manually. adjust "direction" based on whether jumping forward or backwards.
to communicate between child view controllers , parent, use delegate. first, declare delegate in parent view controller's header, such this:
@protocol photosourcedelegate <nsobject> - (void)takephoto; - (void)choosefromlibrary; @end
update parent view controller implement delegate:
@interface pageviewcontroller : uipageviewcontroller <photosourcedelegate> @end @implementation pageviewcontroller ... #pragma mark - photosourcedelegate - (void)takephoto { // call "setviewcontroller" display "take photo" view controller; } - (void)choosefromlibrary { // call "setviewcontroller" display "choose library" view controller; }
within header of view controller shows "take photo" , "choose library" buttons (p2?), add property hold reference delegate:
@property(nonatomic, weak) id<photosourcedelegate> delegate;
when instantiate controller, sure set delegate:
uiviewcontroller *p2 = [self.storyboard instantiateviewcontrollerwithidentifier:@"two"]; p2.delegate = self;
finally, within same view controller, when "take photo" button pressed, call delegate this:
if (self.delegate != nil) { [self.delegate takephoto]; }
and when "choose library" button pressed, call delegate this:
if (self.delegate != nil) { [self.delegate choosefromlibrary]; }
the use of delegates common in ios, quick search turn quite few tutorials if/when you're ready better understanding.
Comments
Post a Comment