I am dismissing a modal view controller whom is the delegate
of a UIPickerView
.
When I dismiss the view using
[self dismissViewControllerAnimated:YES completion:NULL];
My app crashes, but only when the UIPickerView
is showing, and currently the first responder.
I found the the cause of the crash to be a Zombie, this method, [UIPicker _updateSelectedRows]
is showing in my instruments as the issue.
I am able to fix the issue by setting the UIPicker delegate and dataSource to nil prior to dismissing the view controller.
I have never needed to do this before, is there something I'm missing?
Here is a bare bones of the presenting view, presented in a modal segue. When you dismiss this using the IBAction, it will cause the crash
@interface VTSecViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *catPicker;
@property (strong, nonatomic) NSArray *catItems;
@property (weak, nonatomic) IBOutlet UITextField *pickerTF;
@end
@implementation VTSecViewController
- (IBAction)dismpress:(id)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.catItems = @[@"one", @"two"];
self.catPicker = [[UIPickerView alloc] init];
self.pickerTF.inputView = self.catPicker;
self.catPicker.delegate = self;
self.catPicker.dataSource = self;
[self.pickerTF becomeFirstResponder];
// Do any additional setup after loading the view.
}
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.catItems count];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.pickerTF.text = self.catItems[row];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.catItems[row];
}
@end
This post helps clear up a few things