UITextFieldDelegate crash (exc_bad_access) - ios

Looks like I have a problem with UITextFieldDelegate.
I just created a view controller that responds to UITextFieldDelegate protocol, and easily added the field to the xib, then set delegate field...you know.
But when I trying to press the field (to start editing, the program crashes).
Same thing happens when I trying to create field programmatically.
Here is call stack:
Here is full code:
.h
#import <UIKit/UIKit.h>
#interface TopBar : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField * field_top;
}
.m
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(#"textFieldShouldBeginEditing");
textField.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"textFieldDidBeginEditing");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(#"textFieldShouldEndEditing");
textField.backgroundColor = [UIColor whiteColor];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"textFieldDidEndEditing");
}
Delegate is set by IB.
Error screenshot:
Any help please.

Ensure you have this in your .h
#interface TopBar : UIViewController <UITextFieldDelegate> {
}
#property (nonatomic, weak) IBOutlet UITextField *field_top;
and remove from the #interface
IBOutlet UITextField * field_top;
It sounds like your field_top is being released and you're trying to access it later, thats why it's crashing.

I found the answer.
The solution is ti also use addChildViewConroller, not only addSubview.
Hope it will help to someone...

Related

Error in if statement while comparing UITextField's text and a string

In my app, I have a view controller which is having a search bar with UItextfield at the top and a UIImageview below that. The image view is initially hidden.
I want this image view to unhide through an if statement. The user will enter keywords into the textfield and when a certain word will match a pre defined string in the.m file, then it must show the image.
I originally had two view controllers but now I added another one (thirdviewcontroller). As I enter a word into the textfield, the simulator will direct me back to the code highlighting in green on this line:
if ([string1 isEqualToString:string2]) {
locationMap.hidden = YES;
This is .h file:
#interface ThirdViewController : UIViewController
{
IBOutlet UITextField *searchLocation;
IBOutlet UIImageView *locationMap;
}
-(IBAction)SearchGo;
#end
This is the .m file:
-(IBAction)SearchGo{
NSString *string1 = searchLocation.text;
NSString *string2= #"sydney";
if ([string1 isEqualToString:string2]) {
locationMap.hidden = YES;
}
}
It sounds like you've accidentally set up a breakpoint. Simply remove the breakpoint by clicking the blue arrow to the left of the line it breaks on.
In viewDidLoad method, use:
locationMap.hidden = YES;
In your -(IBAction)SearchGo method, use:
locationMap.hidden = NO;
OR for your searchGo method:
-(IBAction)SearchGo{
if ([searchLocation.text isEqualToString:#"sydney"]) {
locationMap.hidden = NO;
}else {
//implementation
}
}
I am guessing, you have attached the IBAction with your textfield,searchLocation and triggered the action specifying "Touch up Inside". This will not work for couple of reasons.
First of all, you need to implement the textFieldShouldReturn: delegate method, so that your controller knows when you press return, it should hand over the control from your text field. Then again, a you have attached your action method to your text filed, as soon as you tap on the textfield, it goes to your method and start comparing but at this point, you have typed nothing in your textfield and it fails to conform to your if condition.
the solution is to either use the have a button and attach the action method to that button. That way, after you have typed the word "sydney", and you hit on the button. It will take whatever in your textfield and compare to that.
Here is the solution-
See the extra button named "Go". Attach your method to it.
This is my .h file-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
#property(nonatomic, strong) IBOutlet UITextField *searchLocation;
#property(nonatomic, strong) IBOutlet UIImageView *locationMap;
-(IBAction)SearchGo:(id)sender;
#end
And this is the .m file-
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic, strong) NSString *string1;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark- textfield delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
self.string1 = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(IBAction)SearchGo:(id)sender{
NSString *string2= #"Sydney";
if ([self.string1 isEqualToString:string2]) {
self.locationMap.hidden = NO;
}
}
#end
Save the string from the textfield after your editing is done through the delegate method. Make sure, you attach the UITextFieldDelegate to your ViewController.
Alternatively, you may want to avoid all this trouble and use the UISearchDisplay controller.

resignFirstResponder Not Responding

I have a text field named "fieldPassword" declared as an IBOutlet
#property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
I synthesize it later on and then, in an attempt to have the return key dismiss the keyboard, I have:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
fieldPassword.delegate = self;
[fieldPassword resignFirstResponder];
[self.view endEditing:YES];
return YES;
}
The problem is that when I run the simulator and hit the return key in the designated text field, nothing happens. Previously, I also had fieldPassword.delegate = self; in -viewDidLoad and that crashed the simulator with an unrecognized selector error.
Any help is much appreciated!
It should be [self.fieldPassword resignFirstResponder]; instead of [fieldPassword resignFirstResponder];
Also self.fieldPassword.delegate = self; should be in viewDidLoad or viewDidAppear
If you don't set the delegate earlier, you won't get the delegate callback. Try this in viewDidLoad:
self.fieldPassword.delegate = self;
You might have been missing the self before.
Follow the below things
1.Go to xib or storyboard where you have set that your view.
2.Right Click the TextField.If you click that you can see the
Outlets->Delegate with Empty Circle
3.Just connect with File's Owner.It is Yellow Color.
Once you do this,the circle is not empty.It is filled now.
4.Then go to declaration or .h part
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelegate>
5.Then in .m
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

Why scrollViewWillBeginDragging in uiTableViewController didn't work?

In file .h I have:
#protocol VVInformationTableViewControllerDelegate;
#interface VVInformationTableViewController : UITableViewController <UITextFieldDelegate, UIGestureRecognizerDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate>
#property (strong, nonatomic) VVUser* currentAttendee;
#property (weak, nonatomic) id<VVInformationTableViewControllerDelegate> delegate;
In file .m:
In viewDidLoad:
self.tableView.delegate = self;
and I have method:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self dismissKeyboard];
NSLog(#"HEJ");
}
But when I dragging my tableView (no matter if keyboard is show or not) this method didn't work. I looked for this problem but I couldn't find asnwer.
EDIT:
I make this (scrollViewWillBeginDragging) method in parentViewController and it works. But when I call in this method in parentViewController [self.infoTableView dismissKeyboard] (VVInformationTableViewController as childViewController) which is in childView It didn't work. For example if I try use dismissKeyboard:
- (void)dismissKeyboard {
[self.textField resignFirstResponder];
NSLog(#"%#", self.textField.text);
}
in childView then self.textField.text in NSLog is good, but when I try call it in parentView then it is nil, so resignFirstResponder on this textfield didn't work.
I make protocol with method dismissKeyboard in .h of childViewController and:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
self.textField = textField;
self.tableView.scrollEnabled = NO;
}

Action when pressing return key on keyboard (iOS)

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self checkRun:nil];
return YES;
}
I'm trying to complete the IBAction checkRun when the return key is pressed, by using the above code, but it doesn't seem to be working. Where am I going wrong? I thought maybe it's because I'm not directly referencing the textfield that I'm typing in, but I can't work out where I'd need to put the name of that textfield.
Thanks in advance.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) UITextField *textField;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
}
This UITextfield subclass enables you to set a condition for the text and dynamically change the UIReturnKey:
https://github.com/codeinteractiveapps/OBReturnKeyTextField

For some reason the method DidBeginEditing: (UITextField *)textfield is not working

I'm trying to move the view up when the keyboard shows so it wont cover up the screen, but for some reason its the -(void)DidBeginEditing: (UITextField *)textfield is not working.
- (void)textFieldDidBeginEditing:(UITextField *)ga1
{
/* should move views */
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 220);
}
- (void)textFieldDidEndEditing:(UITextField *)ga1
{
/* should move views */
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 220);
}
its nor going into the method, can anyone tell me why?
In the interface of the class add the line , so in the .m file you would put above where it says #implementation...
#interface MyClassName () <UITextFieldDelegate>
// properties can also go here
// for example dragging the IBOutlet for the textfield from the storyboard
#end
You then in viewDidLoad should set the delegate of the UITextField like so...
-(void)viewDidLoad {
// whatever code
self.textField.delegate = self;
}
Alternatively, and more cleanly, you can do this in the story board by control clicking the text field and dragging the indicator to the view controller class icon (the icon to the furthest left) in the lower bar.
Also, why are you calling the argument to the textField in your implementation "ga1"? Best practice you should call it
- (void)textFieldDidBeginEditing:(UITextField *)textField
One final note is that if you have multiple textFields you should set the delegate for each of them in the way described above. This is why the storyboard way of doing it is "cleaner," because it keeps you from having multiple delegate declarations in your code.
If implemented, it will get called in place of textFieldDidEndEditing
#interface MyViewController :UIVieController <UITextFieldDelegate>
#property UITextField *myTextField
#end
#implementation MyViewController{
}
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextfield.delegate=self;
}
-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
if(reason==UITextFieldDidEndEditingReasonCommitted)
{
NSLog(#"Committed");
}
}
Implement <UITextFieldDelegate> Protocol for your class. And set the delegate to self. Here is Apple's document UITextFieldDelegate Protocol

Resources