Firing didEndOnExit programmatically - iphone

I have many textfields created in my application, and i want them to close keyboard when user finishes editing. I know that if textField's created by IB, and there is an IBAction i can use but what if the textField's created like this,
int top = 60;
for(NSUInteger i=0; i< [kArray count]; i++){
UITextField *kField = [kArray objectAtIndex:i];
kField.frame = CGRectMake(130, top, 60, 30);
kField.borderStyle = UITextBorderStyleRoundedRect;
kField.autocorrectionType = UITextAutocorrectionTypeNo;
kField.returnKeyType = UIReturnKeyDone;
kField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[scrollView addSubview:kField];
top += 50;
}
How can i close the keyboard when user presses done button?

Use UITextFieldDelegate method:
#pragma mark UITextFieldDelegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
Remember to make your class comply with delegate like so:
#interface MyViewController : UIViewController <UITextFieldDelegate>

Try this:
- (BOOL)textFieldShouldReturn: (UITextField *)textField {
[kField resignFirstResponder];
return YES;
}

Related

pass #selector to my UIbutton but click event not response

my code :
- (void)showWithStatus:(NSString *)status barColor:(UIColor*)barColor textColor:(UIColor*)textColor click:(SEL)click{
if(!self.superview)
[self.overlayWindow addSubview:self];
[self.overlayWindow setHidden:NO];
[self.topBar setHidden:NO];
self.topBar.backgroundColor = barColor;
NSString *labelText = status;
CGRect labelRect = CGRectZero;
CGFloat stringWidth = 0;
CGFloat stringHeight = 0;
if(labelText) {
CGSize stringSize = [labelText sizeWithFont:self.stringLabel.font constrainedToSize:CGSizeMake(self.topBar.frame.size.width, self.topBar.frame.size.height)];
stringWidth = stringSize.width;
stringHeight = stringSize.height;
labelRect = CGRectMake((self.topBar.frame.size.width / 2) - (stringWidth / 2), 0, stringWidth, stringHeight);
}
self.stringLabel.frame = labelRect;
self.stringLabel.alpha = 0.0;
self.stringLabel.hidden = NO;
self.stringLabel.text = labelText;
self.stringLabel.textColor = textColor;
clickBn=[[UIButton alloc]initWithFrame:self.stringLabel.frame];
clickBn.backgroundColor=[UIColor blueColor];
[clickBn addTarget:self action:click forControlEvents:UIControlEventTouchUpInside];
if(!clickBn.superview)
[self.topBar addSubview:clickBn];
[UIView animateWithDuration:0.4 animations:^{
self.stringLabel.alpha = 1.0;
}];
// [self setNeedsDisplay];
}
and call this method:
- (IBAction)successButtonPressed:(id)sender {
[KGStatusBar showSuccessWithStatus:#"Successfully synced" click:#selector(clickBn)];
}
- (void)clickBn {
NSLog(#"sss");
[KGStatusBar dismiss];
}
the NSLog(#"sss") not show. i want to pass a method to UIButton of customView ,but when i click the UIbutton not respond.
Create an delegate in control in .h file before #interface and after header files.
#protocol YourControlDelegate <NSObject>
-(void) delegateMethod;
#end
in #interface
#property (nonatomic,assign) id <YourControlDelegate> yourdelegate;
in .m #synthesize the yourdelegate.
in button click action call [self.yourdelegate delegateMethod];
In your ViewController add YourControlDelegate like as follow
#interface YourVC : UIViewController <YourControlDelegate>
in .m implement the delegate method
-(void) delegateMethod
{
//you code.
}
Here when you click the button the delegateMethod in viewController will be get called.
action should be a selector method.
If you want to pass the button as an argument to the method user : for example
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
-(void)buttonClicked:(UIButton *)sender
{
//your code and you can also access the button properties using sender.
}
it is preferred. If you don't want to use sender(button) exclude the :
this is your clickBn method right?
- (void)clickBn {
NSLog(#"sss");
[KGStatusBar dismiss];
}
now what you doing in action? action:click??
do like following
clickBn=[[UIButton alloc]initWithFrame:self.stringLabel.frame];
clickBn.backgroundColor=[UIColor blueColor];
[clickBn addTarget:self action:#selector(clickBn) forControlEvents:UIControlEventTouchUpInside];
If you want to call method which is in another class then do like this
first make object of that class
ViewController *dvController = [ViewController alloc] init];
after that [dvController methodName];
do this all in your clickBn method First clickBn method will be called and it will call that another class method which you wanted to call
NOTE: you need to import that class in header

Dissmissing the keyboard when UITextField and UITextview displayed inside of UIWindow

I've added my UITextField and UITextView inside a UIWindow I can not able to type inside in both of them. Both are not letting me allow to type in those fields. And, can't able to dismiss it when return pressed in keyboard.
-(void)showAlert
{
alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
alertView = [[UIView alloc] initWithFrame:CGRectMake(10, 115, 300, 230)];
alertWindow.windowLevel = UIWindowLevelAlert; // puts it above the status bar
alertView.center = CGPointMake(alertWindow.frame.size.width/2, alertWindow.frame.size.height/2);
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.borderWidth = 1.0;
alertView.layer.borderColor = [[UIColor whiteColor]CGColor];
UITextField *txt = ....
....
....
txt.delegate = self;
...
...
[alertView addSubview:txt];
UITextView *txtview = ...
....
....
txtview.delegate = self;
...
[alertView addSubview:txtview];
[alertWindow addSubview:alertView];
[alertWindow addSubviewWithZoomInAnimation:alertView duration:1.0 option:curveValues[0]];
[alertWindow setHidden:NO];
}
I've declared both of the delegate methods also. I placed a breakpoint there and checked also, even that method not yet called.
Update -
textFieldShouldReturn delegate method
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[reserveView resignFirstResponder];
[reserveWindow resignFirstResponder];
[self.reserveWindow endEditing:YES];
[reserveView endEditing:YES];
return YES;
}
You should try this for dismiss the keyboard from the UIWindow
[self.window endEditing:YES];
Source
I found, that method has not been called because of UIWindow So, i changed my UIWindow to UIView And, i fixed this issue by using UIView+Animation.h from here It provides the animation what i required exactly.
- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option;
- (void) removeWithZoomOutAnimation:(float)secs option:(UIViewAnimationOptions)option;
Thanks to Google! Cheers!
The keyboard will never dismiss on its own (even if you hit return!). You'll notice that in your delegate methods, you will receive events whenever you press return/done/etc.
A simple way to make the keyboard dismiss every time someone hits the return key is to implement the - (BOOL)textViewShouldEndEditing:(UITextView *)textView method.
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[textView endEditing:YES];
return YES;
}
Edit: In your post you edited to say that you've declared both of the delegate methods... there are more than two so could you clarify what methods you've implemented? http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate
Try this
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.window addGestureRecognizer:tapGesture];
//[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
}
-(void)dismissKeyboard
{
[txt resignFirstResponder];
[textView resignFirstResponder];
}
Try this
That's the delegate method of textfield:-
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtview resignFirstResponder];
return NO;
}

UISearchDisplayController and custom UISearchBar

I want to change UITextField inside UISearchBar by subclass of UISearchBar. But when edit in UISearchDisplayController's UISearchBar, the field doesn't change the size which i set in :layoutSubView method.
How should I do that?
UISearchBar *searchBar = yourSearchController.searchBar;
UITextField *searchField;
for(int i = 0; i < [searchBar.subviews count]; i++) {
if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if(!(searchField == nil))
searchField.frame = CGRectMake(4, 5, 290, 30);
Good Luck.

Can we call a method by tapping on UITextField in iPhone sdk

countryField=[[UITextField alloc]initWithFrame:CGRectMake(0,0,100,25)];
countryField.textColor = [UIColor blackColor];
countryField.font = [UIFont systemFontOfSize:15.0];
countryField.backgroundColor = [UIColor redColor];
countryField.keyboardType = UIKeyboardTypeDefault;
countryField.enabled=NO;
countryField.delegate = self;
[countryField addTarget:self action:#selector(getCountry:) forControlEvents:UIControlEventEditingChanged];
[myView addSubview:countryField];
-(void)getCountry:(id)sender
{
printf("\n hai i am in getCountry method");
}
Your Code looks like you want your TextField disabled. The method is only called if you enable your UITextField.
But when you enable your UITextField the Keyboard appears and the User is able to edit the text. To prevent that you have to implement the - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField method (from the UITextFieldDelegate Protocol) and just return NO.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
In short: Enable your UITextField, implement the method above and change your Control Event to UIControlEventTouchUpInside (I think this is what you want !).
Change your code to
countryField=[[UITextField alloc]initWithFrame:CGRectMake(0,0,100,25)];
countryField.textColor = [UIColor blackColor];
countryField.font = [UIFont systemFontOfSize:15.0];
countryField.backgroundColor = [UIColor redColor];
countryField.keyboardType = UIKeyboardTypeDefault;
// IF YOU WANT CERTAIN METHOD TO GET FIRE WHEN TAPPED ON TEXTFIELD YOU HAVE TO ENABLE THE TEXTFIELD
// countryField.enabled=NO;
countryField.delegate = self;
// [countryField addTarget:self action:#selector(getCountry:) forControlEvents:UIControlEventEditingChanged];
[myView addSubview:countryField];
Then in the delgate method of textfield call the function you want to call i.e.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self getCountry];
return YES;
}
Where the function is
-(void)getCountry
{
printf("\n hai i am in getCountry method");
}
hAPPY cODING...
You could add a target for the UIControlEventTouchDown event just like you did for the UIControlEventEditingChanged event.

iphone - Search button on a UISearchBar

I have a search functionality using UISearchBar that occurs on-the-fly, so I think it would be more obvious to replace that "Search" button on the keyboard with "Done".
Is there a way to do that?
thanks
You can change the keyboardType property of your UISearchBar object. However, there is not a way to change the returnKeyType directly. You may be able to filter down and change it manually. Check the documentation for UISearchBar and see if you can find returnKeyType as that is what you are looking for.
I accomplish it this way:
// -- Basic UISearchBar setup.
self.theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,38)];
[self.theSearchBar setDelegate:self];
[self.view addSubview:self.theSearchBar];
// -- Customize the returnKeyType of the search bar's nested UITextField.
UITextField *searchBarTextField = [[self.theSearchBar subviews] objectAtIndex:1];
searchBarTextField.returnKeyType = UIReturnKeyGo;
Hope that is helpful. This approach (i.e. grabbing a subview by index) may break in the future, but it works fine for now.
for (UIView *view in _searchBar.subviews){
if ([view isKindOfClass:[UITextField class] ]) {
UITextField *searchTf = (UITextField *)view;
searchTf.returnKeyType = UIReturnKeyDone;
}
}
This is working for iOS 6
UITextField *searchBarTextField = [[searchBarObj subviews] objectAtIndex:1];
searchBarTextField.returnKeyType = UIReturnKeyDefault;
[searchBarTextField setEnablesReturnKeyAutomatically:NO];
This is working for iOS 7
for (UIView *subview in self.searchBar.subviews)
{
for (UIView *subSubview in subview.subviews)
{
if ([subSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
UITextField *textField = (UITextField *)subSubview;
[textField setKeyboardAppearance: UIKeyboardAppearanceAlert];
textField.returnKeyType = UIReturnKeyDone;
break;
}
}
}
Don't rely on it being the second subview, use isKindOfClass: method to check. It will be more iOS update proof that way.
for (UIView *subview in self.theSearchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
[(UITextField *)subview setReturnKeyType:UIReturnKeyGo];
break;
}
}