Two buttons, one click - objective-c

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

Related

UISegmentedControl determine which button is tapped when momentary = YES

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.

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.

Pass button Selected state to another view Controller

i have Two View Controllers. 1st controller have 1 button. i use
button.selected = YES;
i'm using If statement to do some custom functions if the button is selected state.
-(IBAction)play:(id)sender;
{
if (button.selected)
{
custom code
}
}
when i use this button in same view controller with play button it works. but i want to move the button to another view controller. but it's not working.
question is how to pass button select value to the other view controller?
I don't know exactly what you're trying to achieve. Do you just dragged a button from one interface builder window to another and wonder why that button doesn't trigger 'play:' in VC 2 or do you want to let a second view controller know about the state of a button that is a subview of view controller one?
1) If you just dragged the button from one IB window to another check the connections again. That's like the most usual problem here.
2) If you just wanna let VC 2 know about the state of the button that's currenty on VC 1 one way would be adding a BOOL to VC 2 that represents that button's state. Whenever the button get's triggered it is responsible for updating that BOOL.

Delay appears when I am trying to register double click event on NSTableView

I have NSTableView and NSView inside of this table using as cell. This NSView has 3 NSTextFields.
When I'm trying to select one of this NSTextFields they respond really quickly. But if I add some code to register double click event on NSTableView, delay appears. So when I click on textfield it thinks half a second and then activates it. But there is no such delay without double click event registered.
My code:
-(void)awakeFromNib{
[tasksList setDoubleAction:#selector(doubleClickInTable:)];
[tasksList setTarget:self];
}
-(void)doubleClickInTable:(id)sender
{
//Don't matter, it can be empty.
}

I cannot hide a textfield and label in my iOS app

here is the low down:
-(IBAction)button1click:(id)sender;
{
label1.hidden=YES;
textfield1.hidden=YES;
label2.hidden=NO;
textfield2.hidden=NO;
-(IBAction)button2click:(id)sender;
{
label1.hidden=NO;
textfield1.hidden=NO;
label2.hidden=YES;
textfield2.hidden=YES;
the is issue is that when i first open my screen all 4 labels are visible. By default button1 radio is checked but label2 and textfield 2 are visible when they shouldnt be. if i press button1 even though it is already selected the items with hide and then all is good. My issue is having them hidden when the screen first opens up.
Thanx all for you help
You can, in your viewDidLoad method:
-(void) viewDidLoad
{
[super viewDidLoad];
[self button1click:nil]; //nil or the instance of button1 if you need it
}
In this way, you will execute the same code when you press button1 without duplicate your code.
You can take one of two approaches to hide the label.
a) in Interface builder you can click the check box for hidden in the attributes inspector. If you do that the default behavior will always be hidden when the app launches then you can make it visible in code like your example shows
b) add your existing code to hide the label to your view controllers - (void)viewDidLoad method.
both methods work equally well.
When you create that objects you can set foo.isHidden = YES