Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that your KelaMagicalControl is getting released at the end of the showMagicalControl: method, it's not getting retained anywhere. Only the UIView that you created inside showWithTouchCompletionBlock: is getting retained because you added it to a superview, in this case the window. That's why the the popup is shown correctly. But a target is always unsafe_unretained, so when you tap that view, the gestureRecognizer will try to call your KelaMagicalControl which was already released, so you get a crash.</p> <p>You can easily solve this by making your KelaMagicalControl a subclass of UIView. I quickly typed out the changed you'd have to make:</p> <p><strong>.h file</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #if NS_BLOCKS_AVAILABLE typedef void (^KelaMagicalControlCompletionBlock)(); #endif @interface KelaMagicalControl : UIView { NSString* _title; NSString* _message; } -(id)initWithTitle:(NSString *)title message:(NSString *)message; -(void)showWithTouchCompletionBlock:(KelaMagicalControlCompletionBlock)completionBlock; @end </code></pre> <p><strong>.m file</strong></p> <pre><code>#import "KelaMagicalControl.h" @interface KelaMagicalControl() @property (nonatomic, strong) NSString * title; @property (nonatomic, strong) NSString * message; @property (copy) KelaMagicalControlCompletionBlock completionBlock; @end @implementation KelaMagicalControl -(void)dealloc { NSLog(@"deallocated"); } + (KelaMagicalControl *)toastWithTitle:(NSString *)title message:(NSString *)message { KelaMagicalControl * magicalControl = [[KelaMagicalControl alloc] initWithTitle:title message:message]; return magicalControl; } -(id)initWithTitle:(NSString *)title message:(NSString *)message { self = [super initWithFrame:CGRectMake(10, 10, 300, 300)]; if (self) { _title = title; _message = message; } return self; } -(void)showWithTouchCompletionBlock:(void (^)())completionBlock { UIWindow * mainWindow = [[UIApplication sharedApplication]keyWindow]; [self setTag:10001]; [self setBackgroundColor:[UIColor redColor]]; [mainWindow addSubview:self]; UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainViewTapped)]; [self addGestureRecognizer:tapGestureRecognizer]; self.completionBlock = completionBlock; } -(void)mainViewTapped { if(self.completionBlock) { self.completionBlock(); self.completionBlock = nil; } } @end </code></pre> <p>Since your KelaMagicalControl is now the UIView that you're showing, it will automatically be retained since it has a superview. When you tap the view the completion block will now execute as you want. Make sure that at the end of the completionblock you remove it from its superview otherwise it'll never get released.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload