PDA

View Full Version : Button Detection Within Button Detection




Kelzorz
May 6, 2011, 05:45 PM
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)
armiesA++;
pool--;
}
if (phase == 2)
{
if ( //button pressed// )
//do stuff//
}
}



dantastic
May 7, 2011, 03:41 AM
Can you explain a bit more what you mean by button detection within button detection?

What is it you're trying to achieve?

Kelzorz
May 7, 2011, 10:51 AM
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.

robbieduncan
May 7, 2011, 12:10 PM
I'm inside an IBAction that is a button,

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

Kelzorz
May 7, 2011, 03:41 PM
What? IBActions are not buttons. IBActions are instance methods of an object, normally in the controller layer.
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.

Sykte
May 7, 2011, 09:53 PM
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?

dejo
May 7, 2011, 11:11 PM
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.

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.

Kelzorz
May 8, 2011, 07:55 AM
right, ok, my pseudo code should look something like this...

if (button is pressed)
{
do
{
if (button2 is pressed)
{
do stuff
{
}
}

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?

robbieduncan
May 8, 2011, 12:40 PM
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.

dantastic
May 8, 2011, 01:57 PM
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.