App crashed on touch of UITextField in ios [duplicate] - ios

This question already has answers here:
-[UIViewController _keyboard]: unrecognized selector sent to instance 0x7b731ac0
(2 answers)
Closed 6 years ago.
I added one text field in storyboard and link it with header file.
Now when I run the application I get the error like
[InputViewController _keyboard]: unrecognized selector sent to instance
I tried many things but it didn't worked.
here is interface of text field
#property (strong, nonatomic) IBOutlet UITextField *countryCode;
This is the line that uses the value of text field
NSString *body = [NSString stringWithFormat:#"country=%#", self.countryCode.text];
How can I solve this?
Connections of Textfield can be seen below:

The problem is with your name:
#property (strong, nonatomic) InputViewController *inputViewController;
Apple has added a property with similar name in UIResponder class:
#property (nonatomic, readonly, retain) UIInputViewController *inputViewController NS_AVAILABLE_IOS(8_0);
This is what happens:
User taps your TextField.
Because of the similarly named property, UIKit Framework mistakes your custom InputViewController to be an instance of type UIInputViewController.
UIKit tries to call keyboard method of UIInputViewController on your InputViewController's instance.
keyboard method does not exist on your InputViewController because it is not an instance of UIInputViewController.
You get the error that an unrecognized selector is sent to your ViewController because there is no such method in your ViewController and the application correctly gets crashed.
Change the name of that property and you should be fine.
For more details, See Brian Nickel's answer here.
Also Read, Apple's documentation on UIInputViewController.

Related

Objective-C: Diagnosing unrecognized selector error

I am using htautomcomplete to add autocompletion to a textfield and getting the error below.
As far as I know I have made the field in question an HTautomplete field but it is saying that it is trying to send the message to an UITextField. Could this be error or what else could cause this? Thanks for any ideas.
[UITextField setAutocompleteType:]: unrecognized selector sent to instance 0x17eea9a0
(lldb)
//code creating property
#property (unsafe_unretained, nonatomic) IBOutlet HTAutocompleteTextField *titleField;
Since this is an IBOutlet, it's most likely that you didn't set the class of your UITextField in the .xib file to be an HTAutocompleteTextField.
You can do so by selecting the object in your .xib file, then going to the Identity Inspector and setting the class under Custom Class.

Connecting next field chain for form text fields

I'm trying to connect the text fields in a sign up form so that, upon done being pressed for each, the next field in the form automatically becomes active. To achieve this, I've extended the UITextField class, as follows, in the view controller for the sign up view.
#interface UITextField (Extended)
#property (nonatomic, weak) IBOutlet UITextField* nextField;
#end
I've then set the nextField outlets via interface builder for each field in the form, and have the following code implemented in the view controller, which is also acting as the delegate for the text fields in the form.
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
UITextField* field = textField.nextField;
if (field)
[field becomeFirstResponder];
return NO;
}
However when building and running the app, it immediately crashes giving the following error :
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason:'[<UITextField 0x78729580> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key nextField.'
A bit lost as to how to address this, any help would be appreciated! Thanks in advance.
Just remove all the outlets and recreate them again. You just missed or duplicated one of them, since you have several textfields it is highly possible something has gone awry. And make sure that each nextField really points to next field :)
UPDATE: It's late night so I missed one big problem.
You can't add new properties via category to an existing class like that. The reason is category is not capable of creating a backing variable behind the scenes. (There is a way with associated objects..but it's best not to delve into that black magic.)
Instead of UItextField+extended category, subclass the UITextfield class.
#interface CustomTextField : UITextField
#property (nonatomic, strong) IBOutlet UITextField *nextField;
#end
Then in interface builder, you will have to set that CustomTextfield class in the class inspector for each text field.
The problem is that while you can declare a property in a category, the compiler will not define the property getter and setter for you. You have to define the getter and setter yourself.
You can use an “associated object” to store the property value. See this answer for example code. Since you don't want your nextField property to retain the next field, use OBJC_ASSOCIATION_ASSIGN instead of OBJC_ASSOCIATION_RETAIN_NONATOMIC when setting the associated object.

uilabel gets deallocated prematurely in ARC

I've been working on a project for several weeks, and recently implemented a singleton object to assist with saving data. After this was implemented, I've been having issues updating labels inside my main view controller.
For example, I'm trying to update the following labels:
#property (nonatomic, retain) IBOutlet UILabel *numDrinksLabel;
#property (nonatomic, retain) IBOutlet UILabel *BACLabel;
with the following code, which is inside a function that gets called on a button press:
BACLabel.text = [NSString stringWithFormat:#"%.2f", user.BAC];
numDrinksLabel.text = [NSString stringWithFormat:#"(%i)", user.numDrinks];
this code block gives me the runtime error:
-[__NSCFString setText:]: unrecognized selector sent to instance 0x1197ef40
However, the same code block called inside viewDidLoad or viewDidAppear is executed with no problems. Initially this suggested to me that there was a problem with my #property declaration, but I get the same error when I change retain to strong, and when I change to weak, the uilabel object is simply null, which is to be expected but nonetheless very frustrating.
So the question is, why would the label objects become dealloced after the viewDidAppear function?
Any suggestions on how to fix this or further test for the root cause would be greatly appreciated!
It seems that your object which contains the iVars numDrinksLabel and BACLabel does no longer exist when you assign something to the text property of the UILabel objects.
Since this happens after you press a button, you have been in the main event loop before. In this loop, any autorelease object will be released if it is not retained by some object.
Thus it seems to me that the object that has your UILabels as iVars is an autorelease object, and it is not retained because you don't use setter methods like self.BACLabel.text = but simply assign methods as BACLabel.text =.
So try replacing your assignments like BACLabel.text = by setters like self.BACLabel.text =, as sixthcent said.
Please check if the superview of these labels is also declared strong

Objective-C NSString Category Getter/Setter Crash [duplicate]

This question already has answers here:
Objective-C: Property / instance variable in category
(6 answers)
Closed 9 years ago.
I'm trying to create NSString Category but the app crashes when trying to access getters/setters.
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#interface NSString (MyCat)
#property (assign, nonatomic) ABRecordRef personRef;
- (void)setPersonRef:(ABRecordRef)personRef;
- (ABRecordRef)personRef;
#end
Can anyone point out the problem?
You cannot add properties to a class via a category. Adding methods is allowed because it doesn't increase the size of the class. Properties don't just add a getter and setter method, they also add a field to your class. The best way to add properties/fields to an existing class is to subclass it.

UITextField possibly being released too soon and I cannot figure out where and why

Here are the areas of concern in my class:
.h
#interface TimerViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *hourField,*minuteField;
}
#property (strong, nonatomic) IBOutlet UITextField *hourField,*minuteField;
#end
.m
#implementation TimerViewController
#synthesize hourField,minuteField;
#end
My issue is that I can use the text fields multiple times (retrieve and set properties) before the app crashes with this log:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString setText:]: unrecognized selector sent to instance 0x1e1dafa0'
This happens when the code [self.hourField setText:#"5"]; is run. It can also happen when checking if [self.hourField isFirstResponder] with the same type of log.
The console sees the text field as a __NSCFString which leads me to think it may have been released?
Runs with ARC, and I'm always using synthesised accessors. Never had this issue before and can't see where I'm going wrong.
I think the problem may be a confusion with how you declared the variables. You do not need the #synthesize statement anymore and and normally you would want to have your private iVars have a different spelling than your property declarations. To see if this is the problem remove both your #property declaration and your #synthesize and access hourField simply as [hourField setText:#"5"]; . Another way would be to remove the #synthesize and the iVar declarations altogether.
Other I would check are whether the IBOutlet is connected to the right item in your .xib or storyboard.

Resources