1

I have the following code:

- (IBAction)mapPressed:(id)sender
{
    MapViewController *mapVC = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:[NSBundle mainBundle]];
    mapVC.delegate = self;
    [self.navigationController pushViewController:mapVC animated:YES];
}

For MapViewController:

//MapViewController.h
@protocol MapViewDelegate
@required
- (void)selectedPlacemark:(MKPlacemark*)placemark;
@end
//...
@property (nonatomic, weak) id<MapViewDelegate> delegate;
//...

If ARC is enabled, do I still need to set mapVC.delegate to nil? If so, does this mean I'm not supposed to create the MapViewController locally and instead set up an instance variable for the object?

ninjaneer
  • 6,951
  • 8
  • 60
  • 104
  • This all looks OK to me. Are you seeing problems? If so, it might help to be more specific about what they are. Or is this just a question to double check that you've got it right? – Will Pragnell Apr 20 '12 at 10:00
  • Well let's say that fooViewController creates MapViewController and fooViewController sets itself as MapViewController's delegate. If fooViewController gets destroyed, and one of MapViewController's delegate method gets called, won't it crash? – ninjaneer Apr 20 '12 at 10:02
  • I don't think it will, that's the point of weak references I believe. However, I don't have much experience with ARC yet so I'll leave it for someone who does to confirm. – Will Pragnell Apr 20 '12 at 10:08

1 Answers1

0

Just confirmed via this question that you don't need to set the reference to nil. All should be fine with the above code.

Community
  • 1
  • 1
Will Pragnell
  • 3,073
  • 1
  • 20
  • 21