I have a custom subclassed uiview I created to display in-app notifications.
I can call the view just fine, but am having problems dismissing it using a uibutton (embedded in the custom view)
When the button is pressed, the app crashes and I get this error:
UPDATE – Fixed the above issue, but now only the button dismisses, and not the actual view. See updated code below.
-(id)initWithMessage:(NSString *)message{
self = [super initWithFrame:CGRectMake(0, -70, 320, 60)];
if (self) {
//Add Image
UIImage *image = [UIImage imageNamed:@"notice-drop-down"];
UIImageView *background = [[UIImageView alloc] initWithImage:image];
[self addSubview:background];
//Add Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.frame.size.height/2-25, 300, 50)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor blackColor]];
[label setText:message];
label.numberOfLines = 0;
[label setFont:[UIFont fontWithName:@"Hand of Sean" size:16]];
//NSLog(@"FONT FAMILIES\n%@",[UIFont familyNames]);
[self addSubview:label];
//Add Close Button
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(280, self.frame.size.height/2-15, 30, 30)];
UIImage *closeImage = [UIImage imageNamed:@"notice-close"];
[closeButton setImage:closeImage forState:UIControlStateNormal];
[closeButton addTarget:self action:@selector(closeNoticeDropDown:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseIn
animations:^{
self.frame = CGRectMake(0,70,320,60);
}
completion:nil
];
}
return self;
}
-(void)closeNoticeDropDown:(id)self{
NoticeDropDown *notice = (NoticeDropDown *)self;
NSLog(@"Frame: %f",notice.frame.size.width);
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
notice.frame = CGRectMake(0,-70,320,60);
}
completion:^(BOOL finished){
[notice removeFromSuperview];
//notice = nil;
}
];
}
View call from another view controller:
noticeDropDown = [[NoticeDropDown alloc] initWithMessage:message];
[self.view insertSubview:noticeDropDown belowSubview:hudContainerTop];
You have declared your method to be a class method but are sending the message to an instance. If you want it to be a class method still, pass [NoticeDropDown class] as the target parameter to your addTarget:action:forControlEvemts: method. Otherwise replace the “+” with a “-” in the method declaration.
Also- when UIControl actions have sender parameters it will send the control as the sender – so you will get a UIButton instead of your view.
My recommendation is to change your action to an instance method and replace “sender” with “self”.
I apologize for formatting I’m posting from my phone. I’ll try to fix when back at a computer.
EDIT:
Change your updated method like so:
You don’t need to pass
selfinto a method, it always exists.