将@selector传递给我的UI按钮,但点击事件不响应

时间:2013-04-23 06:47:45

标签: iphone ios uibutton

我的代码:

 - (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”)未显示。我想将方法​​传递给customView的UIButton,但是当我点击UI按钮时没有响应。

3 个答案:

答案 0 :(得分:1)

@interface之前和头文件之后,在.h文件中创建一个控制委托。

@protocol YourControlDelegate <NSObject>

-(void) delegateMethod;

@end
@interface

中的

@property (nonatomic,assign) id <YourControlDelegate> yourdelegate;
<{1>} .m @synthesize

按钮点击动作调用yourdelegate;

[self.yourdelegate delegateMethod]添加ViewController,如下所示

YourControlDelegate
@interface YourVC : UIViewController <YourControlDelegate> 中的

实施.m方法

delegate

当您点击该按钮时,-(void) delegateMethod { //you code. } 中的delegateMethod将被调用。

答案 1 :(得分:0)

这是你的clickBn方法吗?

- (void)clickBn {
NSLog(@"sss");
 [KGStatusBar dismiss];
}

now what you doing in action? action:click??
do like following 

clickBn=[[UIButton alloc]initWithFrame:self.stringLabel.frame];
    clickBn.backgroundColor=[UIColor blueColor];
    [clickBn addTarget:self action:@selector(clickBn)  forControlEvents:UIControlEventTouchUpInside];
 If you want to call method which is in another class then do like this 
first make object of that class


  ViewController *dvController = [ViewController alloc] init];
   after that [dvController methodName];
do this all in your clickBn method First clickBn method will be called and it will call that another class method which you wanted to call 
 NOTE: you need to import that class in header 

答案 2 :(得分:0)

动作应该是一个选择器方法。

如果要将按钮作为参数传递给方法用户:,例如

[button addTarget:self action:@selector(buttonClicked:) 
                                         forControlEvents:UIControlEventTouchUpInside];

-(void)buttonClicked:(UIButton *)sender
{
   //your code and you can also access the button properties using sender.
}

是首选。如果您不想使用发件人(按钮),请排除: