init内部块后,对象变为nil

时间:2014-07-09 03:16:56

标签: ios objective-c objective-c-blocks

这是我的代码:

    __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;
    }

buttonOnebuttonTwo不是属性。

我打电话给addressOptionView getter后,这两个按钮立即被释放,因此它们不会显示在视图中(我想,NSLog时为零。)

我将setupButton块更改为@property,但它也无效。 将两个按钮更改为@property也不起作用。

但是,当我将setupButton块更改为

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

显示两个按钮,但稍后我无法通过其他方法访问它们(nil已经存在)。

有人可以给我一个简短的解释,说明我做错了什么?我如何使其工作? 感谢。

2 个答案:

答案 0 :(得分:3)

@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;
}

答案 1 :(得分:1)

这一行是问题......

button = [[UIButton alloc] init];

执行此操作后, 参考 副本 将传递到您的块中的按钮现在指向内存中的不同位置。这样做会更改原始按钮引用所指向的位置,因为您只更改 复制 按钮引用所指向的位置。请记住,objective-c是值得传递的!