1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Button Detection Within Button Detection

Discussion in 'iPhone/iPad Programming' started by Kelzorz, May 6, 2011.

  1. Kelzorz, May 6, 2011
    Last edited by a moderator: May 6, 2011

    macrumors newbie

    How do i put button detection in an if statement, here is my code where IBActionA is a button...

    - (IBAction)A: (id)sender {
        if (phase == 1)
            if (pool > 0)
        if (phase == 2)
            if (  //button pressed// )
               //do stuff//
  2. macrumors 6502


    Can you explain a bit more what you mean by button detection within button detection?

    What is it you're trying to achieve?
  3. macrumors newbie

    I'm inside an IBAction that is a button, and i want have the user press the button, then the code will wait until a different specific button is pressed. the goal of this code is to create a game of risk where you select a territory the select a territory to attack, by touching buttons corresponding to each territory.
  4. Moderator


    Staff Member

    What? IBActions are not buttons. IBActions are instance methods of an object, normally in the controller layer.
  5. macrumors newbie

    ug, thats my bad grammar. button A's file owner is -(IBAction)A: (id)sender, thats the one it is linked to in the view controller.
  6. macrumors regular

    The answer to your question is within this line of code.

    - (IBAction)A: (id)sender

    Do you understand what IBAction is for?
    Can you explain what sender is and why it's included in the method?
  7. Moderator


    Staff Member

    Your grammar is still off. A UIButton's file owner is not an IBAction. It's normally an instance of a UIViewController subclass. An IBAction is just a method that is called when an event on the button is triggered, normally Touch Up Inside.
  8. macrumors newbie

    right, ok, my pseudo code should look something like this...

    if (button is pressed)
            if (
    button2 is pressed)
    how do i do the second button pressed, the first one is a method, is it possible to nest methods or do i need some other kind of command?
  9. Moderator


    Staff Member

    You are thinking about this in 100% completely the wrong way. In fact in such a way that I question if you have any understanding of event-driven object-oriented programming. You really need to learn the theory before even thinking about writing code. My best suggestion would be to go off and learn the theory.

    If you are unwilling to take this considered advice then you need to at least think about this in a better way. You can view an object (in this case your controller object) as encapsulated state (the instance variables) and methods that operate on the state (for example the IBAction methods). You need to update the state of the object when the first button is touched and then use this state to do the correct thing when the second button is touched.
  10. macrumors 6502


    What you're after is
    - (IBAction)buttonPressed:(id)sender {
        UIButton *button = (UIButton*)sender;
        if (button == buttonA){
            //Do button a stuff
        else {
            //Do all other buttons' stuff
    You'll get that one for free if you promise to get a book.

Share This Page