EXEC_BAD_ACESS for retained object [duplicate] - ios

This question already has answers here:
Not able to release view Controller/ cause EXC_BAD_ACCESS
(2 answers)
Closed 2 years ago.
I am using xCode 4.3 and this is weird. I have properly initialized my UITextfield but when i call it in IBAction it gives me EXEC_BAD_ACCESS>
in .h File
interface .........
{
IBOutlet UITextField * usernameField;
}
#property(nonatomic,retain) UITextField * usernameField;
in .m File
#implementaion ............
#synthesize usernameField;
- (IBAction) editingEnded:(id)sender
{
[usernameField resignFirstResponder];
}

I think your property should have iboutlet on it to.
#property(nonatomic,retain) IBOutlet UITextField * usernameField;
does that solve the problem?

I Think you should implement the UITextFieldDelegate Protocol, this way you can manage the event when the user "Ends Editing" trough the delegate's methods. If I understand correctly, what you want to do is to "Resign First Responder" so I suggest you to do the following:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;
}

Related

resignFirstResponder with Text Field is not working

I have four UITextFields and associated each with an outlet in .h files. I have also defined 4 dismiss function for each textfield as shown below:
- (IBAction)dismiss1:(id)sender;
- (IBAction)dismiss2:(id)sender;
- (IBAction)dismiss3:(id)sender;
- (IBAction)dismiss4:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *name;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *weight;
#property (strong, nonatomic) IBOutlet UITextField *age;
Implementation of dismiss function in the .m file:
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss2:(id)sender {
[sender resignFirstResponder];
}
...
I am very sure that the outlet is connected to each UITextFiled correctly. The IBAction of each dismiss function is also connected with 'Editing did end' event correspondingly. However, when I ran the app using simulator, the keyboard will not dismiss when I click 'Enter/Done'. It's also very weird that when I place the breakpoint inside the dismiss function, clicking 'Enter' when typing in the corresponding UITextField does not bring up the debugger.
Thanks a lot for helping!
Update: I checked the object type of sender (dismiss1) using breakpoint and it's UITextFiled. However, I did not entered the debugging mode when I click 'Enter' in the first TextField, but entered the debugging mode when I click on the second TextField (before typing).
set UITextFielDelegate
textfieldname.delegate = self;
use this code it will resolve your issue
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Use the code to resign keyBoard without specify textFiled name.
- (IBAction)dismiss1:(UITextField*)sender {
[self.view endEditing:true];
// or use [sender resignFirstResponder];
}
Add text field delegate
name.delegate = self;
email.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
No need to create four actions for textfield
You can set delegate of Textfield and use following method
- (void)textFieldDidEndEditing:(UITextField *)textField
Inside this method you can compare your textfield with method returning textfield and get you task done in "if" condition
Hope it help you!!
Assign the tag to different textfields
then >
textfield.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textfield.tag==num)
{
[textField resignFirstResponder];
}
return YES;
}
The Editing did end is called when a textfield did lose the "focus", mean it is not the first responder any more (because it has resigned or an other one became first responder).
Use textFieldShouldReturn to detect when the return key is called.
And call resignFirstResponder on the texfield when this is called. The Editing did end event will occur after that.
Edit : you have to implement something (Your ViewController?) as delegate of the textfield.

textViewDidBeginEditing not working in IOS 9

i'm new in Objective C . What i'm going to do is simple just to animate keyboard up when textfield is focused and shrink when it's not. I already followed some other tutorial on how to set up this step by step but it not working.The event textViewDidBeginEditing is never get called when textfeld is focus.
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *cCodeTextField;
#property (weak, nonatomic) IBOutlet UITextField *jidTextField;
- (IBAction)nextBtnTapped:(id)sender;
#end
I'm also set the delegate to the textfield but it's not working.
- (void)viewDidLoad {
[super viewDidLoad];
[self addTextFieldBorder:_jidTextField];
[self addTextFieldBorder:_cCodeTextField];
_jidTextField.delegate = self;
}
-(void) textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"Enter");
}
It looks like your ViewController class is implementing methods from the wrong delegate. It is implementing UITextViewDelegate methods when it should be implementing UITextFieldDelegate methods.
Note that 'jidTextField' is of type UITextField.
Your delegate method is called 'textViewDidBeginEditing' which is a UITextViewDelegate method and takes a UITextView as a parameter.
The issue here is that your class is implementing delegate functions for UITextViewDelegate and not UITextFieldDelegate.
The correct method definition is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Here is a link to the documentation for the correct delegate:
https://developer.apple.com/reference/uikit/uitextfielddelegate
Hope this helps!

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;
}

UITextFieldDelegate crash (exc_bad_access)

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...

Adding UITextField input to NSMutableArray [duplicate]

This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Closed 9 years ago.
I'm trying to add UITextField input into an NSMutableArray using the following IBActions, connected to the 'did end editing' part of the UITextFields:
- (IBAction) returnKey1
{
[textInputOne addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[textInputOne resignFirstResponder];
[players addObject:textInputOne.text];
}
- (IBAction) returnKey2
{
[textInputTwo addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[textInputTwo resignFirstResponder];
[players addObject:textInputTwo.text];
NSLog(#"array: %#",players);
}
and I've initialized the players array in the viewDidLoad section like so:
- (void)viewDidLoad
{
[super viewDidLoad];
players = [[NSMutableArray alloc] init];
}
but the array remains 'nil'. Anyone know how to fix this?
UITextField doesn't send any actions when the user taps Return. So you won't receive the “did end editing” action when the user taps Return.
UITextField sends “did end editing” when it resigns first responder. You can make it resign first responder when the user taps Return by setting the text field's delegate and implementing textFieldShouldReturn:. Start by finding the class extension at the top of your ViewController.m. Edit it (or add it if it's not there) to declare that the class conforms to UITextFieldDelegate:
#interface ViewController () <UITextFieldDelegate>
#property (nonatomic, strong) IBOutlet UITextField *textFieldOne;
#property (nonatomic, strong) IBOutlet UITextField *textFieldTwo;
#end
Next, implement textFieldShouldReturn: in the class to make the text field resign first responder:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Finally, in your xib, connect each text field's delegate outlet to the view controller (which is normally File's Owner).
I hope you have set the delegates to textfield properly. As you have allocated the players array in viewDidLoad try with following code
- (IBAction) returnKey1
{
[players addObject:textInputOne.text];
}
- (IBAction) returnKey2
{
[players addObject:textInputTwo.text];
}
Both returnKey1 & returnKey2 IBActions assigned to textfield did end editing events.
Now to resign the key board
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
I tried the same thing in one sample project it worked well.

Resources