danielamitay / DAKeyboardControl

DAKeyboardControl adds keyboard awareness and scrolling dismissal (ala iMessages app) to any view with only 1 line of code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Capturing self strongly in this block is likely to lead a retain cycle

Piero87 opened this issue · comments

Hello, i have this uitableview:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

then in the viewDidLoad i do this:

[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView)     {
    // Move interface objects accordingly
    // Animation block is handled for you

    CGRect tableViewFrame = _tableView.frame;
    tableViewFrame.size.height = keyboardFrameInView.origin.y;
    _tableView.frame = tableViewFrame;
}];

i doesn't use self, but i however have this warning:

"Capturing self strongly in this block is likely to lead a retain cycle"

so i found this "trick":

__weak typeof(self) weakSelf = self;
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
    // Move interface objects accordingly
    // Animation block is handled for you

    CGRect tableViewFrame = weakSelf.tableView.frame;
    tableViewFrame.size.height = keyboardFrameInView.origin.y;
    weakSelf.tableView.frame = tableViewFrame;
}];

and i receive no warning, and seems work, but i read in the example project that i have to use this:

[self.view removeKeyboardControl];

but where i have to insert it?

[self.view removeKeyboardControl]; should generally go in your view controller's -dealloc method.

@Piero87 CGRect tableViewFrame = _tableView.frame; is using an ivar, thus retaining self.