Search code examples
iosuibuttonprogrammatically-created

Programmatically added button wont work


So this is what i have. I have created a button and set it to an action but every time i click the button it crashes the program. Why wont my button work? Thanks in advance.

UIButton *currentGamesButton = [UIButton buttonWithType:UIButtonTypeCustom];
currentGamesButton.frame = CGRectMake(124,18,72,65);
[currentGamesButton addTarget:self
                       action:@selector(goToCurrentGamesViewController:)
             forControlEvents:UIControlEventTouchDown];
UIImage *currentGamesPNG = [UIImage imageNamed:@"CurrentGamesHighlightedState.png"];
[currentGamesButton setBackgroundImage:currentGamesPNG forState:UIControlStateNormal];
[self.view addSubview:currentGamesButton];

Solution

  • If the method for goToCurrentGamesViewController takes no parameters, change this line:

    [currentGamesButton addTarget:self
                       action:@selector(goToCurrentGamesViewController:)
    

    to:

    [currentGamesButton addTarget:self
                       action:@selector(goToCurrentGamesViewController)
    

    (remove the colon : from the method in the selector)