Created
April 23, 2012 04:37
-
-
Save joshdholtz/2468899 to your computer and use it in GitHub Desktop.
iOS - UIButton+Block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
[button setAction:kUIButtonBlockTouchUpInside withBlock:^{ | |
NSString* launchUrl = @"http://joshholtz.com"; | |
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]]; | |
}]; | |
.. |
Thanks combien! I actually did run across the issue and solved it with copying the block I just forgot to update my gist.
Thanks for the reminder though! Much appreciated.
Code is updated now to copy the block.
I love this simple extension.
It's better to change the import path #import "/usr/include/objc/runtime.h"
into #import <objc/runtime.h>
in order to prevent build failure in Xcode5.
You only updated ONE of the missing "block copy" statemetns above. The UIButton+Block.m is broken.
Wouldn't it cause a retain cycle if you use self
in the block implementation ?
@implementation ViewController
//...
[[self button] addAcction:kUIButtonBlockTouchUpInside withBlock:^{
[self doSomething];
}]
//...
@end
I'm asking because I'm trying to do something like this, and hate for the consumer of the control to always do :
__weak __typeof(self) weakSelf = self;
[[self button] addAcction:kUIButtonBlockTouchUpInside withBlock:^{
[weakSelf doSomething];
}]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to copy the block to avoid the block to be garbage collected, see http://stackoverflow.com/questions/8852107/arc-related-crash-in-ios-when-using-a-block-pointer-as-a-member-variable
updated version at git://gist.github.com/2835270.git