2 guodeganggdg guodegangGDG 于 2013.04.24 13:51 提问

UIButton传递@selector

代码:

- (void)showWithStatus:(NSString *)status barColor:(UIColor*)barColor textColor:(UIColor*)textColor click:(SEL)click{
    if(!self.superview)
        [self.overlayWindow addSubview:self];
    [self.overlayWindow setHidden:NO];
    [self.topBar setHidden:NO];
    self.topBar.backgroundColor = barColor;
    NSString *labelText = status;
    CGRect labelRect = CGRectZero;
    CGFloat stringWidth = 0;
    CGFloat stringHeight = 0;
    if(labelText) {
        CGSize stringSize = [labelText sizeWithFont:self.stringLabel.font constrainedToSize:CGSizeMake(self.topBar.frame.size.width, self.topBar.frame.size.height)];
        stringWidth = stringSize.width;
        stringHeight = stringSize.height;

        labelRect = CGRectMake((self.topBar.frame.size.width / 2) - (stringWidth / 2), 0, stringWidth, stringHeight);
    }
    self.stringLabel.frame = labelRect;
    self.stringLabel.alpha = 0.0;
    self.stringLabel.hidden = NO;
    self.stringLabel.text = labelText;
    self.stringLabel.textColor = textColor;

    clickBn=[[UIButton alloc]initWithFrame:self.stringLabel.frame];
    clickBn.backgroundColor=[UIColor blueColor];
    [clickBn addTarget:self action:click forControlEvents:UIControlEventTouchUpInside];
    if(!clickBn.superview)
        [self.topBar addSubview:clickBn];
    [UIView animateWithDuration:0.4 animations:^{
        self.stringLabel.alpha = 1.0;
    }];
//    [self setNeedsDisplay];
}

调用方法:

 - (IBAction)successButtonPressed:(id)sender {
[KGStatusBar showSuccessWithStatus:@"Successfully synced" click:@selector(clickBn)];
 }
 - (void)clickBn {
NSLog(@"sss");
 [KGStatusBar dismiss];
}

运行之后NSLog(@"sss") 不显示。我想要传递一个方法到自定义视图的UIButton,但是点击之后无任何响应。

1个回答

zhanglei5415
zhanglei5415   Rxr 2013.04.27 09:31

像你的这种设计不是太好.
使用代理协议来实现比较好

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!