0

I have created 8 UIViews, I have a UIPicker and when the user selects something with the UIPicker I run the thread though a big if statement which I am not happy about.

I would like to know if what I am doing is okay? and also if the code I am using to load the view is how it should be done.. the code is below you will see I am simply loading the UIView once, then bringing it to the front layer... I would rather reload it entirely using remove from subview... but I couldn't get that to work..

Any one of the UIViews can be loaded at one time which is what is causing me issue.

#pragma mark - Picker Delegates
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // ReloadView
    if (row == 0) {
        if (my1View == nil ) {
            [self DrawView1];
        }
        [self.view bringSubviewToFront:my1View];
    } else if (row == 1) {
        if (my2View == nil ) {
            [self DrawView2];
        }
        [self.view bringSubviewToFront:my2View];
    }else if (row == 2) {
        if (my3View == nil ) {
            [self DrawView3];
        }
        [self.view bringSubviewToFront:my3View];
    }else if (row == 3) {
        if (my4View == nil ) {
            [self DrawView4];
        }
        [self.view bringSubviewToFront:my4View];
    }else if (row == 4) {
        if (my5View == nil ) {
            [self DrawView5];
        }
        [self.view bringSubviewToFront:my5View];
    }else if (row == 5) {
        if (my6View == nil ) {
            [self DrawView6];
        }
        [self.view bringSubviewToFront:my6View];
    }else if (row == 6) {
        if (my7View == nil ) {
            [self DrawView7];
        }
        [self.view bringSubviewToFront:my7View];
    }else if (row == 7) {
        if (my8View == nil ) {
            [self DrawView8];
        }
        [self.view bringSubviewToFront:my8View];
    }
}

The only other issue is that sometimes it will get stuck on one view when no matter what I pick nothing will load over it.

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

3 Answers3

0

First of all, maybe to make it more beautiful you could think about a switch statement, but in terms of performance this doesn't matter:

switch (row) {
        case 0:
            if (my1View == nil ) {
                [self DrawView1];
            }
            [self.view bringSubviewToFront:my1View];
            break;
        case 1:
            if (my1View == nil ) {
                [self DrawView2];
            }
            [self.view bringSubviewToFront:my2View];
            break;

           //THE OTHER CASES HERE...

        default:
            break;
}

About the rest: I would suggest you put all the views into an array or dictionary and also have a variable currentView

@property (nonatomic, strong) UIView* currentView;
@property (nonatomic, strong) NSMutableDictionary* myViews;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // ReloadView
    UIView* myNewView =
    [self.myViews objectForKey:[NSString stringWithFormat:@"%d",(int)row]];

    if (myNewView!=self.currentView) {
        [self.currentView removeFromSuperview];
    }
    if (!myNewView) {
        [self drawView:row];
    }
    [self.view addSubview:myNewView];
    self.currentView = myNewView;
}

Make sure to initiate the myViews property in your viewDidLoad (or something similar) like this:

self.myViews = [NSMutableDisctionary new];

Also you need a new method for this row: [self drawView:row];, in this one you can just call your existing methods or copy your existing methods into this one!

In order to change as little as possible, you can use:

- (void)drawView:(NSInteger)row
{
    switch (row) {
        case 0:
            [self DrawView1];
            break;
        case 1:
            [self DrawView2];
            break;

            //THE OTHER CASES HERE...

        default:
            break;
    }
}
Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40
0

I have an app that has a place-holder UIView (self.mainView) for viewControllers to be placed based on a tab selection. Using this technique is good because then only 1 of the views is loaded at any time, and I can create a ViewController class that operates with that view instead of having a monolithic controller that needs to understand all the views that may be in that place.

Here is some sample code I use to show the appropriate viewController into that place holder.

@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (strong) UIViewController *currentController;

if (self.currentController) {
    [self.currentController willMoveToParentViewController:nil];
    [[self.currentController view] removeFromSuperview];
    [self.currentController removeFromParentViewController];
    self.currentController = nil;
}


switch (row) {
    case 0:
        self.currentController = [self.storyboard instantiateViewControllerWithIdentifier:@"AboutViewController"];
        break;

    case 1: {
        ...
    }
        break;
}


if (self.currentController) {
    [self addChildViewController:self.currentController];
    [self.mainView addSubview:self.currentController.view];
    [self.currentController didMoveToParentViewController:self];
    [self adjustMainView]; // my own method to adjust some constraints base on flags
    [self.view layoutIfNeeded];
}
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
0

I developer your project and I can't see a problem in your code. Works fine. There is no problem in the code that you are showing. Maybe you should share more code to check it.

I tried to follow your style, however I changed small things.

You can found the project I created to test your code here.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81