0

我的代码:</p>

 - (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 ,但是当我单击 UIbutton 时没有响应。

4

3 回答 3

1

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

@protocol YourControlDelegate <NSObject>

-(void) delegateMethod;

@end

@interface

@property (nonatomic,assign) id <YourControlDelegate> yourdelegate;

在. .m @synthesize_yourdelegate

在按钮点击动作调用中[self.yourdelegate delegateMethod]

在您的ViewController添加YourControlDelegate中,如下所示

@interface YourVC : UIViewController <YourControlDelegate>

.m实现delegate方法

-(void) delegateMethod
{
    //you code.
}

在这里,当您单击按钮时,将调用delegateMethodin 。viewController

于 2013-04-23T07:20:06.967 回答
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 
于 2013-04-23T06:56:14.650 回答
0

action 应该是一个选择器方法。

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

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

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

它是首选。如果您不想使用 sender(button) 排除:

于 2013-04-23T07:00:12.067 回答