UISegmentedControl determine which button is tapped when momentary = YES - objective-c

I have a UISegmentedControl element where there are two buttons (i.e "Delete all Points" and "Delete Pt #x"). I don't want the buttons to ever remain selected so I have them set as "momentary = YES". I also don't want to handle click events until AFTER the user has released a button. The problem is, I can't figure out which of the two buttons was pressed within the UISegmentedControl view.
What I've tried:
1) I've tried using the "ValueChanged" event. The nice thing here is I can get the selected index for which button was pressed (even when momentary=YES), but the problem is that this event if triggered when the user presses down... which I don't want. I want the event when the user releases their finger, and at that I only want the event when the user releases WITHIN the button view
2) I've tried using the "Touch Up Inside" event. But it doesn't trigger any events when I tap on one of the buttons...
Any help on this would be appreciated. (Programming in Objective-C)

I'm not sure what you mean when you say "I've tried using the "Touch Up Inside" event." Unless you specify precisely what you tried, with code, that is just a meaningless phrase.
Ideally, you'd want create a method something like the following, and set it up as the button's action:
- (IBAction)myButtonTouched:(id)sender forEvent:(UIEvent *)event {
if (event.type == UIControlEventTouchUpInside) {
// do your thing
}
}
Basically you send every user event for that button to the handler and test for the one you want.

Related

NSEvent clickCount ignore single click method if double click occurs

Is it possible to ignore the first click if a double click is detected?
The following example will still log the one click even before the two clicks. I want to ignore the single click if there is a double click event.
- (void)mouseUp:(NSEvent *)theEvent {
if (theEvent.clickCount > 1) {
NSLog(#"two clicks %ld", theEvent.clickCount);
} else {
NSLog(#"one click %ld", theEvent.clickCount);
}
}
The purpose of doing this, is when the user single clicks in an NSView I want to perform a method that makes another view or window to appear. However, if the user double clicks inside the NSView then I want a different view or window to appear. The following steps outline this work flow:
user single clicks the NSView to make ViewA appear
user double clicks the NSView to make ViewB appear
The problem that I'm having is the single click is always detected therefore ViewA will always appear. But I do not want ViewA to appear if there is a double click, I just want ViewB to appear when there is a double click.
Unless, you can go back in time, you will need to cancel the previous single tap event somehow.
The easiest way: delay the execution on single tap event, and cancel it out if double click is detected.
[self performSelector:#selector(singleTap) withObject:nil afterDelay:[NSEvent doubleClickInterval]];
/// cancel out
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap) object:nil];
Or, you can execute single tap action immediately, and write a code to cancel it out if double click is detected. This should be very easy too. Because, if it's hard, there might be something wrong with your UX design.

Two buttons, one click

I have two buttons on top of each other in my nib. I need both of them to be pressed when tapped, but only the top button carries out its function. Is there a way for the top button to tell the bottom button to activate when the top one gets pressed. I do not think I can just merge the buttons into one and use one -(IBAction)buttonName function because one button is bigger than the other so I do not always need both to activate when one of them is pressed.
Thanks!
Is there a way for the top button to tell the bottom button to
activate when the top one gets pressed.
Not really, but you can have the action for the top button call the action for the bottom button.
Here's one way:
- (IBAction)actionTop:(id)sender
{
NSLog(#"The top button was activated.");
[self actionBottom:self];
}
- (IBAction)actionBottom:(id)sender
{
NSLog(#"The bottom button was activated.");
}
Another way would be to use the same action for both, and figure out what to do based on which button triggered the action:
- (IBAction)action:(id)sender
{
// if the top button was tapped, do this part
if (sender == self.topButton) {
NSLog(#"The top button was activated.");
}
// you want the bottom button to be activated no matter which button was tapped, so
// no need to check here...
NSLog(#"The bottom button was activated.");
}
the bottom button is the whole screen which changes what is displayed.
the top button plays a sound, except there are 4 top buttons that
plays different sounds
It seems like an invisible button covering the whole screen might be the wrong way to tackle the problem. You might look into using a gesture recognizer attached to your view to trigger the change. Your button actions could call the same method that the gesture recognizer uses.
Since one button is larger than the other, I assume that you would like the smaller button to "bring down" the larger button with it, including the change in the visual state. In cases like that you could send your target button a "tap" programmatically, like this:
- (IBAction)largeButtonClick:(id)sender {
}
- (IBAction)smallButtonClick:(id)sender {
// Perform the acton specific to only the small button, then call
[largeButton sendActionsForControlEvents:UIControlEventTouchUpInside];
}

How to differentiate mouse click from key press for NSButton in IBAction

Currently I have an IBAction for a NSButton, and the NSButton also has a key equivalent. I would like to know if there is a way inside my IBAction function "strike"
- (IBAction)strike:(id)sender
that can tell me whether this action is triggered by a mouse click on the button or a press of the key equivalent of the button?
get the current event using -currentEvent and using if loop check for mouse click count
if([theEvent clickCount]>=1)
{
mouse clicked;
}
else
{
button pressed;
}

NSComboBox action selector fires when setHidden:YES

I have an NSComboBox. I've set an action selector. When the box gets hidden the selector fires, even if the user never touched it. Yes, I need to hide it.
IBOutlet NSComboBox *comboBox;
[comboBox setAction:#selector(onComboBoxSelection:)];
- (void)onComboBoxSelection:(id)sender
{
NSLog(#"Why does this fire twice");
//My code doesn't actually set hidden here, it's just for proof while debugging the issue.
[comboBox setHidden:YES];
}
Why would hiding an NSControl fire it's selector? What's the best way to fix it?
Update:
I've fixed it by wrapping the method. But I'd still like to understand why, or other ways to fix it.
- (void)onComboBoxSelection:(id)sender
{
if(![sender isHidden]{
NSLog(#"Now only fires once");
//My code doesn't actually set hidden here, it's just for proof while debugging the issue.
[comboBox setHidden:YES];
}
}
Set a breakpoint in onComboBoxSelection: and look at the backtrace when it's called the second time (type bt in the debugger to see the backtrace). That will explain what's going.
A combo box is both a text field and a popup, and it will fire actions for both. The text field action is fired when editing ends, either by hitting the Return key or when it resigns first responder (e.g., tabbing out to another field).
When you hide the combo box, the text field resigns first responder and fires its action.
What you probably want to do is check if combo box value has actually changed, and only then proceed with hiding the combo box, etc.
Another option is to use data bindings to observe changes to the combo box. Bind the combo box value to a property on your controller. Then implement the property setter in your controller.
try this [comboBox setHidden:1];

Check if spacebar is pressed inside a NSTextView

I have a NSTextView where I need to check when a specific button is pressed. Using ModifierKeyFlags I can check if buttons like shift and control is pressed, but I need to check if SPACE is pressed. Therefore, I cannot use the ModifierFlags (cause it can check some buttons, excluding the spacebar)
So I need a check, that notifies me everytime I press the spacebar in the textfield in my application. Any thoughts? I think it needs to be something like this:
if(spacebar is pressed) {
Dlog(#"give me a notification");
}
Use NSText's delegate.
-(void)textDidChange:(NSNotification *)notification
{
//Check if it has added a spacebar
}