0

I created a custom UIViewController.

@property (weak, nonatomic) NSArray *optionArray;

-(VCSelectionViewController*) getPopoverViewControllerForTextField:(UITextField*)textField {

UIViewController *buttonPopover = [[UIViewController alloc] init];
buttonPopover.optionArray = [OptionArray getLOCQualityArray]

But the optionArray value became nil in viewWillAppear for iOS9. It was working in iOS8. I have spent hours on this with no luck.

This is where I called the getPopoverViewControllerForTextField method listed above.

-(void) displaySelectionPopover:(UITextField *)textField {

VCSelectionViewController *popoverViewController = [self getPopoverViewControllerForTextField:textField];

if(_masterPopoverController == nil && popoverViewController != nil){   //make sure popover isn't displayed more than once in the view
    _masterPopoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
}

[_masterPopoverController setPopoverContentSize:[popoverViewController getPopoverSize]];
_masterPopoverController.delegate = self;

[_masterPopoverController presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

After I executed the code above, I can still see the optionArray values. optionArray still exists inside

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

But when it start executing viewWillAppear, optionArray became nil.

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];    
    self.view.tintColor = [UIColor blackColor];
    [self layoutButtons];
}

-(void)layoutButtons {

    CGRect buttonRect = CGRectMake(START_POSTITION_X, START_POSTITION_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    int arrayPosition = 0;

    for (NSString *buttonTitle in _optionArray) {
        UIButton *button = [[UIButton alloc] initWithFrame:buttonRect];
        [button setTitle:buttonTitle forState:UIControlStateNormal];
        }

        [self.view addSubview:button];

        arrayPosition++;
     }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JIANG
  • 1,687
  • 2
  • 19
  • 36
  • Not really enough information to debug here. Either `[OptionArray getLOCQualityArray]` returns a value or it doesn't. If it's not returning a value, you would need to post the code for that method. – memmons Sep 24 '15 at 01:36
  • Thank you for looking into this. [OptionArray getLOCQualityArray] does returns a value, but when the layoutButtons method uses the _optionArray, the value became nil. – JIANG Sep 24 '15 at 16:27

1 Answers1

1

I just found the solution.

Changed from weak to strong, and then everything worked again. @property (strong, nonatomic) NSArray *optionArray;

Not sure what changed iOS9 caused this behavior.

JIANG
  • 1,687
  • 2
  • 19
  • 36
  • 1
    Nothing need officially have changed; you were relying on non-guaranteed behaviour. `weak` means the reference will work for as long as the thing it points to stays alive. You have no guarantees in place on its lifetime. – Tommy Nov 02 '15 at 21:09