SahaNerede
SahaNerede

Reputation: 219

Tapping Dynamically created UIButtons Crash App

I have a method which makes buttons. arguments are text color position x position y button type and method (Note: this method is in a class)

-(UIButton *)makeButton :(NSString *)string :(UIColor*)textColor  :(CGFloat)posx :(CGFloat)posy :(UIButtonType)type :(SEL)run
{
    UIButton *button =[UIButton buttonWithType:type];
    [button addTarget:self action:run forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:string forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont systemFontOfSize:11];
    [button setTitleColor:textColor forState:UIControlStateNormal];
    CGSize stringSize =[string sizeWithFont:[UIFont systemFontOfSize:11]];
    [button setFrame:CGRectMake(posx, posy, stringSize.width+10, stringSize.height+5)];

    return button;
}

and I'm calling the method like this.

 UIButton *btnGidenler=  [methods makeButton:anlik :[methods RGBR:0 G:135 B:222 A:1.0] :10 :5:(UIButtonType)UIButtonTypeCustom :@selector(gidenKisiler)];
            [viewGidenler addSubview:btnGidenler];

and @selector(btnGidenler)

basic method -(void)gidenKisiler{ //just alert }

and when I tap the button, my app crashes.. Why is the selector not working ?

Crush Error

@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }

Upvotes: 0

Views: 118

Answers (1)

SahaNerede
SahaNerede

Reputation: 219

I found it.. I was adding target selector in my method and I think selector and method must be in same place so i move the addTarget part under the returning button Imean like this sorry about my english

UIButton *btnGidenler=  [methods makeButton:anlik :[methods RGBR:0 G:135 B:222 A:1.0] :10 :5:(UIButtonType)UIButtonTypeCustom :@selector(gidenKisiler)];
[button addTarget:self action:run forControlEvents:UIControlEventTouchUpInside];
            [viewGidenler addSubview:btnGidenler];

and it works!

Upvotes: 1

Related Questions