iOS - 从 View 的 uibutton 关闭子类 UIView

标签 ios uiview uibutton subclass

我创建了一个自定义子类 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/

相关文章:

ios - Apple Push Notification 服务 SSL(Sandbox & Production)证书是否通用?

ios - 应用内购买是否适用于电子书/有声读物?

ios - 如何自动调整包含多行 UIButton 的 UITableViewCell 的高度?

multithreading - 从 UIView 制作 UIImage 但不在主线程中

iphone - 使用 transitionFromView 和 transitionWithView 的过渡行为

ios - 我如何为 UIButton iOS 创建脉动动画?

ios - 如何在 UITableView 按钮单击中快速获取特定行按钮标题

ios - 是否可以将导航栏添加到选项卡式栏 Controller ?或者我是否将选项卡式栏 Controller 放在 Xcode 的导航 Controller 中?

ios - 如何使用 NSTimer 传递两个参数?

objective-c - UIVIew 动画在 iOS 10 中无法正常工作