How to improve efficiency with AddChildViewController

222 views Asked by At

I am using AddChildViewController and adding a CalendarViewController, here is the code to add it

- (void)calButtonClicked
{
        m_calViewController = [[CalendarViewController alloc]initWithNibName:@"CalendarViewController" bundle:nil];
        [self addChildViewController:m_calViewController];
        [[self view] addSubview:[m_calViewController view]];
        [m_calViewController didMoveToParentViewController:self];
}

Now this CalendarViewController I have a function to create the calendarUI , I have written it in

- (void)viewDidLoad
{
      [self createCalendarUI];
}

Now whenever I click on the button to open this ViewController , method viewDidLoad is called everytime and it takes time to create the UI. And because of that , my app is becoming slow.

So is their any way so that my UI is created only once, So that I can improve the efficiency

Regards Ranjit.

1

There are 1 answers

3
johnMa On BEST ANSWER

if you just want to reuse CalendarViewController

try reuse CalendarViewController's view and do not alloc CalendarViewController in calButtonClicked each time.

- (void)calButtonClicked
{
    if(!m_calViewController){
        m_calViewController = [[CalendarViewController alloc]initWithNibName:@"CalendarViewController" bundle:nil];
        [self addChildViewController:m_calViewController];
        }
    [[self view] addSubview:[m_calViewController view]];
    [m_calViewController didMoveToParentViewController:self];
}