Finish off UIAlertView post

This commit is contained in:
Wesley Moore 2011-12-11 17:23:08 +11:00
parent 468e89693a
commit f79cbb3b56

View file

@ -1,10 +1,9 @@
When it comes time to present errors or other messages in iOS When it comes time to present errors or other messages in iOS with
with [UIAlertView] it becomes immediately obvious that a more [UIAlertView] it is immediately obvious that a more convenient interface
convenient interface would involve the use of blocks. A [search on would involve the use of blocks. A [search on GitHub][github-search]
GitHub][github-search] shows just about every iOS developer has had a shows just about every iOS developer has had the same thought and had a crack at it.
crack at it.
[UIAlertView]: TODO [UIAlertView]: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
[github-search]: https://github.com/search?type=Repositories&language=&q=uialertview&repo=&langOverride=&x=14&y=17&start_value=1 [github-search]: https://github.com/search?type=Repositories&language=&q=uialertview&repo=&langOverride=&x=14&y=17&start_value=1
After reviewing the better options (I.e. those that actually had a After reviewing the better options (I.e. those that actually had a
@ -25,19 +24,19 @@ blocks based wrapper with the UIAlertView.
[set-object]: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html [set-object]: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html
In the `init` method: In the `init` method the wrapper instance is associated with its UIAlertView:
objc_setAssociatedObject(alertView, _cmd, self, OBJC_ASSOCIATION_RETAIN); objc_setAssociatedObject(alertView, _cmd, self, OBJC_ASSOCIATION_RETAIN);
Note that I'm using the implicit second argument to the method, its Note that I'm using the implicit second argument to the method, its
selector, `_cmd` as the key for the associated object. This was selector, `_cmd` as the key for the associated object. This was
suggested in a [tweet by Bill Bummager][bbum]. TODO suggested in a [tweet by Bill Bumgarner][bbum].
[bbum]: http://twitter.com/bbum/status/3609098005 [bbum]: http://twitter.com/bbum/status/3609098005
Then in `alertView:didDismissWithButtonIndex:`, the association is Then in `alertView:didDismissWithButtonIndex:`, the association
removed, `dealloc` of the wrapper called and the `UIAlertView` also is removed, `dealloc` of the wrapper called as a result and the
released. `UIAlertView` also released.
SEL key = @selector(initWithTitle:message:); SEL key = @selector(initWithTitle:message:);
objc_setAssociatedObject(self.alertView, key, nil, OBJC_ASSOCIATION_RETAIN); objc_setAssociatedObject(self.alertView, key, nil, OBJC_ASSOCIATION_RETAIN);