What I got is a TableView which is using Prototype Cells and an array feeding the dataSource - depending on the datasource, I'm using a certain Table View Cell identifier to build the cells. (Please correct me if I'm doing something wrong here - I am new to iOS dev and am looking for best practices wherever possible :)
Anyway, one of the Table View Cell I've created in Storyboard has a UITextField. I have it set up in Storyboard GUI that the Return Key is set to 'Done' and 'Auto-enable Return Key' is checked. I've then gone into my controller and used the following code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(#"finished change...");
}
I've also got a 'Editing Did End' event linked to my controller which is coded to output to NSLog.
The Log outputs the text placed in textFieldDidEndEditing and my event function when I hit Return or Done. However, when I select a table cell, or start moving the slider nothing happens - meaning the keyboard is still on the screen.
Is there a way to resolve this?
Also, whilst I've been working on this Prototype Cells, it seems to be rather laborious for what I need. Basically all I require is a basic table to allow the user to edit some settings, so there would be:
2 textfields that can be edited,
1 slider,
2 switches
I placed all of this in a Static UITableView however it did not appear - this is due to a restriction on this type of table requiring a UITableViewController. Is there a way around this?
Sorry for the long post - I'm still getting my head around iOS dev but I am enjoying it so far :)
Any help would be greatly appreciated.
I usually keep an ivar around to keep track. It lets me conveniently dismiss the keyboard based on all kinds of events:
#interface ViewController () {
UITextField *_textFieldBeingEdited;
}
#end
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_textFieldBeingEdited = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
// no need to resignFirstResponder here.
_textFieldBeingEdited = nil;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_textFieldBeingEdited) {
[_textFieldBeingEdited resignFirstResponder];
}
...
}
Related
I am trying to present custom keyboard on UITextField.
Every thing works fine but when voice over is on and focus goes on UITextField, it says "uitextfield, double tap to edit". Once keyboard is on and again u tap it, it still says same message instead of saying "uitextfield is editing".
This problem persist only when we try to present custom keyboard view.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
field1.inputView=keyBoard;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return TRUE;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
As you can see there is not much in the code.Only line which create problem is
field1.inputView=keyBoard;
If you use Default keyboard then voice-over works as usual.
I've got a UIViewController with an additional small UIView I created on top of it (subview). When I click a button this view hovers to the center of the screen. The issue is the i've got a UITextField in the additional UIView and i cannot seem to get the return key to work.
Although I set my IBAction to the event "Editing did end" of the text field, when i click the return key, the IBAction doesn't run.
What am I doing wrong?
you just set Your Delegate for example :- yourtextfile.delegate=self; and also dont forget to add delegate method in to .h file
#interface contacts_detailView : UIViewController<UITextFieldDelegate>
and then you delegate textFieldDidEndEditing
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//your new view apear code here
[textField resignFirstResponder];
}
make sure UITextFieldDelegate at interface
Clicking on "Return" doesn't trigger an "Editing did end" event. Only when you'll call resignFirstResponder on the UITextField will you see the event triggered.
What you should do is set your UIViewController as the delegate of the UITextField and implement the method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
First of all, delegate the TextField to the file's owner like this:
yourtextField.delegate = self in your viewDidLoad.
And then add this to the view controller
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
It should work.
There is no need to write more code for key board return. Please write this in your .m file , it will work for any number of text field , no need to write again again for different textfield.
use <UItextfieldDelegate> in your .h file. Then make wiring with fileowner in nib file.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}
I am using a UIPickerView and I want my keyboard to never show up for some of my UITextFields. When I call resignFirstResponder on the UITextField after it is touched it doesn't make the keyboard go down.
- (IBAction) txtFieldClicked:(id)sender {
[txtField resignFirstResponder];
}
For each text field where you don't want to be able to show the keyboard, do:
[textField setUserInteractionEnabled:NO];
You can also check a corresponding block in the Interface Builder UI. This prevents the text field from receiving the touch event that would otherwise trigger it to display the keyboard.
Set text field delegate and implement its delegate methods
See UItextFieldDelegate
If you dont want the keyboard to pop up, return NO for all those text fields here
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqualTo:self.myTextField]) {
return NO;
}
return YES;
}
I am trying to show minus symbol when I show data from an sqlite database, but if we show an edit button it shows a delete symbol. I am using navigation controller to show data. I want to show when the page loads, it must be with a "-" symbol without edit button? Does anyone have an example or tutorial?
If I understand you correctly, you want to delete the row immediately, without confirmation of tapping a delete button.
First: Users are used to having a delete button confirmation, and if you delete immediately, they may be surprised.
Second: If the issue is deleting many rows quickly, could you instead allow the user to select multiple rows and delete them all at once (like in the email app)
Third: A solution to your question:
Make a custom UITableViewCell and override this function
- (void)willTransitionToState:(UITableViewCellStateMask)state {
if (state && UITableViewCellStateShowingDeleteConfirmationMask) {
[delegate deleteCell:self];
}
}
You will need to also have a delegate property on the UITableViewCell subclass and set it to your view controller whenever you create a new cell. Then add this function in your view controller:
- (void) deleteCell:(UITableViewCell *)cell {
NSIndexPath *idx = [self.table indexPathForCell:cell];
if (idx) {
NSArray *arr = [NSArray arrayWithObject:idx];
//Delete from database here
if (/*delete success*/) {
[self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
}
}
}
Still not sure exactly what you're looking for, but if I understand better this time, you just want the - symbol to show up as soon as the view appears?
- (void) viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}
I am trying to implement a hello world application for the iPhone and i've worked through some. However i can't figure out how to get the keyboard to go away. I've seen this and no it doesn't help (i have resignFirstResponder in my code). I have connected the relevant textfield to the file's owner as a delegate. Here is the code that determines whether the keyboard should dissapear:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textField)
{
[textField resignFirstResponder];
}
return YES;
}
I'm sure this should be ridiculously obvious, but i cant find the answer. Thanks in advance for your help!
First off, just to clarify: you should connect the text field's delegate to the file's owner, not the file's owner's delegate to the text field. That may sound confusing, but you can check it easily by selecting your text field in Interface Builder and checking that its "delegate" connection points at the file's owner.
Next, what happens if you take out the if statement in your code? Linking the text field's delegate to the file's owner and then changing your code to:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
should produce the desired results.
If it still doesn't work, check that the file's owner's class is the same as the class you have that method implemented in. For example, if the code is in RootViewController.m, then you want to specify that the file's owner is an instance of RootViewController in Interface Builder.
your code
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textField)
{
[textField resignFirstResponder];
}
return YES;
}
is sort of fail.
You didn't call resignFirstResponder to the parameter of the delegate method, you called it to what I assume is an instance variable. This should have been written as follows:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == [self textField]) {
[theTextField resignFirstResponder];
}
return YES;
}