iPhone - Change UISearchDisplayController "Cancel" button text - iphone

Sort question:
Is there a way to change the "Cancel" button text within a UISearchDisplayController to "Done" or "Close"?
Rationale:
I have a UITableView that contains a list of options, each option can be checked or unchecked.
I want to enable search through these options, so I've created and added a UISearchDisplayController.
The user will search for an item, and then perform actions on the items in the search results (i.e. will select/unselect certain items).
Once this is completed the user will then go back to the previous (unsearched) list of options.
The problem is that the only way to dismiss the UISearchDisplayController is to press the "Cancel" button. However "Cancel" is the wrong word here as the actions conducted while within the Search bar will be stored.
Therefore, is there a way to change the "Cancel" button text to "Done" or "Close"?

I found one way to do this, in the delegate for the searchbar, create an empty animation then after a small duration, change your things on the button, this might be because the cancel button is animated in sometime shortly after the delegate is called, thus you cant grab it and change it before that:
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController*)controller {
[UIView
animateWithDuration:0.05 animations:^{} completion:^(BOOL finished){
for (UIView *possibleButton in controller.searchBar.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
//TODO:
// do things with button here...
}
}
}
];
}

Related

How to call numpad using action button not from text field

I know this is silly question but as a beginner at xcode, and i can't find the answer from any source, can i call numpad (numberic keyboard) from my action button? I just want my action button can call numpad like text field do. what code should i insert in my action button?
my code:
- (IBAction)setLoopBtn:(id)sender
{
}
Thank you again for sharing your knowledge to noob like me.
Create a UITextField with a frame that puts it off the visible screen. Assign the type of keyboard you want to display as the default for that field. When the button is tapped, send becomeFirstResponder to your off-screen field.
-(IBAction) ButtonClick
{
txtField.keyboardType = UIKeyboardTypeNumberPad;
[txtField becomeFirstResponder];
}

Working With Stacked UIButtons

Hey guys, so I have this search bar in my view along with many buttons below it. So when the search bar is tapped, a keyboard pops up, however I want the user to be able to click anywhere below the search bar and above the keyboard to get out of searching mode. I have been scavenging SO for a bit and came across a solution that suggested that I create an invisible button which intercepts touch events which I can use to resign first responder status from the search bar. And I can merely hide/disable the button when I do not need it so that the buttons below it may be tapped right? Wrong. setHidden nor setEnabled: aren't doing the trick. Here is the relevant code:
//touch event on button outsideSearchBarButton which is invisible
- (IBAction)selectOutsideSearchBar:(id)sender {
NSLog(#"Selected outside search bar");
[searchBar resignFirstResponder];
[outsideSearchBarButton setEnabled:NO];
[outsideSearchBarButton setHidden:YES];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
NSLog(#"searchbarTextDidBeginEditing");
[outsideSearchBarButton setHidden:NO];
[outsideSearchBarButton setEnabled:YES];
}
Setting the button to disabled or hidden via nib file does not help me at all. Either the button never exists (I can't click between search bar and keyboard and have keyboard go away) or always exists (I can't click any of the buttons below this invisible button). Any help appreciated. Thanks in advance!
//touch event on button outsideSearchBarButton which is invisible
- (IBAction)selectOutsideSearchBar:(id)sender {
NSLog(#"Selected outside search bar");
[searchBar resignFirstResponder];
outsideSearchBarButton.userInteractionEnabled = NO;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
NSLog(#"searchbarTextDidBeginEditing");
outsideSearchBarButton.userInteractionEnabled = YES;
}
Do this.
go to the interface builder and select the button on which you are removing the keyboard during search, then select layout from the above menu and select send to back.Then in the inspector window set the button style to custom.
Hope this helps you.......

How to open a view with a button clicked as default

In my app i want to open a view with the content of a particular button (so that button should look clicked and should be not clickable). I have 4 button with pictures and all the four have different content inside them (Table view with different content).When this view gets open i want the first button clicked automatically and the content of that button should get displayed and by clicking any other button the content of that button should get displayed and the previous clicked button should be available to click again.
I am using different pictures for clicked and unclicked button.
Thanks,
Maybe this will help you
- (void)didClickButton:(id)sender {
UIButton *optionButton = (UIButton *)sender;
if(lastSelectedButton.tag!= optionButton.tag) {
optionButton.selected = YES;
//According to your needs enable or disable the button's interaction
}
Here lastSelectedButton should be an instance variable.
What you're describing sounds like a segmented control. Essentially the segmented control works like buttons on a tape recorder (dating myself, I know.) When you press Play, it stays down and can't be pressed again until you press Stop or FF or Rew, etc. (Ok, Stop doesn't really work that way, but the rest of the buttons do)
Unfortunately, I don't believe you can use your own images in a UISegmentedControl, but fortunately there's an open-source version that should work for you: https://github.com/xhan/PlutoLand/blob/master/PLSegmentView.h
Once you have the control in place you can change the content of your main view depending on the value of the segmented control. You can handle that in the UIControlEventValueChanged event
Keep a single selector for all the buttons something like
[btn addTarget:self action:#selector(templateSelected:) forControlEvents:UIControlEventTouchUpInside];
and make use of the tag to carry any index to the selector
[btn setTag:integer];
and if you want to keep track of previously clicked button then keep a global (id) and assign the current button address to the that id.
And if you want the first button to be clicked on load then call the function melodramatically during initialization of the first button.
[self templateSelected:firstButton];

UIButton delayed state change

I have a UIButton subview inside of a UITableViewCell.
When this button is touched, the user must hold the button for about a half second for the button's image to change to the UIControlStateHighlighted image.
This means that if the user just taps the button as is usually the case, the highlighted state is never shown.
Why does this occur and how can I fix it?
I just encountered this problem and saw that this issue hadn't been closed. After screwing around for a while I found a fix for it.
Now you can fix this by turning off delaysContentTouches or unchecking the "Delays content touches" box on the tableview.
The only negative side effect is that the user won't be able to tap down on a button and initiate a scrolling gesture. However, if the user tries to scroll starting from anywhere that doesn't itself accept touches, the behavior should be the same as before.
The problem is that your UIButton is inside a UITableView. This means that the table view has to determine whether your tap is going to be a swipe or if it's just a tap intended for the button. The table view has to delay sending a message to the UIButton until it knows that the user doesn't intend to swipe and therefore scroll the view instead of pressing the button.
If you don't need a table view, get rid of the UITableView.
Up for David Hodge's answer.
I just want to add a way to remove that "only negative side effect", already described by David: if you start scrolling inside a UIcontrol in a UIScrollView with delayContentTouches=NO, scrolling doesn't work.
SOLUTION
Subclass UIScrollView (or UITableView as the original question) and override:
-(BOOL) touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
Your UIControls inside UIScrollView/UITableView will change their state immediately on tap and the scrollviews will be able to scroll even if the touch starts on some UIControl. Works like a charm.
I just change the image from within the target action method:
[sender setBackgroundImage:[UIImage imageNamed:#"highlighted-image.png"] forState:UIControlStateNormal];
It changes the background image instantly.
Edit: completely re-written following a misunderstanding of the question
One way of thinking of a UIButton is as a shorthand way of setting up an area of the screen that can respond to various instantaneous touch events the response it makes is defined by UIControl's Target-Action system for delivering messages to other objects.
UIControlEventTouchDown sounds like the one you need to respond to. It will be triggered as soon as someone touches inside your button - this is what the "Contact Info" button in SMS does.
UIButton* myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// SEt up title, frame etc
[myButton addTarget:self action:#selector(myButtonWasPressed) forControlEvents: UIControlEventTouchDown];
[myMainView addSubView:myButton];
Will send a -(void)myButtonWasPressed message to the object this code runs from (ideally you view controller). In myButtonWasPressed you can then add a new view or take any action you like. The SMS app pushes a view controller to display the contact info using a navigation controller.
If this still doesn't solve your problem, you're going to have to post some code in order to get more insight into what's going wrong.

iPhone: is there another way of yielding First Responder status?

Problem: If you have a big form with a lot of text input fields with number pad, you would not want to tell every single text field something like
[myTextField1 resignFirstResponder];
[myTextField2 resignFirstResponder];
instead, it would be great to just tell for example an invisible background button, that it is the First Responder as soon as the user tabs outside of any text field.
Like in JavaScript, when you give an element the focus(), all others lose it. How can I do that in UIKit?
[button becomeFirstResponder];
In your header file, paste this: -(IBAction)backgroundClicked:(id)sender;
in your implementation file, paste this:
(IBAction) backgroundClicked:(id)sender
{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
In InterfaceBuilder, create a button that covers the entire view.
with the button selected, click Layout > Send to Back
Control drag from the button to File's Owner and select the outlet called: backgroundClicked.