How to pause a method until a condition is true then resume - objective-c

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.

Related

Show button after two clicks Flash CS6 As2

I want to make my "button 3" visible after click in "button 1" and "button 2". How can I do it?
I have not tried any code cause I have no idea how to do this :p
You can use global variables for each bt and a listener for the last btn, for example, when you click bt1 replace the original value of the variable, lets say:
bt1 = 0; now could be bt1 = 1;
you write these somewhere in your first frame o in the root, just remember to point the right timeline if you write it outside your current timeline.
Let's say you write in the first frame of the root:
bt1=0;
bt2=0;
mybt._visible = false; //here you turn it off
onEnterFrame = function(){
if (bt1 == 1 && bt2 == 1){
trace("Say it Worked");
mybt._visible = true; //and here you turn it on
delete this.onEnterFrame;
}
}
Then each button must have something like:
on (release) {
_root.bt1 = 1; // or 2, depends on what button it is
// Your Actions
}
And your invisible button must have an instance name, in this case: "mybt"

How to prevent window from loosing focus when receiving a grabbed key from X11

My window calls hide() when a QEvent::FocusOut is received. Simultaniously I want its visibility to be toggled if a hotkey is pressed. Now I have the following problem: Pressing the hotkey, registered with XGrabKex(...) seems to steel the focus of my app. Resulting in an unwanted behaviour. If my app is visible the hotkeyevent steels focus, which results in a QEvent::FocusOut, which hides my app, and after that the hotkey is received which toggles visibility (shows) my app. I.e. my app does not hide when pressing the hotkey.
Is there a way to tell the x window system to not steel the focus when a grabbed key is pressed? Or are there other possible solutions to this problem?
A couple of different methods.
Use XQueryKeymap to see which keys are pressed. For instance, when you get a FocusOut event, call XQueryKeymap and see if your hotkey is pressed. If it is not, hide the window; if it is, don't hide it and wait for the hotkey event.
Delay hiding on FocusOut by 100 or so milliseconds. Cancel hiding if you either get the hot key or get your focus back during this time interval.
Look also here for useful info.
Finally got it to work in a "proper" way:
bool MainWidget::nativeEvent(const QByteArray &eventType, void *message, long *)
{
#ifdef Q_OS_LINUX
if (eventType == "xcb_generic_event_t")
{
xcb_generic_event_t* event = static_cast<xcb_generic_event_t *>(message);
switch (event->response_type & 127)
{
case XCB_FOCUS_OUT: {
xcb_focus_out_event_t *fe = (xcb_focus_out_event_t *)event;
if ((fe->mode==XCB_NOTIFY_MODE_GRAB && fe->detail==XCB_NOTIFY_DETAIL_NONLINEAR)
|| (fe->mode==XCB_NOTIFY_MODE_NORMAL && fe->detail==XCB_NOTIFY_DETAIL_NONLINEAR ))
hide();
break;
}
}
}
#endif
return false;
}

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
}
}

Determining which button the user pressed

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

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;
}
}