I have a bug in an app I'm working on regarding a UISearchBar.
Using the code:
int width = 250;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(768 - width - indent, companySelectionButton.frame.origin.y + (companySelectionButton.frame.size.height - 44)/2, width, 44)];
[searchBar setAutoresizingMask: UIViewAutoresizingFlexibleLeftMargin];
[searchBar setPlaceholder:[Globals localisedString:#"Search by Code or Name"]];
[searchBar setAutocorrectionType:UITextAutocorrectionTypeNo];
[searchBar setDelegate:self];
[self.view addSubview:searchBar];
[searchBar release];
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
The search bar is in the position I want in both orientations, but it won't respond to taps in landscape and become first responder/show the keyboard.
I've tried commenting out the following lines to see if it helps but it doesn't fix it:
[searchBar setAutoresizingMask: UIViewAutoresizingFlexibleLeftMargin];
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
Anyone else had a similar issue?
Thanks in advance :)
EDIT
After commenting out [searchBar setDelegate:nil]; the search bar is able to become first responder in all orientations. But of course I need a delegate to be able to use the searchbar! Any ideas??? Will continue to have a play, perhaps I'm missing some delegate method?
Redrawing the frame of the searchbar in willRotateToInterfaceOrientation fixes the issue.
Sorry everyone, I discovered that I was accidentally doing something strange in my searchBarTextDidBeginEditing delegate method that resigned the searchbar as first responder if the device was in landscape.
This question is of no use to any one, should I delete it?
Related
I need a help . Actually when I tap or click on search button then I should get a black screen . As you could see using in ABPeoplePicker.So it is possible with UITableView ? I've added an image that can help you in getting my question.
here is my code :-
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
if(searching)
return;
[searchBar setShowsCancelButton:YES animated:YES];
searching = YES;
letUserSelectRow = NO;
[contactTableView setScrollEnabled:NO];
}
![Example image][1]
If you add a UISearchDisplayController instead of a UISearchBar, the black screen will come automatically. If you need it in some other places than UISearchBar, you have to add view with black background color and show/hide it when needed.
I am trying to hide a keyboard and show up a uipicker instead for textfield.
But it seems like I am in the wrong direction or there's something wrong with my coding as
the keyboard still persistently show up (uipicker can be seen after another view is bring forward). I supposed there is nothing wrong with my uipicker methods as the uipicker is showing correctly just that the keyboard is still blocking it. Can anyone point out which part of the code went wrong or guide me to the right direction?
Here are my codes in the textFieldDidBeginEditing method:
[textField resignFirstResponder];
[pickerView setHidden:NO];
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 180, 300, 180)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
Please let me know if there's is any need for more codes.
Thanks
The easiest way to do this is to set the picker view as the text field's input view:
textField.inputView = pickerView;
I have a UIViewController with UISearchBar (and SearchDisplayController) along with a UITableView. When navigating to this view controller, I want to auto-focus on the UISearchBar (bring up the keyboard with focus on the text field in the search bar). Everything says to use
[searchBar becomeFirstResponder]
(assuming searchBar is an outlet to the UISearchBar)
I put this at the end of viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL did = [searchBar becomeFirstResponder];
[searchBar setText:#"donkey"];
}
Variable did is 0 (and focus doesn't happen), but the search bar's text is successfully changed to donkey.
What am I doing wrong?
I'm using iOS 5 with ARC and latest Xcode (4.3.2).
Got it working. Just had to put it in viewDidAppear instead of viewDidLoad or viewWillAppear.
add your code to -(void)viewDidAppear;
This worked for me:
[window makeKeyAndVisible]
we must ensure the METHOD becomeFirstResponder be performed on mainThread
so make it like :
[xxx performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil waitUntilDone:NO];
I have a problem with switching views in an iPhone application.
I have the source code of "Beginning iPhone 3 Development" (http://books.google.com/books?id=TcP2bgESYfgC&printsec=frontcover&dq=beginning+iphone+3+development#v=onepage&q=beginning%20iphone%203%20development&f=false) - chapter 6 - Multiview Applications.
Now I have the problem, I want to create a new view which should switch by clicking the button on the blue screen "Press me". But it did not work.
I add the these lines to the IBAction that the button on the blue screen is pressed:
StartViewController *startController = [[StartViewController alloc] initWithNibName:#"StartViewController" bundle:nil];
self.startViewController = startController;
[self.view insertSubview:startController.view atIndex:1];
[startController release];
But the toolbar at the bottom won't disappear. But I want that this toolbar disappear.
If I wrote
[self.view insertSubview:startController.view atIndex:0];
instead of
[self.view insertSubview:startController.view atIndex:1];
the new xib lies behind the old one, so I see both views, the old and the new.
Why? I do not understand this.
Thanks a lot in advance & Best Regards Tim
The toolbar is in the SwitchView so you would need to hide it from the view if you want it to hide. You could make an IBOutlet for the toolbar and then call setHidden:(BOOL) to hide it. You will need to do this from BlueViewController so you will need a way to get to your super view (which is SwitchView). You will also need to remove the BlueView from the super view by calling removeFromSuperView on blueViewController before inserting the new view into place. It is basically the same code that comes from the switch button in SwitchViewController.
Update:
I looked at your code. In BlueViewController.m use this for blueButtonPressed:(id)sender
StartViewController *start = [[StartViewController alloc] initWithNibName:#"StartViewController" bundle:nil];
self.startViewController = start;
[start release];
View_SwitcherAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
SwitchViewController *switchController = appDelegate.switchViewController;
switchController.theToolbar.hidden = YES;
[self.view removeFromSuperview];
[self.view insertSubview:startViewController.view atIndex:0];
You will also need to add these two imports for "View_SwitcherAppDelegate.h" and "SwitchViewController.h".
I'm using an tableView with custom cells. When I want to display another view using the pushViewController function of the navigationController I loop through the textfields and call resignFirstResponder on them. But resignFirstResponder does only work when the textfields are being displayed so I scroll first to the top of the page. This is the code:
NSIndexPath *topIndexPath;
topIndexPath = [[NSIndexPath indexPathWithIndex:0] indexPathByAddingIndex:0];
[self.tableView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
[[self textFieldForRow:0] resignFirstResponder];
[[self textFieldForRow:1] resignFirstResponder];
[[self textFieldForRow:2] resignFirstResponder];
[[self textFieldForRow:3] resignFirstResponder];
This works, but after this my tableView has some weird problem with its origin. I tried to set it's superviews origin to 0, but that doesn't help.
Here is a screenshot of the problem: link
As you can see, my tableview is too large and the scrollbar is stuck in the middle of the view when reaching the bottom.
Sorry for my english, I hope that you can understand me,
Thanks in advance!
Hans
It was actually quite simple. Just put your resignFirstResponder in -viewWillDisappear
edit: this is better and less hacky, I added this to my class, and it worked:
edit 2: seems that your app will be rejected when using the previous code. Here is a updated public api version:
- (void)viewWillDisappear:(BOOL)animated
{
[self.view findAndResignFirstResponder];
}
And:
#implementation UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResponder
{
if (self.isFirstResponder) {
[self resignFirstResponder];
return YES;
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResponder])
return YES;
}
return NO;
}
#end
(source: Get the current first responder without using a private API)
I would fix your other problem. I imagine when you say you can't call "resignFirstResponder" when the other textFields are on screen, you mean that there is a crash?
If so, it is because of screen cells don't exist and therefore the textfields are gone as well. They are recycled (so they can be dequeued for new cells).
The easy solution is to only call resignFirstResponder only on textFields that ARE on screen.
What you are doing now seems a little hacky.