Showing UISearchBar "X" within text field vs. adjacent Cancel button - iphone-sdk-3.0

I know I can set showsCancelButton to NO for a UISearchBar ... until you tap the search bar text field, and then the cancel button appears anyway! (At least it does for me.)
Is there a way to show the "X" in a circle within the UISearchBar text field vs. having that separate Cancel button show up adjacent to it? Note that this button only appears for me when search mode is active, and this is regardless of the setting for showsCancelButton.

try this
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar's cancel button while in edit mode
mySearchBar.showsCancelButton = NO;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
mySearchBar.showsCancelButton = NO;
}
when using this cancel button will not shown.

for (UIView *searchBarSubview in [searchBar subviews]) {
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)searchBarSubview setClearsOnBeginEditing:YES];
}
}
to enable the cancelButton try searchBar.showsCancelButton = YES;
Try the above lines of code.

if you use interface builder it is ery easy to show.it is there by default.i have used search bar in my app and the searchbartextfield has an "X" to clear the textfield

It might be that the "X" is only intended to clear the search field and not to cancel the search, and so the Cancel button must remain once a search is in play, regardless of showsCancelButton.

Related

iOS7:editable textfield cells in tableview is not working properly

Editable textfield cells Tableview is causing problem on keyboard tab button everytime it is calling textfieldshouldbeginediting even if i am in first textfield it is not going to nextfield.
It is going to last textfield and if popover is availabe it will crash.How can i fix this so that if enter tab then it has to resign current responder in textfielddidendediting and it should not go to textfieldshouldbegin editing.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
PickerViewController *selectOperatorController;
NSLog(#"tag %d",textField.tag);
return NO;
}
I also declared textfield delegates like didendediting and shouldendediting
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
activeField = nil;
if (self.chooseOperatorController) {
[self.chooseOperatorController dismissPopoverAnimated:YES];
}
return YES;
}
This is not as issue in iOS 6.But it is in iOS 7.
textfieldshouldbegin editing will not allow desktop keyboard tab button input,it cannot judge to move between the textfields.If we add textfielddidbeginediting we can move the controls,eventhough we have two methods we can move by using keyboard tab.So textfielddidbeginediting is mandatory if we want to move bewtween available textfields.

Hiding a button Obj-C

I'm looking to hide a button on my i-phone app and then by clicking another button it will appear. I've managed to make the button disappear with a click but can't figure out the opposite. I'm also new to Objective-C as you can probably tell so any tips on improving my code would be helpful. Thanks!
.h :
#property(nonatomic,retain) IBOutlet UIButton* button1 ;
-(IBAction)buttonTouch:(id)sender ;
.m :
#synthesize button1=_button1;
-(BOOL)hideOutlets {
_button1.hidden=TRUE;
}
-(void)buttonTouch:(id)sender {
_button1.hidden = !_button1.hidden;
}
Well to start from scratch, if you want to hide a button set its property hidden to YES,
else if you want to make it reappear then set the property to NO.
Example:
button1.hidden=YES;
button1.hidden=NO;
Your code is basically correct
-(void)buttonTouch:(id)sender {
_button1.hidden = !_button1.hidden;
}
This code will hide your button when it's shown and show it when it's hidden. This should be correct.
You are saying
then by clicking another button it will appear
Are you sure both buttons have the touch-up-inside event properly connected to this action? I guess your problem will be that the buttonTouch: is not called when you touch the other button.
#synthesize button1=_button1;
-(BOOL)hideOutlets {
_button1.hidden=TRUE;
}
-(void)buttonTouch:(id)sender {
_button1.hidden = FALSE; //Or "NO" or "0", it's a boolean
}
In addition, its weird setting a button hidden with a BOOL. If you want to have them hidden on load, go put _button1.hidden = YES; if you want it to hide it only when you have it visible, use
-(void)buttonTouch:(id)sender {
if(_button1.hidden == YES)
{
_button1.hidden = NO;
}
else { _button1.hidden = YES; }
}
I'll try to answer the question correctly as I understand it.
2 buttons, button1 and button2. Pressing button1 hides itself and shows button2. Pressing button2 hides itself and shows button1 again.
-(IBAction)button1Pressed:(id)sender {
// button1 can only be pressed when not hidden, so we can dispense with checks for hidden
[button1 setHidden:YES];
[button2 setHidden:NO]; // assuming this button was hidden at startup
}
-(IBAction)button2Pressed:(id)sender {
// button2 can only be pressed when not hidden, so no need to check for hidden
[button2 setHidden:YES];
[button1 setHidden:NO];
}
This should allow you to flip back and forth between buttons having them hide/show opposite of each other.
Two obvious problems with the code presented.
1) Cocoa uses YES and NO for boolean values not TRUE and FALSE.
2) You've declared a property, so you should use it in preference to the synthesised instance variable.
3) You're button touch method should return IBAction in the implementation as well as the interface.
Don't know if that'll fix your problem, but it's the first step to fix up your code.
#synthesize button1=_button1;
-(BOOL)hideOutlets {
self.button1.hidden=YES;
}
-(IBAction)buttonTouch:(id)sender {
self.button1.hidden = !self.button1.hidden;
}

How to reset seachDisplayController View to original state programatically

I have the UISearchDisplayController page view as follows (original state)
After clicking on the searchbar and typing some search term, I get a list of results in the table
After clicking on an entry in the table, a new viewController is displayed on top of the stack
My intention is that on the click of the cancel button (on top left), the root view will display the original state of the searchDisplay controller
This is what I have tried
In my cancel method (triggered when I click on the top left hand cancel button)
- (void)cancel
{
[self dismissViewControllerAnimated:YES completion:NULL];
[self searchBarCancelButtonClicked:self.searchDisplayController.searchBar];
}
And in the searchbar delegate method I did this
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
self.searchBar.showsCancelButton = NO;
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
searchDisplayController.searchResultsTableView.hidden = YES;
[self searchDisplayControllerWillEndSearch:self.searchDisplayController];
}
All I manage to achieve after clicking on the cancel button is this
The original tableview seems to be missing (appears only if I click on the page)
How can I modify my methods to revert the search to its' original state?
Try [searchDisplayController setActive:NO animated:NO]
This should hide the search table.

Moving From One textfield to Another during Next Button click in Virtual Keyboard

So i have Two Text Box , lets say , TextBox1 and TwoBox2 .
On Viewdidload TextBox1 i am using
[self.TextBox1 becomeFirstResponder];
Now how to move the Cursor to TextBox2 ,when i press Next Button in Virtual Keyboard
In Cocoa for Mac OS X you have the next responder chain, where you can ask the text field what control should have focus next. This is what makes tabbing between text fields work. But since iPhone do not have a key board, only touch, this concept has not survived the transition to Cocoa Touch.
This can be easily done anyway, with two assumptions:
All "tabbable" UITextFields are on the same parent view.
Their "tab-order" is defined by the tag property.
Assuming this you can override textFieldShouldReturn: as this:
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
Add some more code, and the assumptions can be ignored as well.
In the callback for Next Button, use,
[self.TextBox2 becomeFirstResponder];
Easy!

How to dismiss keyboard when the UISearch Bar 'Search" button is pressed?

I've implemented a UISearch bar with the searching functionality set up, however when I press the "Search" button on the keyboard that shows up nothing happens. How can I get the keyboard to hide when the "Search" button is pressed while keeping the text in the search bar intact (To keep the search results present)?
From Text, Web and Editing Programming Guide for iOS:
To dismiss the keyboard, you call the resignFirstResponder method of the text-based view that is currently the first responder.
So you should do this in your UISearchBarDelegate:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
Swift 4
Make sure you have UISearchBarDelegate defined in your UIViewController
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}