[英]pass @selector to my UIbutton but click event not response
my code : 我的代码:
- (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];
}
and call this method: 并调用此方法:
- (IBAction)successButtonPressed:(id)sender {
[KGStatusBar showSuccessWithStatus:@"Successfully synced" click:@selector(clickBn)];
}
- (void)clickBn {
NSLog(@"sss");
[KGStatusBar dismiss];
}
the NSLog(@"sss") not show. NSLog(@“ sss”)不会显示。 i want to pass a method to UIButton of customView ,but when i click the UIbutton not respond.
我想将方法传递给customView的UIButton,但是当我单击UIbutton时不响应。
Create an delegate in control in .h file before @interface
and after header files. 在.h文件中
@interface
之前和头文件之后的控件中创建委托。
@protocol YourControlDelegate <NSObject>
-(void) delegateMethod;
@end
in @interface
在
@interface
@property (nonatomic,assign) id <YourControlDelegate> yourdelegate;
in .m @synthesize
the yourdelegate
. 在
.m @synthesize
yourdelegate
。
in button click action call [self.yourdelegate delegateMethod]
; 在按钮中单击操作调用
[self.yourdelegate delegateMethod]
;
In your ViewController
add YourControlDelegate
like as follow 在您的
ViewController
添加YourControlDelegate
,如下所示
@interface YourVC : UIViewController <YourControlDelegate>
in .m
implement the delegate
method 在
.m
实现delegate
方法
-(void) delegateMethod
{
//you code.
}
Here when you click the button the delegateMethod
in viewController
will be get called. 在这里,当您单击按钮时,将调用
viewController
的delegateMethod
。
this is your clickBn method right? 这是您的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 should be a selector method. action应该是选择器方法。
If you want to pass the button as an argument to the method user :
for example 如果要将按钮作为参数传递给方法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.
}
it is preferred. 这是首选。 If you don't want to use sender(button) exclude the
:
如果您不想使用sender(button),请排除
:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.