我创建了一个自定义子类 uiview 来显示应用内通知。我可以很好地调用视图,但是在使用 uibutton (嵌入在自定义视图中)解除它时遇到问题
按下按钮时,应用程序崩溃,我收到此错误:
更新 - 修复了上述问题,但现在只有按钮消失,而不是实际视图。请参阅下面的更新代码。
-(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;
}
];
}
来自另一个视图控制器的视图调用:
noticeDropDown = [[NoticeDropDown alloc] initWithMessage:message];
[self.view insertSubview:noticeDropDown belowSubview:hudContainerTop];