Determining which button the user pressed - objective-c

I would like to have three buttons or images in my view, that each represent a "weapon". How can I know which button has been selected by the user, and use this information?
I thought I would use one function to collect the information about the weapon selected, its damage, etc., but I'm used to creating one function for each button in the view. I'm wondering now how to determine the difference between those buttons, depending on which one has been selected.

You should create action methods that take one argument, the sender:
- (IBAction)weaponPressed:(id)sender;
You can then check the sender against instance variables pertaining to the buttons:
if (sender == gunWeaponButton)
// Do something
else if (sender == mineWeaponButton)
// Do something
else
// Do something else
Also, you can assign a tag to the buttons, which is simply an integer value:
gunWeaponButton.tag = 0;
Then you can check for the sender's tag:
if (sender.tag == gunWeaponButton.tag)
// Do something

Related

How to pause a method until a condition is true then resume

Upon a button being pressed, i would like to begin tracking whether other buttons are being pressed.
Right now this is how i am attempting to do like this:
while(true)
{
if button1pressedbool and button2pressedbool
{
break
}
usleep(20000)
}
[self doMethodNowThatButtons1&2arePressed]
I need for the user to select another button before proceeding in this method, is there a better way to do this?
Edit: User...'s answer looks correct, does anybody have an example code of how to use and check a selector as he suggested?
There is a much better way to do this.
Set button1 tag to 0 and button2 tag to 1.
button1.tag = 0;
button2.tag = 1;
Then set the action for both buttons for the same selector.
In that method, check the tag of the button (use sender). If the tag == 0 set a BOOL button1Pressed to YES. Do the same for the other button.
This way, regardless of which button you pressed first, when button1Pressed == YES && button2Pressed == YES you can do whatever you want.

how can i give multiple actions to button on iPhone

I have two buttons,if the first button is clicked two textfields will come and the second button dimension will change to next to the textfields.Now if i click the second button one textfield will appear.I did all those things.Now my question is if we click the second button,textfield will appear with what ever the dimensions we have given,but if we click the second button after clicking the first button also dimensions should change.Can any one help to do this.
Thank You
you can add multiple selectors on a single button for different control states
Your question is vaguely phrased. If I understood correctly, when the second button is pressed, you want two different actions done depending on whether the first button has previously been pressed or not. If that's the case, the solution is to define a boolean flag, which will be set to YES when the first button is pressed, and will be set to NO (if needed) after the second button's action is completed.
In your header file:
#interface yourViewController: UIViewController {
BOOL firstButtonFlag;
}
And in yourViewController.m file:
-(void)viewDidLoad {
firstButtonFlag = NO; // technically redundant line, just to show the concept
}
-(IBAction)firstButtonPressed {
firstButtonFlag = YES;
// whatever action you want done
}
-(IBAction)secondButtonPressed {
if (firstButtonFlag) {
// whatever action if first button had been pressed before
firstButtonFlag = NO; // resetting the flag for next round
}
else {
// whatever action if first button had NOT been pressed before
}
}

NSMenu --> Adding SubMenu --> receive select event

I need to create a Menu for a selected row of a table,
I am able to create by overriding
-(NSMenu*)menuForEvent:(NSEvent*)evt
Method, of a table, This particular Menu has two sub-menu, i am able to make the Sub-Menu
but facing Following Problem
1 -- To Add a Sub-Menu in a MenuItem, i need to get Sub-Menu from a different class/Interface, i am calling following method
pSubMenu = [CommonUIUtil GetCommonUIMenu:pSubMenu
ActionId:self
Selector:#selector(MyMenuAction)];
Where prototype of this function is as follow
+(NSMenu *)GetCommonStatusMenu:(NSMenu *)pMenu ActionId:(NSObject*)aDelegate Selector:(SEL)selector
Implementation as below,
// pStrArray is Array of String to have the Menu Title
for(int idx =0;idx<[pStrArray count];idx++)
{
pTempStr = [pStrArray objectAtIndex:idx];
pImage = [CommUIController CommonGetImage:[CommonUIUtil GetImageByStatus:pTempStr]];
[pImage setSize:NSMakeSize(20,20)];
pMenuItem =[[NSMenuItem alloc]init];
[pMenuItem setTitle:pTempStr];
// this should set the selector
[pMenuItem setAction:selector];
// Setting the target
[pMenuItem setTarget:aDelegate];
[pMenuItem setImage:pImage];
[pMenuItem setEnabled:YES];
[pMenu addItem:pMenuItem];
[pMenuItem release];
}
return pMenu;
I am able to see the Image, String on this Sub Menu, but the problem i am facing is this menu is not at all Enable, can anyone guide me , where i am making the mistake,
This function will return the Menu and Menu i will add as below,
pMenuItem = [pCTTableMenu itemWithTitle:#"Status"];
//status menu is the menu returned from the above function,
[pMainMenu setSubmenu:pStatusMenu forItem:pMenuItem];
Thanks in Advance :)
looks like, i am not passing the selector method properly, in fact i don't know how to pass function pointer in Cocoa, probably i am mixing Cocoa/Objective C and Normal C both :), corrected, it, in the method just creating the view and in the main class/interface assigning target and action

Objective C - one button with multiple sequential outcomes

I am new to programing so any help is appreciated. I am trying to find the method for the following. I need one button that will, when executed, perform a different task in a sequence when activated. For example the first time that the button is pressed, I would like it to act as if the user is confirming the information in the title of the button (i.e. confirms that the user is older than 45). The second time that the user presses the button it confirms the response in the negative (i.e. confirms the user is NOT older than 45). The third time the user presses the button the field is reset and no value has been assigned (i.e. it is as if the user never answered the question).
For real-estate purposes I would like the information in question to be the title of the button and update appropriately once the action has been captured. For example, if the action is positive the display is shown with a circle around the title. If the action results in a negative response, the title is shown with a line through it.
I have done quite a bit or reading to try to get a solution but no dice so far. I have tried overlaying a button on a label with the text of the label changing, but so far I am not having any luck
I can't imaging that this is a unique issue and any help is appreciated. Much thanks in advance.
On the event just have some code like this
Header
- (IBAction)buttonPressed:(id)sender{
static int counter;
if (counter == 0){
// do stuff for first time Example [sender setTitle:#"45"];
}else if (counter == 1){
// do stuff for second time Example [sender setTitle:#"-45"];
}else if (counter == 2){
// do stuff for the third time
}
counter += 1;
if (counter > 2){
// restart the counter
counter = 0;
}
}

Something like .NET's "tag" property for Interface Builder

I'm currently struggling to use UI elements in Interface Builder. I keep trying to do things "in the .NET way."
I have several buttons that all map down their TOUCH event to the SAME FUNCTION:
-(IBAction) onTouch:(id) sender
{
// do something with touch, DEPENDING ON WHAT BUTTON WAS PUSHED
// I want to do something like
if( sender.tag == "something" )
{
//...doesn't work on apple, of course..
}
}
I want to uniquely identify each BUTTON USING SOMETHING like the TAG property in .NET. I tried using the INTERFACE BUILDER "NAME" field that is on the "Identity" panel of interface builder, but I don't know how to access that field programmatically.
-(IBAction) onTouch:(id) sender
{
// do something with touch, DEPENDING ON WHAT BUTTON WAS PUSHED
// I want to do something like
if( sender.InterfaceBuilderName == "something" )
{
//...doesn't work..
}
}
So, WHAT / IS THERE a way to uniquely identify a UI element (such as a button) OTHER THAN doing something like
-(IBAction) onTouch:(id) sender
{
// look at
[sender currentTitle]
}
The reason that's bad is because if the text on the button changes for some cosmetic reason you break the whole app, right
The last solution I can think of is write seperate functions for each button's touch event but I really want to know if it is possible to uniquely identify a button by something similar to .Net's TAG property.
In the iPhone SDK all UIView objects have a property also called tag which is an integer value and can basically be used to do what you are intending.
I usually define a constant for the tag values I'm going to use for a specific purpose.
You can access the tag on the button object:
myButton.tag = MYBUTTON_TAG_CONSTANT
// button tag constant
#define MYBUTTON_TAG_CONSTANT 1
For buttons, there is a Tag entry in the View section (click on your button, select Attributes Inspector from the Tools menu). You can then use this integer value in your code.
Here is a link that may help as well:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/25582-using-tags-interface-builder.html
UIView's tag property is accessible from Interface Builder. Unlike .NET, it's an integer rather than a string.