I just put UITextField in NavigationBar using xCode. But when I try to create IBOutlet binding my programm receive -[UITextField isEqualToString:]: unrecognized selector sent to instance.
Any suggestions?
Thnx.
You have to call it on the UITextField's text property:
UITextField *myTextField = [[UITextField alloc] initWithFrame:...];
myTextField.text = #"someString";
...
[myTextField.text isEqualToString:#"someString"]
Try this
-(void)viewDidAppear:(BOOL)animated{
UITextField *txtField=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
txtField.text=#"Hello";
[self.navigationController.navigationBar addSubview:txtField];
}
Will look like..
UITextField *textField = [[UITextField alloc] init];
// configure text field
self.navigationItem.titleView = textField
Of course you could also use a property in your view controller instead of a local variable.
isEqualToString is method of string class not UITextField. Thatswhy you get this error. You are passing method to wrong object. Use textField.text instead of textField alone
Please provide some sample code or a screenshot if you want a good answer. May I suggest you read this document for future questions.
This is clearly a case of you calling isEqualToString on a UITextField when this is a method that must be called on a NSString.
Related
I created an IBOutlet for a UIPicker and named it 'RequestedDate.' I then went on the type
_RequestedDate.delegate = self;
but I keep getting the error:
property 'delegate' not found on object type 'UIDatePicker'
I am relatively new with Xcode and am just starting to work with code. Is there something I am doing wrong or cannot I not have a delegate for a UIPicker?
Thanks~
UIDatePicker uses the target-action pattern instead of the delegate pattern.
You either add a IBAction for the 'value changed' event in Interface Builder or you do it in code like this:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[datePicker addTarget:self action:#selector(datePickerDidChange:) forControlEvents:UIControlEventValueChanged];
// ...
Then you implement the action method, and can retrieve the new date.
- (void)datePickerDidChange:(UIDatePicker *)picker {
NSDate *newDate = picker.date;
NSLog(#"Picker changed date to %#", newDate);
}
UIPickerView and UIDatePicker only look similar, they are completely separate classes. UIPickerView is a subclass of UIView and UIDatePicker is a subclass of UIControl, they are not related.
I don't think UIDatePicker has a delegate property. https://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
What are you trying to do exactly?
I am creating text fields dynamically and each text field has tag.Now, I am trying to get text of textfield using that tags but, it returning UIButton and my application getting crashed.How can i get text from tags? please help me.
Here is my code to get text :
UITextField *textValue = (UITextField *)[self.view viewWithTag:textField.tag];
NSString *chkText = textValue.text;
NSLog(#"entered Text %#",chkText);
my application is crashing at this line :
NSString *chkText = textValue.text;
and error is :
-[UIButton text]: unrecognized selector sent to instance
The main problem is your view contains button as well. So when you are trying to access it. It is returning UIButton object. So it is always better that check the condition whether it is button type or textfield type object. Try below:-
id nextTextField = [self.view viewWithTag:textField.tag];
nextTextField=([nextTextField isKindOfClass:[UITextField class]]) ? nextTextField.text : nextTextField.title;
NSLog(#"%#", nextTextField);
From your code (viewWithTag:textField.tag), If you can access your textField then why are you accessing it once more? However, the below description will help you
You have assigned the same tag to one of your UIButton control in the same view. Also make sure that textField.tag is greater than Zero.
The below code will fix the crash, however, it will not resolve your issue.
UITextField *textFeild = [[self.view viewWithTag:textFieldTag] isKindOfClass:[UITextField class]]?(UITextField *)[self.view viewWithTag:textFieldTag]:nil;
if(nil != textFeild)
{
NSString *chkText = textFeild.text;
NSLog(#"entered Text %#",chkText);
}
else
{
NSLog(#"textFeild is null");
}
Set textfield tag as any unique value like "346". (Avoid using 0 as tag value). And Use this --
UITextField *textValue = nil;
id myObj = [self.view viewWithTag:yourTagValue];
if([myObj isKindOfClass:[UITextField class]])
{
textValue = (UITextField*)myObj;
NSString *chkText = textValue.text;
NSLog(#"entered Text %#",chkText);
}
When you use tag, using NS_ENUM is a good habit.
NS_ENUM(NSUInteger, TEST_TAGS)
{
TAG_TEXT_INPUT,
TAG_BTN_GO,
};
Then you can use it.
[textField setTag:TAG_TEXT_INPUT];
First check to which UIVIew you are adding this textfield as a subview.
Take that UIVIew instance and call viewWithTag method then your code works.
I am having a hell of a time fixing a bug, which currently is only occurring when I run my app on the simulator. Essentially, I have a UITextView that I am trying to send the message endEditing to. If I send the message while the user is editing the textview (forced or otherwise) I get back YES. If however, the user has not yet begun editing the textview and I send the message endEditing:YES, I get back NO. Should this even be possible? Shouldn't endEditing:YES always force the view to end editing?
Additional Details: I have tried setting the owning class to be the uitextviews delegate, but even then it doesn't look like the shouldEndEditing method even gets called.
UPDATE: It does not seem that this is normal behavior (that the method should return no if the text field is not currently the first responder).
I created a simple test:
AppDelegate.h
#import <UIKit/UIKit.h>
#interface DRAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (assign) IBOutlet UITextView *textView;
#property (assign) IBOutlet UIButton *endButton;
#property (assign) IBOutlet UILabel *results;
-(IBAction)tapEndEditingButton:(id)sender;
#end
and AppDelegate.m
-(void)tapEndEditingButton:(id)sender
{
BOOL didEndEditing = [self.window endEditing:YES];
NSString *result = (didEndEditing) ? #"YES" : #"NO";
_results.text = result;
}
Regardless of whether the textfield has focus, and regardless of whether I set the force parameter of endEditing to YES or NO, didEndEditing gets set to YES.
Check Apple Doc on UIView
endEditing:
Causes the view (or one of its embedded text fields) to resign the first responder status.
...
Return Value
YES if the view resigned the first responder status or NO if it did
not.
Discussion
This method looks at the current view and its subview hierarchy for
the text field that is currently the first responder. If it finds one,
it asks that text field to resign as first responder. If the force
parameter is set to YES, the text field is never even asked; it is
forced to resign.
So... When you said:
If however, the user has not yet begun editing the textview and I send the message endEditing:YES, I get back NO
It's perfectly fine because in your scenario, since there is no firstResponder to resign, calling -endEditing: will return NO and doesn't harm the performance (isn't a bug either, imho)
To answer the essence of the question:
[UIView endEditing:YES/NO]; will return NO if the specified UIView object was not the firstResponder.
Example:
-(void)testEndEditing
{
UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom];
[btnTest setFrame:CGRectMake(0, 130, 320, 44)];
[btnTest setBackgroundColor:[UIColor blueColor]];
[btnTest addTarget:self action:#selector(myEndEditing:)
forControlEvents:UIControlEventTouchUpInside];
[btnTest setTitle:#"End Editing" forState:UIControlStateNormal];
[self.view addSubview:btnTest];
UITextField *txtFTest = [[UITextField alloc] init];
[txtFTest setFrame:CGRectMake(0, 50, 320, 30)];
[txtFTest setBackgroundColor:[UIColor orangeColor]];
[txtFTest setText:#"textField"];
[self.view addSubview:txtFTest];
/*
globally declare "UITextView *txtVTest;"
*/
txtVTest = [[UITextView alloc] init];
[txtVTest setFrame:CGRectMake(0, 80, 320, 50)];
[txtVTest setBackgroundColor:[UIColor redColor]];
[txtVTest setText:#"textView"];
[self.view addSubview:txtVTest];
//make UITextField object the firstResponder
[txtFTest becomeFirstResponder];
}
-(void)myEndEditing:(UIButton *)sender
{
//test endEditing method on UITextView object
BOOL isWhat = [txtVTest endEditing:YES];
NSLog(#"%i",isWhat);
}
PS: If neither the textField nor the textView is the firstResponder then it returns YES (dunno why but i'll check)
I am mocking up a quick demo of a project but am having a problem with a UITextField.
The behavior that we want is that when a user clicks on a button, there should be a custom view that appears with a UITextField and a UIButton in a custom view that overlays the main view.
I have a custom view called Searchview and the following in the Searchview.m. The problem is that when the textField is a property, it doesn't show but when it is a local variable, it does show. Can anybody help me with what is going on so that the UITextField shows? Is how I am doing this even the right idea (custom UIView or custom UIControl or a modal controller)? Finally, would setNeedsDisplay be appropriate here?
thx in advance
#interface Searchview()
#property (nonatomic, weak) UITextField *textField;
#end
- (void)drawRect:(CGRect)rect
{
// this doesn't work
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 25.0f)];
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.placeholder = #"Writer";
self.textField.borderStyle=UITextBorderStyleBezel;
[self.textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self addSubview: self.textField];
/* this works
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 120, 25)];
textField.returnKeyType = UIReturnKeyDone;
textField.placeholder = #"Writer";
textField.borderStyle=UITextBorderStyleBezel;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self addSubview: textField];
*/
UIButton *mButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
mButton.frame=CGRectMake(200.0f,10.0f,100.0f,37.0f);
[mButton setTitle:#"search" forState:UIControlStateNormal];
[mButton addTarget:self action:#selector(showSearchController:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:mButton];
[self setNeedsDisplay];
}
As a property - not showing:
As a local variable - showing:
#property (nonatomic, strong) UITextField *textField;
change the weak to strong and change the self.textFiled to _textField to have a try
And make sure your textField property not be released
It's pretty simple when you think about it. ARC (approximately) converts the following code:
self.weakProp = [[Foo alloc] init];
to the equivalent of the following "manually reference-counted" code:
Foo * temp = [[Foo alloc] init];
self.weakProp = temp;
[temp release];
Nothing is retaining it, so it is released.
I can only think of two reasons to have assign/weak IBOutlets:
For an outlet in a VC, so it doesn't retain a subview when its view is set to nil (e.g. on a memory warning). This is less relevant in iOS 6.0 since views are not automatically released on a memory warning (so if you do it, you can release them all explicitly).
For a view where the outlet points to a superview (and would cause a retain cycle). This is quite rare.
In general, I prefer strong IBOutlets: They might keep objects alive for a little longer than necessary, but they are safer than assign and more efficient than weak. Just watch out for retain cycles!
the following is my code:
UIImage *takePhotoImg = [UIImage imageNamed:#"add_pic.png"];
self.takePhoto = [[UIButton alloc] initWithFrame:CGRectMake(120, 100, takePhotoImg.size.width, takePhotoImg.size.height)];
[_takePhoto setImage:takePhotoImg forState:UIControlStateNormal];
[_takePhoto addTarget:self action:#selector(takePhotoBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_takePhoto];
when I use analyze,it shows the line:
[_takePhoto setImage:takePhotoImg forState:UIControlStateNormal];
Potential leak of an object allocated.
Did I need add release,or just ignore?
thank you in advance
UPDATE:
I did have released the button _takePhoto in my dealloc:
-(void)dealloc
{
[_takePhoto release];
[super dealloc];
}
my property :
#property(nonatomic,retain)UIButton *takePhoto;
If you're not using ARC then yes, you need to release it. When you add it to self.view's subview, self.view will retain it so the button will not go away.
If you don't need to access the button and don't need to hold a reference, you can release it right after the addSubview: call.
However, it looks like takePhoto is a property on your class. If that's the case and you want to hold a reference to the button for later use, just add the [_takePhoto release] call to your class dealloc method. That should surpress the code analysis warning.
change code:
self.takePhoto = [[UIButton alloc] initWithFrame:CGRectMake(120, 100, takePhotoImg.size.width, takePhotoImg.size.height)];
to
self.takePhoto = [[[UIButton alloc] initWithFrame:CGRectMake(120, 100, takePhotoImg.size.width, takePhotoImg.size.height)] autorelease];
[_takePhoto release] in delloc method is for the retain in setter method. Every time you call self.takePhoto = aNewTakePhote, aNewTakePhote will be retained once.