![](/img/trans.png)
[英]iOS Subclassed UIView - How to have UIButton call method from ViewController?
[英]iOS - Closing a Subclassed UIView from view's uibutton
我有一個自定義子類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];
您可能嘗試在視圖實例上調用您的方法(類似[noticeDropDown closeNoticeDropDown:...]
),但closeNoticeDropDown:
是一個類方法,您應該這樣調用它:
[NoticeDropDown closeNoticeDropDown: noticeDropDown];
你的動畫代碼中也很少有東西看起來錯了:
[UIView commitAnimations];
應該刪除調用,因為它們與[UIView beginAnimations:... context:...]方法配對使用,基於塊的動畫不需要它們
[sender removeFromSuperview];
call應該進入動畫的完成塊,否則它將在動畫開始之前被調用,你將無法獲得所需的效果
您已將方法聲明為類方法,但正在將消息發送到實例。 如果您希望它仍然是一個類方法,請將[NoticeDropDown類]作為目標參數傳遞給addTarget:action:forControlEvemts:方法。 否則在方法聲明中用“ - ”替換“+”。
此外 - 當UIControl操作具有發件人參數時,它將發送控件作為發件人 - 因此您將獲得UIButton而不是您的視圖。
我的建議是將您的操作更改為實例方法,並將“sender”替換為“self”。
我為從手機上發布的格式道歉。 我會在回到電腦前嘗試修復。
編輯:
更改您的更新方法,如下所示:
-(void)closeNoticeDropDown:(id)sender{
NSLog(@"Frame: %f",notice.frame.size.width);
//Animate In
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationCurveEaseOut
animations:^{
self.frame = CGRectMake(0,-70,320,60);
}
completion:^(BOOL finished){
[self removeFromSuperview];
}
];
}
您不需要將self
傳遞給方法,它始終存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.