0

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.

Ranjit
  • 4,576
  • 11
  • 62
  • 121

1 Answers1

0

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];
}
johnMa
  • 3,291
  • 24
  • 37
  • I cant do that, because I have 2 buttons, so when I click on other button I have to remove it , because it is added as a subView – Ranjit Dec 14 '13 at 09:18
  • I mean put `if(!m_calViewController){// alloc your CalendarViewController }` to prevent generate CalendarViewController again. – johnMa Dec 14 '13 at 10:59
  • Ok I will try it and get back to you – Ranjit Dec 14 '13 at 12:18