how can i give multiple actions to button on iPhone - uibutton

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

Related

Detect when Today Widget "Show More" is tapped or is expanded?

Is it possible to detect when the Today widget is expanded/contracted or 'Show More' button is tapped in Objective C?
There is no way to detect whether the Show More button has been tapped in Today Extension.
What you can do is determine if it's open with checking frame height.
You can add a boolean and set it in :
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
if (activeDisplayMode == NCWidgetDisplayModeCompact) {
//non expanded
} else {
//expanded
}
}
You can also have a layout that doesn't need to know if it's opened or closed, that's what most apps do.
Hope this helps :)

replacement for tabbar selected index

I have an old app where I have 5 tabs. Each tab have list of ads & there is details. Also each tab have respective add ad. Design for all 5 tabs is same except some changes, so I decided to use 1 screen only for all 5 tabs.
Now what I am doing is while add ad, I am checking which tab I am and based on tab bar index, I am showing hiding fields. Same is applicable for details screen also. Sample code is as shown below.
if (self.tabBarController.selectedIndex==0) {
field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==1) {
field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==2) {
field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==3) {
field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==4) {
field1.hidden = YES;
}
Now I have around 15 fields for each form so I write this code for all fields.
What I noticed that client change the tab bar position continuously so I was looking for efficient way of doing it.
Right now what I do is at all places manually change index positions by doing Search-Replace, however I dont like this.
I was looking for a way where instead of selectedIndex, I was looking for someconstant value that I will assign to tab item of tab bar, so my code will change as below
if (self.tabBarController.selectedIndex==adType_News) {
field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==adType_Occasions) {
.
.
And so on...
This way, I only need to change in the storyboard and code level will not have any changes.
Is there way where I can achieve this?
Main Issue
As client ask to change tab bar completely, I need to make changes of selectedIndex changes at all places which I don't like and feel more in-efficient way. So I was looking for a way where I will make change in storyboard only and coding level there won't be any change.
The main issue for me is changing selectedIndex in all files which make more error.
I think I understand the question to mean that the code is full of number literals, referring to the selected index of the tabbar, and the business requirements about the ordering of items are shifting. Fix by changing the literals to something symbolic that can be controlled centrally:
// in a new file, indexes.h
#define INDEXA (0)
#define INDEXB (1)
#define INDEXC (2)
// wherever existing code refers to the tab selection
#import "indexes.h"
// in the code, for example if selectedIndex == 2, change to
if (self.tabBarController.selectedIndex==INDEXC) {
// ...

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

Cocoa Objective C - How to end editing of NSTextField when user presses Enter key or clicks out side text box

I have created two cocoa controls on my GUI. NSButton and NSTextField. When user clicks on the button, button handler will be executed. It will make button disable and NSTextField enable. On end editing, the string of text field will be shown as a button title.
button click
{
- Make button visible = false;
- Make text field visible = true;
- Text field allow selection = true;
- text field allow editing = true;
}
control text end editing
{
- text field allow selection = false;
- text field allow editing = false;
- Set button title as text field string;
- Set text field visible = false;
- Set button visible = true;
}
I have written above two functions to achieve the functionality. I am getting required functionality when user presses Tab key, as the view switches to next view. But, I want that, once user enters required text in the text field, he can press Enter or click outside of text field to end editing.
On end editing, text field should be closed and text written should be displayed as a title of button.
Please let know, how this functionality can be achieved. Any help would be appreciated.
Thanks in advance...
I'd make an object the delegate of your NSTextField and then implement the delegate method:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
- text field allow selection = false;
- text field allow editing = false;
- Set button title as text field string;
- Set text field visible = false;
- Set button visible = true;
}
I believe this captures both the case where you hit enter and where you tab out.
When user presses Enter key:
Implement NSTextFieldDelegate and attach to the text field.
Implement the delegate method:
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
if (commandSelector == #selector(insertNewline:)) {// #selector(cancelOperation:) for esc clicked
[self.view.window makeFirstResponder:nil];
}
return NO;
}
When user click outside of the box:
Check my another answer for how to end editing when clicks outside of the box.