2013-04-23 61 views
0

我的代碼:通@selector我的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,但是當我點擊UIbutton沒有迴應。

+0

什麼是'KGStatusBar',一個類?如果是的話,問題是你正在調用 – tkanzakic 2013-04-23 06:53:01

+0

類的實例方法是的,你是對的。它是KGStatusBar,我想要自定義點擊事件。 – pengwang 2013-04-23 06:54:45

+0

你必須從一個實例變量進行調用,而不是從它自己的類,這是奇怪的,編譯器不會警告你這個 – tkanzakic 2013-04-23 06:57:22

回答

1

@interface之前和頭文件之後的.h文件中創建一個控制委託。

@protocol YourControlDelegate <NSObject> 

-(void) delegateMethod; 

@end 
@interface

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

.m @synthesizeyourdelegate

in button click action call [self.yourdelegate delegateMethod];

在你ViewController添加YourControlDelegate像如下

@interface YourVC : UIViewController <YourControlDelegate> 

.m實現delegate方法

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

這裏當你點擊按鈕viewControllerdelegateMethod將被調用。

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 
+0

我想發送選擇器到另一個類 – pengwang 2013-04-23 06:57:55

+0

答案更新檢查 – 2013-04-23 07:07:02

+0

再次回答是更新檢查 – 2013-04-23 07:13:17

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

它是優選的。如果您不想使用發件人(按鈕),請排除:

+0

buttonClicked方法來自控件,我的按鈕來自我的自定義view.i想要將buttonClicked從控件傳遞到視圖 – pengwang 2013-04-23 07:08:31

+0

實現委託。 – AMohan 2013-04-23 07:10:02