0

I am working to get user survey before user starts using my app, it is a typically collection of data for my research.

My problem is to add subview on the top of mainviewcontroller.

I have two subviewcontroller as you can see in the first screenshoots. I would like to add first subview on the mainviewcontroller, and whenever user clicks on next customized button, then firstsubview disappear and secondsubview appear on the top of mainviewcontroller.

firstsubview implemented as follows:

    CGRect rect = [firstSurveyViewController.view frame];
    rect.origin.x = 5;
    rect.origin.y = 5;
    [firstSurveyViewController.view  setFrame:rect];
    [self.view addSubview:firstSurveyViewController.view];

But I want to drop the firstsubview and add the second when user clicks on next button. How could I implement?

firstSubview secondSubView mainViewController + mainViewController

casillas
  • 16,351
  • 19
  • 115
  • 215
  • I recommend you set a flag in `NSUserDefaults` to detect if the app did run before. Then, in your first view, check if this is the case. If it's the first run, just present your viewcontrollers like you would present any other. Don't forget to set the flag in `NSUserDefaults` when the user finished the survey. – Swissdude Jul 12 '13 at 16:35
  • Are you using two views in a single view controller or do you have two distinct view controllers you are trying to compose? – Brian Nickel Jul 12 '13 at 16:37
  • I have two distinct freeform view controllers – casillas Jul 12 '13 at 16:38
  • 1
    Ignoring the problem, go sort out your UI. – max_ Jul 12 '13 at 16:53
  • I recommend using "modal" view controllers for each picker. Also, using a table view with static cells and the `UITableViewCellStyleValue1` cell style (like the Settings app) with detail views makes this type of thing easy. – Marcus Adams Jul 12 '13 at 17:11

3 Answers3

2

For composing modal view controllers, you have two real options (ignoring cool stuff under NDA):

  1. Use the Container View Controller pattern to insert the inner view controller into its parent.

  2. Add a new window with that view controller, similar to how you would do it in your AppDelegate. Create the window, add your child as a root view controller, make it key and visible. This is actually what UIAlertView does to perform a similar behavior to your app.

I can't say what would be better for your case, but I have more experience with container view controllers so I'll give you the highlights. To add a child view you will need these steps (from the link above) in your parent view controller:

- (void) displayContentController: (UIViewController*) content;
{
    [self addChildViewController:content];                 // 1
    content.view.frame = [self frameForContentController]; // 2
    [self.view addSubview:self.currentClientView];         // 3
    [content didMoveToParentViewController:self];          // 4
}

This will:

  1. Add your child view controller.
  2. Set the frame to whatever you want. This could be self.view.bounds if you want it to take up the full space.
  3. Add the view to its parent.
  4. Notify the child view controller that it was added.

To remove a child view controller you would do the opposite, again from the link:

- (void) hideContentController: (UIViewController*) content
{
   [content willMoveToParentViewController:nil];  // 1
   [content.view removeFromSuperview];            // 2
   [content removeFromParentViewController];      // 3
}

This will:

  1. Notify your child view controller that it will disappear.
  2. Remove its view from the visual stack.
  3. Remove it from its parent.
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
  • How to remove current view controller? Your approach allows me to add secondSubViewController but still my firstSubviewController is under the secondSubViewController – casillas Jul 12 '13 at 17:01
  • I've added the instructions for removing a view controller. – Brian Nickel Jul 12 '13 at 17:08
  • How to call [self hideContentController:view] in firstSurveyViewController? How should I replace "view" – casillas Jul 12 '13 at 17:17
  • It is expecting your view controllers. You would do `[self hideContentController:firstSurveyViewController]` – Brian Nickel Jul 12 '13 at 22:06
1

You have got two options.

  1. Use both controllers as modal controllers. After tapping Next on first controller, dismiss it and call second controller modally.

  2. Another much better option (in my opinion) is to present these two series of controllers one after another using modal UINavigationController. You can push your controllers in navigation controller, and when done, you can dismiss it and show your main controller.

EDIT

See this post.

Community
  • 1
  • 1
NightFury
  • 13,436
  • 6
  • 71
  • 120
  • how could I implement these two subviews using UINavigationController, is there any sample code to refer? – casillas Jul 12 '13 at 16:35
  • But these viewcontrollers are not fullviewcontrollers, they are subviewcontroller on the top of mainviewcontroller. – casillas Jul 12 '13 at 16:42
0

Transitioning between views is easy-

1) Straight forward

-(IBAction):onNextButtonClick:(id)sender

{
     [self.firstViewController.view removeFromSuperView];
     self.secondViewController.view.frame = newFrame;
     [self.view addSubView:self.secondViewController.view];
}

2) If you want some fancy animations try this-

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionCurlDown animations:^{
    [self.firstViewController.view removeFromSuperView];
    self.secondViewController.view.frame = newFrame;
    [self.view addSubView:self.secondViewController.view];
} completion:^(BOOL finished) {
    //Completion
}];
vforvendetta
  • 596
  • 7
  • 15
  • your approach is quite what I am looking. Just only one point I need to ask, self.view1 is firstViewController.view ? right? – casillas Jul 12 '13 at 17:03