I have a simple view controller with a uisearchbar and a uitable.
My problem is that when search bar is tapped I see delegate function searchBarShouldBeginEditing being called but not searchBarTextDidBeginEditing(and because of that keyboard is not opened and search is not editable)
I tried to implement delegate function searchBarShouldBeginEditing returning YES, set searchbar as first responder, but no way I get searchBarTextDidBeginEditing called...
Any idea what could be happening??
Some code:
controller.h
#interface ViewController : UIViewController <UISearchBarDelegate>
{
UISearchBar * searchbar;
}
#property (nonatomic, retain) IBOutlet UISearchBar* searchbar;
#end
controller.m
#synthesize searchbar;
- (BOOL)respondsToSelector:(SEL)sel {
NSLog(#"Queried about %#", NSStringFromSelector(sel));
return [super respondsToSelector:sel];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
NSLog(#"searchBarShouldBeginEditing -Are we getting here??");
return YES;
}
-(void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
NSLog(#"searchBarTextDidBeginEditing -Are we getting here??");
}
Of cousrse my class have plenty more code (that surely is affecting somehow searchbar) but if someone has got similar problems with searchbar it would be very apreciated its response ;)
I tryed to make simple application with only searchbar and obviously it works...
EDITING:
Testing a little bit I discovered that it is not something related with uisearchbar as I added a TextField getting same result (just textFieldShouldStartEditing delegate function being called)
Application has all view controllers inside a UITabBar cotroller, but I do not think this can cause all this mess...
EDITING2:
Really strange behaviour: Setting IBAction function to TouchDown event of a UITextfield works perfectly but setting IBAction function to EditingDidBegin never gets fired...
Why this event could not be called??
did you set the delegate property?
searchbar.delegate = self;
Maybe you somewhere call [searchbar resignFirstResponder]. It was the case in my similar problem.
I had this problem when I was converting from a standalone UISearchBar to a searchBar integrated into the UINavigationBar.
In converting my code, I was still allocating the UISearchBar and setting the delegate for that. What I should have been doing was configuring the searchBar that comes as part of of the UISearchController, and setting the delegate of that,
self.searchController.searchBar.delegate = self;
Once I did that, my delegate methods started firing!
Related
The question: How do I prevent the copy/paste/select popup that occurs over a UITextView from appearing (not using UIwebView and css)?
I did not want to go the rout of UIWebView as some posts have gone because I already am using UIViews with UITextFields for data entry. I had tried unsuccessfully to implement the solutions dealing with UITextField in my implementation file of my view controller with the methods: targetForAction:withSender, setMenuVisible:animated and finally canPerformAction:withSender. (It NO WORKY WORKY - [sad face])
Ok, I found a working solution (in Xcode 5.1) to my question which, in short, is subclassing the UITextField.
I realized I wasn't really overriding the default behavior of the UITextField in the view controller like I wanted to and neither was putting the methods listed here override the behavior of the textfield delegate in the view controller file. The Key was to subclass the UITextField itself with -targetForAction:withSender. (I know some of you are screaming at the screen about how OBVIOUS that was!) It was not obvious to me. Like most problems when first figuring them out I went through a lot of different paths some I found here in SO. But the solution is a simple one. I want to share this solution in its own area so hopefully it can help someone out.
The header file:
//
//
#import <UIKit/UIKit.h>
#interface TPTextField : UITextField
- (id)targetForAction:(SEL)action withSender:(id)sender;
#end
and the implementation file (.m)
//
//
#import "TPTextField.h"
#implementation TPTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#pragma mark - method overrides - deny user copy/paste on UITTextFields
- (id)targetForAction:(SEL)action withSender:(id)sender
{
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (action == #selector(selectAll:) || action == #selector(paste:) ||action == #selector(copy:) || action == #selector(cut:)) {
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return nil;
}
return [super targetForAction:action withSender:sender];
}
#end
In your storyboard or nib/xib file just connect this class to your UITextfield like the picture below:
I have it on git to for easy access here. Please let me know if this is helpful to you!
Tony
If the UITextView is created as an object on a storyboard, the solution is even easier. In Attributes Inspector for the UITextView object, under Behavior, uncheck Editable and uncheck Selectable. Under the Scroll View section, you can check Scrolling Enabled if you want the user to be able to scroll text.
I have a UITableView that i would like to hide until the user taps the button searchButtonTapped. (I'm also using this button as an IBAction.)
Originally i'm hiding the table view as you see in the viewDidLoad, and i wanna show it after the button was tapped, but it does not shown up after i tap the search button. Do i missed something? For me, it seems it should be work properly, after the button was tapped i refresh the table view.
my .h file
#property (weak, nonatomic) IBOutlet UIButton *searchButtonTapped;
- (IBAction)searchButton:(id)sender;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.hidden = YES;
}
- (void)buttonTapped:(id)sender {
if (sender == self.searchButtonTapped) {
self.tableView.hidden = NO;
[self.tableView reloadData];
}
}
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
It's impossible to tell from the little bit of code that you posted. Add NSLog statements in your buttonTapped method that show entering the method, entering the if statement, the value of searchButtonTapped, and the value of self.tableView.
Then you can tell if the method is getting called, if the if statement is evaluating as true, and if the table view is non-nil. One of those things is likely to be the cause of your problem.
I'm guessing that the if statement is wrong. what type is the property self.searchButtonTapped? Post the code that declares that property.
Based on the name I would guess that searchButtonTapped is a boolean?
you have declared only one IBAction, which is for the method searchButton.
This method call the searchSetup´s method. What is the purpose of it?
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
So you must have another IBAction for buttonTapped method witch is currently a "void" method and not a IBAction. Or you make that connection from the storyBoard, or you must declare it programaticly like:
[self.searchButtonTapped addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]
I'm using an UISearchBar in my app. When I run on simulator everything is fine. On device does not work. On device, my searchbar frame is changing but keybord doesn't show. NSlogs are working. My code is here:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"wrote");
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self.mySearchBar setFrame:CGRectMake(0,100,mySearchBar.frame.size.width,mySearchBar.frame.size.height)];
mySearchBar.showsCancelButton=TRUE;
mySearchBar.text=#"nowwrite";
NSLog(#"chosed");
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(#"cancel");
}
- (void)viewDidLoad
{
mySearchBar.delegate=self;
[super viewDidLoad];
}
and my .h file
#interface sehirRehberi : UIViewController <UISearchBarDelegate,MKMapViewDelegate,CLLocationManagerDelegate,UINavigationControllerDelegate> {
IBOutlet UISearchBar *mySearchBar;
}
#property(retain, nonatomic) UISearchBar *mySearchBar;
Have you made the view first responder? That's necessary for the keyboard to show. I'm not sure why it's working on the simulator, but you should call [self becomeFirstResponder]; in your viewDidLoad method to enable the view to receive keyboard events and thus show the keyboard. When you're done (viewWillDisappear, etc.), just call [self resignFirstResponder];. I think that's your problem. It's hard to tell without more explanation/code.
I had the similar problem that 'UISearchBar' appeared in Simulator but not in actual device. I fixed the issue by calling Constructor with NibName as I placed the UISearchBar in nib file.
[MyUITableViewController initWithNibName:#"MyUITableViewController" bundle:nil];
I've just started with xcode and objective-c and did some very basic apps, but what i'm having problem with is very basic this. the keyboard return button not hiding the keyboard.
I've searched the internet for the solution and all they say is to connect delegate to the file's owner and add the function and it should work, i did that and nothing is working.
I have an ok button and it is working and also clicking on any free space on the screen is working, just the return button....
I am using the simulator, not testing on iphone yet. (xcode 3.2.5 64 bit with the 4.2 simulator).
This is the line of code that should connect the delegate to every textFiled.
1. i've tried already to return both YES and NO, didn't work.
2. i've tried both a specific object name for the textField and this general way, didn't work.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
In the: basic view controller connection -> connections -> outlets, i have the: delegate -- File's Owner. and in the file's owner in referencing outlets there is: delegate - Round style text.....
EDIT - i forgot to mention before, i've check and the method isn't being called!!!
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(#"Working!!!");
[textField resignFirstResponder];
return YES;
}
what should i do to make it happen? that is why people say to connect the delegate, but in my case it is connected and not triggering the function...i know it is kind of dumb question but for a nobie like me the solution is not obvious...
OK, another Edit - with all my code: just can't understand what to do....
This is: basicViewController.h:
#import <UIKit/UIKit.h>
#interface basicViewController : <#superclass#> <UITextFieldDelegate>
#interface basicViewController : UIViewController <UITextFieldDelegate> {
//every object that we want to interact with (like text field or lable) is call an outlet!!!!
//here we define the outlets for our program
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
//here are the getters and setter for our outlets
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
//method decleration for the OK button action
- (IBAction) doSomething;
//method for hiding the keyboard when clicking on empty area in the app
//we will put an invisible button on all area and clicking on it will make keyboard disapear
- (IBAction) makeKeyboardGoAway;
#end
This is basicViewController.m:
#import "basicViewController.h"
#implementation basicViewController
//synthesizeing the objects that we made' this will create the getter and setters automaticly
#synthesize txtName;
#synthesize lblMessage;
- (IBAction) doSomething{
// makeing keyboard disapear when pressing ok button (doing that form the text field)
//when pressing the OK button, the keyboard will disapear and when clicking in the text field it will show again
[txtName resignFirstResponder];
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",txtName.text];
//the objective-c way for setting the test in the text field
[lblMessage setText:msg];
//the regular object oriented way
//lblMessage.text = msg;
[msg release];
}
- (IBAction) makeKeyboardGoAway{
[txtName resignFirstResponder];
}
//when clicking the return button in the keybaord
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(#"Working!!!");
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Maybe now i am more clear, sorry i didn't do it before.
Any one has an idea what am i doing wrong? it should be pretty strait forward.....
EDIT - Adding an image of all the elements, i hope that will help to help me :-)
10x a lot for every one that is trying to help....i really like this framework, it is so great after c++ and java, python and many other...and i am working with a book, but it is for ios 3.1, maybe that is the problem.....
Firstly you should check if textFieldShouldReturn: is actually being called by adding an NSLog statement or breakpoint at the beginning of the method.
Once that's out of the way, try an manually declare that your view controller conforms to <UITextFieldDelegate> protocol in your interface file:
#interface YourClass : ... <UITextFieldDelegate>
Also declare a property & outlet for your UITextField, make the appropriate connections in IB and manually declare self as the UITextField delegate with:
self.yourUITextFieldObject.delegate = self;
Once that's done see if your method above is now being called and make sure you return YES.
Just write one line in the
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
}
before return YES;
the final version will be as given below:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"%#",textField.text);
}
You need to assign the delegate of the textfields to your file owner. The textfields are sending the message, but doesn't have a delegate to respond to it.
Use the interface builder to do that.
You have to implement this method..
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Like Rog said, don't forget to register the textfield to the delegate, you can do this manually as he said but in Storyboard you can just control drag from all of your textfields to the view controller and register the delegate (choose delegate). Only the textfields that are registered can make use of all those methods.
So this line is important:
self.yourUITextFieldObject.delegate = self;
Or even more easy these days is to just use the storyboard:
textfield is in a subview? in this case, make sure textfield have as delegate the FileOwner.
put a log at the
- (IBAction) makeKeyboardGoAway
function. I think its this method everytime anything is tapped on the screen. In that case, you will need to send the touch event to the text field. Not sure how this is done but that should do it.
Else try removing the which takes care of tap(click) all over the view and try to do what you are doing.
Most likely the problem is that your actual view controller in the running application is not a "basicViewController" but a UIViewController that does not implement the UITextFieldDelegate-protocol.
What you've done in the interface builder by selecting your class "basicViewController" as the FilesOwner is just declaring the FilesOwner-object in your running application to be of type basicViewController; the actual object is not instantiated by this declaration and in your case it is not in the xib / nib.
Some other part of your code actually instantiates a view controller object and loads the xib / nib file. At that place, I guess your code is instantiating a UIViewController (typically by auto-generated code) and not an instance of your basicViewController; you simply have to change the class there.
Furthermore, this error often happens when using a UINavigationController or UITabBarController in the Interface Builder that is (should be) configured to instantiate and load other custom views. If you use such a higher-level controller, double-check that it is actually configured to use your basicViewController, not UIViewController when loading your view from the xib / nib.
Hope, that solves the issue!
Can you try this..
#interface ClassName : SuperClass < UITextFieldDelegate >
Use like this...
textfield.delegate=self;
and use the UITextFieldDelegate in .h class
You can always dismiss the keyboard when you don’t even know which view the text field is in by using:
Objective-C:
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
Swift:
UIApplication.sharedApplication().sendAction("resignFirstResponder",
to:nil,
from:nil,
forEvent:nil)
I need to keep track of which text field is the firstResponder for my custom keyboard to work. In the code below, I have grossly oversimplified my program, but here is the gist of the problem:
#implementation SimplePickerViewController
#synthesize pickerKeyboard;
#synthesize textView;
#synthesize textView2;
#synthesize firstResponder;
-(void)viewDidLoad{
pickerKeyboard = [[PickerKeyboardViewController alloc] initWithNibName:#"PickerKeyboard" bundle:nil];
pickerKeyboard.delegate = self;
[self.textView setInputView:pickerKeyboard.view];
[self.textView setDelegate:self];
[self.textView2 setInputView:pickerKeyboard.view];
[self.textView2 setDelegate:self];
}
-(void)hideKeyboard{
[self.firstResponder resignFirstResponder];
self.firstResponder = nil; //without this line, the code doesn't work.
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
self.firstResponder = textView;
[self.pickerKeyboard.picker reloadAllComponents];
return YES;
}
If I remove the line setting the firstResponder to nil, the code ceases to function properly, but I am not sure why. (Without that line, I can select the first textView to bring up the keyboard, but after that I can never bring the keyboard back. Any ideas? Thanks!
I'm not sure that I understand why firstResponder needs to be kept track of for a custom keyboard to work. I use a custom keyboard without knowing what the first responder is.
Do you use:
textView.inputView = pickerKeyboard
How about the following, called on the view to resign the first responder:
[self.view endEditing:NO];
I have had a similar problem and I have just figured out the issue. Somewhere in some part of Apple's first responder code, they are using a selector named firstResponder. When you created the property firstResponder you inadvertently overrode that selector. That will cause Apple's code to fail. This, in my humble opinion, is a bug in Apple's framework, and the firstResponder method isn't documented anywhere. Name your property myFirstResponder or anything else and everything should work just fine.
See Why does the keyboard not show when a view is popped from the navigation stack?