From 52ac833f7c8fd374444e5de27dac52737870688a Mon Sep 17 00:00:00 2001 From: Giuseppe Nucifora Date: Tue, 5 Apr 2016 11:19:35 +0200 Subject: [PATCH] - Fix dealloc crash --- .../gradiendButtonViewController.m | 24 ++++++++++ GradientButton.podspec | 2 +- Pod/Classes/GradientButton.m | 47 +++---------------- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/Example/GradientButton/gradiendButtonViewController.m b/Example/GradientButton/gradiendButtonViewController.m index 3aea1dc..8ac70cc 100644 --- a/Example/GradientButton/gradiendButtonViewController.m +++ b/Example/GradientButton/gradiendButtonViewController.m @@ -10,12 +10,14 @@ #import "GradientButton.h" #import "PureLayout/PureLayout.h" + @interface gradiendButtonViewController () { GradientButton *button; GradientButton *autoLayoutButton; BOOL didUpdateConstraints; + UIButton *presentControllerButton; } @end @@ -42,6 +44,13 @@ [self.view addSubview:autoLayoutButton]; + presentControllerButton = [UIButton newAutoLayoutView]; + [presentControllerButton setTitle:@"Presenta Conttoller" forState:UIControlStateNormal]; + [presentControllerButton addTarget:self action:@selector(presentAction:) forControlEvents:UIControlEventTouchUpInside]; + [presentControllerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; + + [self.view addSubview:presentControllerButton]; + // Do any additional setup after loading the view, typically from a nib. } @@ -98,11 +107,26 @@ [autoLayoutButton autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:40]; [autoLayoutButton autoSetDimension:ALDimensionHeight toSize:50]; + [presentControllerButton autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:autoLayoutButton]; + [presentControllerButton autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:autoLayoutButton]; + [presentControllerButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:autoLayoutButton]; + [presentControllerButton autoAlignAxisToSuperviewAxis:ALAxisVertical]; + didUpdateConstraints = YES; } [super updateViewConstraints]; } +- (void) presentAction:(UIButton *) sender { + gradiendButtonViewController *viewController = [[gradiendButtonViewController alloc] init]; + + [self presentViewController:viewController animated:YES completion:nil]; + + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [viewController dismissViewControllerAnimated:YES completion:nil]; + }); +} - (void)didReceiveMemoryWarning { diff --git a/GradientButton.podspec b/GradientButton.podspec index a316579..7e7c473 100644 --- a/GradientButton.podspec +++ b/GradientButton.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = "GradientButton" -s.version = "0.1.5" +s.version = "0.2.0" s.summary = "GradientButton is a UIBUtton subclassing with gradient support" # This description is used to generate tags and improve search results. diff --git a/Pod/Classes/GradientButton.m b/Pod/Classes/GradientButton.m index 8b156e3..9372e1e 100644 --- a/Pod/Classes/GradientButton.m +++ b/Pod/Classes/GradientButton.m @@ -9,6 +9,8 @@ #import "GradientButton.h" #import "PureLayout.h" +static char ClassNameKVOContext = 0; + @interface GradientButton() @property (nonatomic, strong) CAGradientLayer *gradientLayer; @@ -16,8 +18,6 @@ @property (nonatomic, strong) NSMutableArray *cgColors; @property (nonatomic) CGPoint startPoint; @property (nonatomic) BOOL didUpdateConstraints; -@property (nonatomic, strong) UIView *container; -@property (nonatomic, assign) BOOL didSetupConstraints; @end @@ -30,12 +30,6 @@ _colors = [[NSMutableArray alloc] init]; _cgColors = [[NSMutableArray alloc] init]; - _container = [UIView newAutoLayoutView]; - [_container setBackgroundColor:[UIColor clearColor]]; - [_container setAlpha:0]; - [self addSubview:_container]; - - [_container addObserver:self forKeyPath:@"bounds" options:0 context:nil]; } return self; } @@ -45,12 +39,6 @@ if (self) { _colors = [[NSMutableArray alloc] init]; _cgColors = [[NSMutableArray alloc] init]; - _container = [UIView newAutoLayoutView]; - [_container setBackgroundColor:[UIColor clearColor]]; - [_container setAlpha:0]; - [self addSubview:_container]; - - [_container addObserver:self forKeyPath:@"bounds" options:0 context:nil]; } return self; } @@ -60,12 +48,6 @@ if (self) { _colors = [[NSMutableArray alloc] init]; _cgColors = [[NSMutableArray alloc] init]; - _container = [[UIView alloc] initWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height)]; - [_container setBackgroundColor:[UIColor clearColor]]; - [_container setAlpha:0]; - [self addSubview:_container]; - - [_container addObserver:self forKeyPath:@"bounds" options:0 context:nil]; } return self; } @@ -109,19 +91,10 @@ } } -- (void) updateConstraints { - [super updateConstraints]; - - if (!self.didSetupConstraints) { - [_container autoPinEdgesToSuperviewEdges]; - [_container autoAlignAxisToSuperviewAxis:ALAxisHorizontal]; - [_container autoAlignAxisToSuperviewAxis:ALAxisVertical]; - } -} + - (void) setFrame:(CGRect)frame { [super setFrame:frame]; - [_container setFrame:CGRectMake(0,0,frame.size.width,frame.size.height)]; dispatch_async(dispatch_get_main_queue(), ^{ if (_gradientLayer) { [self setbackgroundLayerWithColors:_colors startPoint:_startPoint]; @@ -141,20 +114,14 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if (object == self && [keyPath isEqualToString:@"bounds"]) { + if (context == ClassNameKVOContext && object == self && [keyPath isEqualToString:@"bounds"]) { if(_gradientLayer) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self setbackgroundLayerWithColors:_colors startPoint:_startPoint]; - }); + + [self setbackgroundLayerWithColors:_colors startPoint:_startPoint]; + } } } -- (void) dealloc { - - [_container removeObserver:self forKeyPath:@"bounds"]; -} - - @end