简体   繁体   中英

Object becomes nil after init inside block

Here's my code:

    __block UIButton buttonOne;
    __block UIButton buttonTwo;
    - (UIView *)addressOptionView
    {
        if (!_addressOptionView) {
            _addressOptionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), addressButtonArray.count * BasedAdditionForSegment)];

            void (^setupButton)(UIButton *, NSString *, NSInteger) = ^
            void (UIButton *button, NSString *title, NSInteger idx) {
                if (!button)
                    button = [[UIButton alloc] init];
                button.frame = ({
                    CGRect frame = _addressOptionView.bounds;
                    frame.origin.x += 20;
                    frame.size.width -= 40;
                    frame.origin.y = idx * BasedAdditionForSegment;
                    frame.size.height = BasedAdditionForSegment;
                    frame;
                });
                [button setTitle:title forState:UIControlStateNormal];
            };
            setupButton(buttonOne, addressButtonArray[0], 0);
            setupButton(buttonTwo, addressButtonArray[1], 1);

            DebugLog(@"%@", buttonOne);
            [_addressOptionView addSubview:buttonOne];
            [_addressOptionView addSubview:buttonTwo];
        }
        return _addressOptionView;
    }

buttonOne and buttonTwo is not properties.

After I call addressOptionView getter , these two buttons are deallocated immediately, thus they don't show up in the view (I guess, nil when NSLog ).

I changed the setupButton block to @property and it doesn't work either. Changing two button to @property doesn't work too.

However, when I change the setupButton block to

UIButton * (^setupButton)(NSString *, NSInteger)

Two buttons do show up, but I cannot access them later in other methods ( nil already).

Can somebody give me a brief explanation about what do I do wrong ? How do I make it work ? Thanks.

@implementation SomeObject {
  // do not make these __block types
  UIButton* _buttonOne;
  UIButton* _buttonTwo;
}

- (UIView *)addressOptionView
{
  // most of method removed for brevity, see question
  void (^setupButton)(UIButton*__autoreleasing*, NSString *, NSInteger) = ^
   void (UIButton*__autoreleasing*button, NSString *title, NSInteger idx) {
    NSAssert( button, @"Must not pass nil reference as button" );
    UIButton* localButton = *button;
    if (!localButton) {
      // continue to work on the more convenient localButton, but make sure
      // that the button reference is written to the ivar...
      localButton = *button = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    // other button setup clipped
  };
  // compiler will generate strong stack-allocated temporary variable here
  // to deal with autoreleasing assignment, but make sure ivars are not
  // __block class
  setupButton(&_buttonOne, addressButtonArray[0], 0);
  setupButton(&_buttonTwo, addressButtonArray[1], 1);

  DebugLog(@"%@", _buttonOne);
  [_addressOptionView addSubview:_buttonOne];
  [_addressOptionView addSubview:_buttonTwo];

  // top-most branch was clipped for this example
  return _addressOptionView;
}

This line is the problem...

button = [[UIButton alloc] init];

Once you do that, the copy of the reference to your button that you passed into your block is now pointing to a different location in memory. This does not change where the original button reference is pointing as you are only changing where the copied button reference is pointing. Remember, objective-c is pass by value!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM