Question

I have a UIView that contains more than 20 UIButtons. On clicking each of these buttons, i reveal a distinct UIView containing UIPickerView and two UIButtons('OK','CANCEL') as its subviews(each button corresponds to revealing separate and distinct UIVIews).

I have created all the UIViews(20 of them) through IB and set all of them to be hidden initially. So whenever i click a button, it reveals a view and on clicking 'OK' or 'CANCEL' hides the view again.

The problem is when this View Controller is pushed from another, it takes a long time to load(like 5 seconds) and i'm suspecting the time lag is due to loading all the subviews before the parent UIView appears.

I want to reduce or nullify this time lag.

EDIT:

I have trimmed code in viewDidLoad.

- (void)viewDidLoad
{
     @try
    {
        [super viewDidLoad];

        NavigationBarButtonItems *nav=[[NavigationBarButtonItems alloc]init];
        self.navigationItem.rightBarButtonItems = [nav BarButtonItems];


        //the following properties are only for 'quantity' button and its view whereas the original method contains same properties set for all 20 buttons and its views.
        _quantityView.layer.cornerRadius=8;
        _quantityView.clipsToBounds=YES;
        _quantityButton.layer.cornerRadius=8;
        _quantityView.hidden=YES;


        //DEFAULT SELECTED VALUES
        selectedQuantity=@"";

        //only quantity array is initialised here to populate its pickerview. The actual code contains more than 20 arrays initalised for each picker view
        NSArray *quantityData=[[NSArray alloc]init];
        for(int i=1;i<=100;i++)  [quantityData addObject:[NSString stringWithFormat:@"%d",i]];

        quantityArray=quantityData;

        // Do any additional setup after loading the view.
    }
    @catch (NSException *exception)
    {
       NSLog(@"Medications-> View did Load-> %@",exception.name);
    }
}

This is one such UIButton('QUANTITY') and the UIView('QUANTITYVIEW') to be shown when clicked:

- (IBAction)OnClickQuantityButton:(id)sender
{
    @try
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [_mainView setNeedsDisplay];
            CGPoint centerPoint = CGPointMake(160, _mainScrollView.contentOffset.y+228);
            _quantityView.center= centerPoint;
            _quantityView.hidden=NO;
        });
    }
    @catch (NSException *exception)
    {
        NSLog(@"Medications-> quantity button-> %@",exception.name);
    }
}


- (IBAction)OnClickQuantityOkButton:(id)sender
{
    @try
    {
        _quantityLabel.text=selectedQuantity;
        _quantityView.hidden=YES;
    }
    @catch (NSException *exception)
    {
        NSLog(@"Medications-> quantity ok button-> %@",exception.name);
    }
}

- (IBAction)OnClickQuantityCancelButton:(id)sender
{
        _quantityView.hidden=YES;
}
Was it helpful?

Solution

Simple.

  1. All 20 UIView's are having the same UI stuffs like Picker view and 2 Buttons (OK, Cancel).

  2. Picker view datasource may be different.

  3. So create a single UIView which will have those stuffs and based on the picker view is going to be shown, you can pass/ show the different data.

  4. You can also differentiate 20 pickers by specifing tag value of

    pickerview.tag = one enum constants(20 values).

Thats' it.

what if all 20 UIViews contain different UI elements as their subviews?

  1. Don't use the IB

  2. All of them will be created when you create the view

  3. Adopt the lazy loading/creation

  4. Write custom function or class which will create & give the view(One of your 20)

  5. You are not going to show all of them.

  6. create programatically as you needed to show them to the user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top