簡體   English   中英

將@selector傳遞給我的UIbutton但單擊事件未響應

[英]pass @selector to my UIbutton but click event not response

我的代碼:

 - (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時不響應。

在.h文件中@interface之前和頭文件之后的控件中創建委托。

@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.
}

在這里,當您單擊按鈕時,將調用viewControllerdelegateMethod

這是您的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 

action應該是選擇器方法。

如果要將按鈕作為參數傳遞給方法user :例如

[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),請排除:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM