select器作为IOS中的参数

我想为每个创build的button提出一个不同的方法。 我尝试在viewDidLoad中调用“FirstImage”方法,但它不起作用。

我在ViewDidLoad中的select器有问题。 没有认出“FirstImage”这是一个没有参数的void方法。

ViewController.m

- (void)createFirstButton:(NSString *)myName: (SEL *)myAction{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:myName forState:UIControlStateNormal]; btn.frame = CGRectMake(20, 916, 120, 68); [self.view addSubview:btn]; } - (void)viewDidLoad{ [self createFirstButton:@"First" myAction:[self FirstImage]]; } 

我做了什么(我把“CreateFirstButton”改成了“CreateButton”):

ViewControler.m

 #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize myHeight; @synthesize myWidth; @synthesize myX; @synthesize myY; - (void)createButton:(NSString *)myName:(SEL)myAction:(NSUInteger)my_x:(NSUInteger)my_y:(NSUInteger)my_width:(NSUInteger)my_height { UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn addTarget:self action:myAction forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:myName forState:UIControlStateNormal]; btn.frame = CGRectMake(my_x, my_y, my_width, my_height); [self.view addSubview:btn]; } - (void)myXcrementation{ myX = myX + 150; } - (void)viewDidLoad{ myX = 20; myY = 916; myWidth = 120; myHeight = 68; [self createButton:@"First":@selector(FirstImage):myX:myY:myWidth:myHeight]; [self myXcrementation]; [self createButton:@"Previous":@selector(PreviousImage):myX:myY:myWidth:myHeight]; [self myXcrementation]; [self createButton:@"Pause":@selector(PauseImage):myX:myY:myWidth:myHeight]; [self myXcrementation]; [self createButton:@"Next":@selector(NextImage):myX:myY:myWidth:myHeight]; [self myXcrementation]; [self createButton:@"Last":@selector(LastImage):myX:myY:myWidth:myHeight]; } - (void)FirstImage{ current = 0; [self SetImage]; } -(void)SetImage{ [myImageView setImage: [myArray objectAtIndex:(current)]]; } @end 

ViewController.h

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ } @property(assign, nonatomic) NSUInteger myHeight; @property(assign, nonatomic) NSUInteger myWidth; @property(assign, nonatomic) NSUInteger myX; @property(assign, nonatomic) NSUInteger myY; @end 

我再次编辑这个post,没有更多的错误。 特别感谢大家。 这花了我的时间来理解:)

您必须按如下所示使用@selector

 [self createFirstButton:@"First" myAction:@selector(FirstImage)]; 

那么你的签名是错误的,因为SEL不应该是一个指针。

更改

 - (void)createFirstButton:(NSString *)myName: (SEL *)myAction{ 

 - (void)createFirstButton:(NSString *)myName: (SEL)myAction{ 

最后myAction具有SELtypes,所以你可以直接将它传递给UIButton方法,如下所示

 [btn addTarget:self action:myAction forControlEvents:UIControlEventTouchUpInside]; 

另外,我想补充说,使用大写的名字的方法这是一个非常糟糕的做法。

你应该使用

 - (void)viewDidLoad{ [self createFirstButton:@"First" myAction:@selector(FirstImage)]; } 

要么

 - (void)viewDidLoad{ [self createFirstButton:@"First" myAction:NSSelectorFromString(@"FirstImage")]; } 

应该用myActionreplace@selector(myAction) ,并且用SELreplace(SEL *)

在你的代码中, -createFirstButton:myAction:被调用之前,它的第二个参数[self FirstImage]将被首先评估(调用)。 @selector()取一个名字并返回一个SEL。

如下所示更改方法字面值

 - (void)createFirstButton: (NSString *)myName withAction: (SEL)myAction 

对于声明和定义。

而且你为什么要宣布同样的方法作为公共和私人?

使用

 [self createFirstButton:@"First" withAction:NSSelectorFromString(@"FirstImage")]; 

并将方法名称更改为:

 - (void)createFirstButton:(NSString *)myName: withAction: (SEL) myAction{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn addTarget:self action:myAction forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:myName forState:UIControlStateNormal]; btn.frame = CGRectMake(20, 916, 120, 68); [self.view addSubview:btn]; } 

也,

我不知道 – 所有button具有相同的帧,如果可能的话,通过frame这种方法。

如果您正在将string作为方法名称传递给select器,则使用NSSelectorFromString。 string应该以':'结尾示例 –

  NSString *myName=@"aMethod:"; [button addTarget:self action:NSSelectorFromString(myName) forControlEvents:UIControlEventTouchDown];