开发者

Problem passing method as parameter

I'm trying to write a convenience method that returns a UIButton and takes a title, CGRect and an action as parameters. My code compiles and runs, but crashes when I actually click the button.

Here is my convenience method:

+ (UIButton *)makeButton:(NSString *)title withRect:(CGRect)rect 
        withAction:(SEL)selectorX  {

    // setup button properties
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = rect;
    [btn setFont:[UIFont boldSystemFontOfSize:20]];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [btn setBackgroundImage:[[UIImage imageNamed:@"btn_21.png"]
        stretchableImageWithLeftCapWidth:10.0                                                                                    
        topCapHeight:0.0] forState:UIControlStateNormal];
    [btn setTitle:@"TEST" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(selectorX:) 
        forControlEvents:UIControlEventTouchUpInside];

    return btn;
}

And here is how I'm calling it:

- (void)viewDidLoad {
    [super viewDidLoad];

    btn = [Utilities makeButton:@"TEST" withRect:CGRectMake(10, 100, 236, 45) 
            withAc开发者_高级运维tion:@selector(buttonActionTEST:)];
    [self.view addSubview:btn];
}

- (void) buttonActionTEST:(id)sender {

     [Utilities alert:@"yo yo yeoman"];

}


You need to provide both a target and a selector to call on that target. You're only providing the selector, and then do this:

[btn addTarget:self action:@selector(selectorX:) 
    forControlEvents:UIControlEventTouchUpInside];

That's wrong on two accounts: first, self in this context evaluates to the class Utilities (not even an instance, but the Class object). And then, your attempt to provide the selector is also wrong as you pass a hardcoded selector selectorX: and not the variable with the almost-same name. It should be done like this:

+ (UIButton *)makeButton:(NSString *)title withRect:(CGRect)rect 
        target:(id)aTarget action:(SEL)anAction  {
...
    [btn addTarget:aTarget action:anAction 
        forControlEvents:UIControlEventTouchUpInside];
...
}

and then you call it like this:

btn = [Utilities makeButton:@"TEST" withRect:CGRectMake(10, 100, 236, 45) 
        target:self action:@selector(buttonActionTEST:)];


Your convenience method already takes a selector as a parameter -- it is truly a selector, so near the bottom of that method use:

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

(no need to wrap selectorX in @selector() as it is a selector).


I think the problem is coming from the line:

[btn addTarget:self action:@selector(selectorX:)       forControlEvents:UIControlEventTouchUpInside];

Wouldn't "self" there be the Utilities class? Also I don't think you need the @selector there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜