iOS Switching an embedded view in storyboard

后端 未结 3 1005
予麋鹿
予麋鹿 2021-02-04 18:16

I have been trying to figure this out all day and I know it should be able to be done, but being new to iOS development using Objective-C and not Appcelerator I\'m having newbie

3条回答
  •  清歌不尽
    2021-02-04 18:18

    So after much playing around I got it to work, kind of the way I envisioned. It has no transitions at this point, but I wanted to post the code for others to view.

    In the storyboard I have a view controller for the entire game, that has a view container in it and a button. The view embedded into the game view is a page controller that will contain the different type of questions. Then unattached in the storyboard are the different type of question layouts, in my case the teamDisplay and locationDisplay.

    In the QuestionViewController, I added two properties to the .h file:

    @property (nonatomic, strong) UIPageViewController *pageViewController;
    @property (nonatomic, strong) NSMutableArray *questionTypeViewControllers;
    

    Declared a method:

    -(void)changeView;
    

    In the .m file, synthesized them:

    @synthesize questionTypeViewControllers,
                pageViewController;
    

    In the viewDidLoad method:

    pageViewController = [[UIPageViewController alloc]
            initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
            navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
            options:nil];
    
    UIViewController *ldvc = [self.storyboard instantiateViewControllerWithIdentifier:@"locationDisplay"];
    UIViewController *tdvc = [self.storyboard instantiateViewControllerWithIdentifier:@"teamDisplay"];
    
    questionTypeViewControllers = [NSMutableArray arrayWithObjects:ldvc, tdvc, nil];
    
    NSArray *initView = [NSArray arrayWithObject:ldvc];
    
    [pageViewController setViewControllers:initView
                                          direction:UIPageViewControllerNavigationDirectionForward
                                           animated:NO
                                         completion:nil];
    [self addChildViewController:pageViewController];
    [self.view addSubview:self.pageViewController.view];
    

    And then implemented the changeView method:

    NSArray *initView = [NSArray arrayWithObject: [questionTypeViewControllers objectAtIndex:1]];
    [pageViewController setViewControllers:initView
                                     direction:UIPageViewControllerNavigationDirectionForward
                                      animated:NO
                                    completion:nil];
    

    In the GameViewController, where the button is located, add an action to the button and call the newly created changeView method from the QuestionViewController.

    - (IBAction)change:(id)sender {
        // Need a more reliable way to get the QuestionViewController.
        [[self.childViewControllers objectAtIndex:0] changeView];
    }
    

    And that is it. A no brainer right?! ;)

    I have no idea if this is the proper way to do this, but it worked. Any suggestions or improvements are welcome.

提交回复
热议问题