Can a button have 2 actions?

Discussion in 'iOS Programming' started by themacster298, Mar 10, 2011.

  1. themacster298 macrumors member

    Joined:
    Feb 20, 2011
    #1
    Hi! I want to put a button that switches to another view. but I also want to put music when the button is pressed. but when i try it in the emulator it just crashes and takes you to the homescreen. Am I doing something wrong? Can a button have 2 actions? Or do I have to try it in a real iPhone?
     
  2. Unixed macrumors newbie

    Joined:
    Mar 10, 2011
    #2
    Try this type of logic:

    Code:
    - (IBAction)myBtnPushed:(id)sender{
    [self playMusic];
    Right *DetailViewController = [[Right alloc] initWithNibName:@"Right" bundle:[NSBundle mainBundle]];
        [detailViewController.navigationController pushViewController:DetailViewController animated:YES];
    }
    
    - (void)playMusic{
     . . .
    }
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    First of all, u never showed us what u tried, so how can we know if it crashes, and why would there be an simulator if u can't do anything with it.
    So, problem is, you're probably doing something right.
    The guy above me gave u perfect code for switching views, if u have a navcontroller in ur view.
    Allthough u have to add the release.
    And his declaring
    Code:
    Right *DetailViewController = [[Right alloc] initWithNibName:@"Right" bundle:[NSBundle mainBundle]];
        [detailViewController.navigationController pushViewController:DetailViewController animated:YES];
    }
    He forgot capitals

    More like this
    {
    Code:
    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
    }
    Because using Capitals on ur properties is not the right way to go.
    Second, add the release. and do the push on self. so it adds a retain.
    Good luck.
    The playing sound is about adding ur AVAudioPlayer and look up a tutorial about it, isn't that hard.
     

Share This Page