81663

How to make UIBlockButton suit with ARC?

Question:

I am using the UIBlockButton code from <a href="https://stackoverflow.com/questions/3908003/uibutton-block-equivalent-to-addtargetactionforcontrolevents-method" rel="nofollow">this post</a>:

typedef void (^ActionBlock)(); @interface UIBlockButton : UIButton { ActionBlock _actionBlock; } -(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action; @implementation UIBlockButton -(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action { _actionBlock = Block_copy(action); [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event]; } -(void) callActionBlock:(id)sender{ _actionBlock(); } -(void) dealloc{ Block_release(_actionBlock); [super dealloc]; } @end

but I changed my code to under ARC, how to change the code to make sure everything works well?

Answer1:

Header:

@interface UIBlockButton : UIButton - (void) handleControlEvent: (UIControlEvents) event withBlock: (dispatch_block_t) action; @end

Implementation:

@interface UIBlockButton () @property(copy) dispatch_block_t actionBlock; @end @implementation UIBlockButton @synthesize actionBlock; - (void) handleControlEvent: (UIControlEvents) event withBlock: (dispatch_block_t) action { [self setActionBlock:action]; [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event]; } - (void) callActionBlock: (id) sender { if (actionBlock) actionBlock(); } @end

But note that multiple calls to handleControlEvent:withBlock: will overwrite your block, you can’t have different actions for different events with this implementation. Also, you should probably use a different prefix for the class instead of UI to prevent potential clashes with Apple’s code.

Recommend

  • UIRefreshControl with low height UICollectionView
  • #selector' refers to a method that is not exposed to Objective-C swift 3
  • change backbutton with custom image and textcolor
  • Are objects in Objective-C ever created on the stack?
  • Text overlay not showing in GPUImage iOS
  • Argument of '#selector' does not refer to an '@objc' method (swift 3)
  • Passing argument within action selection in UIButton
  • WebBrowser: IDropTarget
  • How can I encode a filename according to RFC 2231?
  • Convert from hex-encoded CLOB to BLOB in Oracle
  • What's the alternative to IValueFormatter in AutoMapper?
  • Is it possible to extract table infomation using Apache Tika?
  • Touch events (touchesMoved) not working on UIViews inside UIScrollView
  • Implement Gauss-Jordan elimination in Haskell
  • How to click on a link that has a certain content in puppeteer?
  • Is it possible to specialize on a static lifetime?
  • How to Compose OSGi Based project with C++ based project?
  • How do I include screenshots of the full page in my serenity report (and not only of the viewport) u
  • How to make the tableview response pan gesture in ZUUIRevealController
  • iOS - Is this a task for enums?
  • How to replace TouchesBegan with UIGestureRecognizer
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Dependable views in Ember
  • Implementation of RTTI using typeid
  • Cocoa OSX: How can I make an image draggable
  • Why can't I use non-integral types with switch [duplicate]
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • What is the undocumented SessionIdInterface in PHP 5.5?
  • Where these are stored?
  • What's the syntax to inherit documentation from another indexer?
  • abstracting over a collection
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Can't remove headers after they are sent
  • Implementing “partial void” in VB
  • Sending HTML Form Data to Spring REST Web Service
  • Jquery popup on mouse over of calendar control
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Observable and ngFor in Angular 2