selector as parameter in IOS

霸气de小男生 提交于 2020-01-02 01:09:07

问题


I want to raise a different method for each created button. I try to call "FirstImage" method in viewDidLoad but it doesn't work.

I have a problem with the selector in ViewDidLoad. Didn't recognise "FirstImage" which is a void method without parameter.

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]];
}

What I have done (I changed "CreateFirstButton" into "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 

I edited this post again and there is no more error. Special thanks to everybody. It took me time to understand :)


回答1:


You have to use use @selector as follows

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

Then your signature is wrong, since SEL shouldn't be a pointer.

Change

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

to

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

Finally myAction has type SEL so you can pass it directly to the UIButton method, as follows

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

Also, I'd like to add that using capitalized names for methods it's a very bad practice.




回答2:


You should use

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

or

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

@selector(myAction) should be replaced with myAction, and (SEL *) should be replaced with SEL.

In your code, before -createFirstButton:myAction: is called, its second parameter [self FirstImage] would be evaluated(called) first. @selector() takes a name and returns a SEL.




回答3:


Change the method literal as in the following

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

For both declaration and definition.

And also why have you declared the same method as a public as well as private?




回答4:


Use

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

And change the method name as :

- (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];
}

Also,

I wonder-all buttons having same frame, if possible pass frame this method.




回答5:


If you are looking to pass string as method name to selector, then use NSSelectorFromString. String should end with ':' Example-

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


来源:https://stackoverflow.com/questions/14083763/selector-as-parameter-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!