1

In a chat based app I have UITableView which display all friends name, on didSelectRowAtIndex I am pushing to chatViewController using navigationcontroller push method.

I have two confusion:

1> When I push chatViewController I do it like this

 chatViewController *cVc = [[chatViewController alloc]initWithFriendName:@"the name" andId:@"the id"];

There can be 10 or 50 or 100 friends, is it correct to call alloc init for every friend?

2> When user tap back button to go back to friends list, what happen to chatViewController's current instance when it will be destroyed to free memory?

S.J
  • 3,063
  • 3
  • 33
  • 66

2 Answers2

2
  1. Yes, if this instance of chatViewController is for this specific friend.

  2. The back button does a implicit popViewControllerAnimated: which pops the chatViewController from the navigation stack and destroys it (unless you have saved a strong reference to that view controller somewhere).

So there will be only one instance of chatViewController at a time (created in didSelectRowAtIndex and destroyed by popViewControllerAnimated: when the user goes back to the table view).

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • thanks for replying, my mistake cVc is not global, it is for specific friend. – S.J May 02 '13 at 08:10
  • @S.J: For my answer I had assumed that vVC is a local instance created in `didSelectRowAtIndex`. – Martin R May 02 '13 at 08:14
  • please also view this question http://stackoverflow.com/questions/16333731/need-assistance-regarding-uiviewcontroller – S.J May 02 '13 at 08:45
  • I would like to invite you to answer this question http://stackoverflow.com/questions/17657719/please-clear-some-confusions-regarding-uiviewcontroller – S.J Jul 16 '13 at 05:26
0

Since you are creating the global instance of chatViewController and allocting it each and everytime the didSelectRowAtIndexpath: method is called , this is the wrong way as it will un-necessarily utilising the memory and thus degrades the app performance . You must create the local instance in the method didSelectRowAtIndexpath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     ChatViewController *_cvc = [[ChatViewController alloc] initWithFriendName:@"the name" andId:@"the id"];

     [self.navigationController pushViewController:_cvc animated:YES];
}

Answer for your second question is whenever the user presses the back button , the instance is destroyed

Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19
  • 1
    In your answer call will be "allocating it each and every time" as well – art-divin May 02 '13 at 07:56
  • 1
    Yes, call will be allocating the instances everytime but the lifetime of the instances will be weak and would get automatically destroyed as soon as they are no longer in uses – Gaurav Rastogi May 02 '13 at 08:12