gpt4 book ai didi

iOS - 从 View 的 uibutton 关闭子类 UIView

转载 作者:行者123 更新时间:2023-11-28 22:49:12 26 4
gpt4 key购买 nike

我创建了一个自定义子类 uiview,用于显示应用内通知。我可以很好地调用 View ,但在使用 uibutton(嵌入在自定义 View 中)关闭它时遇到问题

当按下按钮时,应用程序崩溃并且出现此错误:

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 Controller 查看调用:

noticeDropDown = [[NoticeDropDown alloc] initWithMessage:message];
[self.view insertSubview:noticeDropDown belowSubview:hudContainerTop];

最佳答案

您已将您的方法声明为类方法,但正在将消息发送到实例。如果您仍然希望它是一个类方法,请将 [NoticeDropDown class] 作为目标参数传递给您的 addTarget:action:forControlEvemts: 方法。否则将方法声明中的“+”替换为“-”。

此外,当 UIControl 操作具有发送器参数时,它将作为发送器发送控件 - 因此您将获得一个 UIButton 而不是您的 View 。

我的建议是将您的操作更改为实例方法并将“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 传递给方法,它始终存在。

关于iOS - 从 View 的 uibutton 关闭子类 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12374389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com