选择器作为IOS中的参数

我想为每个创建的按钮提出一种不同的方法.
我尝试在viewDidLoad中调用“First Image方法,但它不起作用.

我在ViewDidLoad中的选择器有问题.没有识别“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

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

解决方法

你必须使用@selector,如下所示
[self createFirstButton:@"First" myAction:@selector(Firstimage)];

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

更改

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

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

最后,myAction的类型为SEL,因此可以直接将其传递给UIButton方法,如下所示

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

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...