set UITextField as non editable - Objective C - iphone

[Number.editable = NO];
[Number resignFirstResponder];
[Password.editable = NO];
[Password resignFirstResponder];
I am getting the error
Request for member 'editable' in something not a structure or union
:S
Thanks

Firstly, the [...] aren't needed if you're not sending a message.
Number.editable = NO;
[Number resignFirstResponder];
Password.editable = NO;
[Password resignFirstResponder];
But this is not the cause of error. The .editable property is only defined for UITextView, not UITextField. You should set the .enabled property for a UITextField (note that a UITextField is a UIControl).
Number.enabled = NO;
...

Also, you can use the delegate methods.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
That would do the trick, I prefer this method over setting textField.enabled = YES when it's likely that the ability to edit will change during the lifecycle of the app.

textField.userInteractionEnabled = NO;
Hope this helps..

Returning NO from shouldChangeCharactersInRange would be better choice because if text is longer than the textfield width, then above solution will give problem, because user won't be able to see all text(i.e. text hidden beyond the text field width)

Related

UITextView alignment is changing on its own

I have a UITextView with RTL alignment.
If the user enters text, and dismisses keyboard, the alignment remains - RTL.
But if the user chooses empty string, I change the string to the place holder, and the alignment flips to be LTR.
I tried to explicitly ask for RTL, but it didn't help.
-(void)cancelPad{
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
userTextView.text = #"place holder text";
}
-(void)doneWithNumberPad{
if ([userTextView.text isEqualToString:#""]) {
[self cancelPad];
}
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
}
Somebody have any ideas?
Instead of:
userTextView.text = ""
you should put:
userTextView.text = [NSString stringWithFormat:#"\u202b"];
This is the UTF-8 character for "starting RTL"
Eventually I just added a label with the place holder text.
When the keyboard is shown, the placeholderLabel is hidden.
If the keyboard is dismissed with empty text, the placeholderLabel is shown again.
If there is a more proper solution, do not hesitate to post it here.
first:
in the .h file add this
UIViewController<UITextFieldDelegate>
assign your text field to this
userTextView.delegate =self;
then use this methods and put your code in
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
}
Example :
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if ([userTextView.text isEqualToString:#""]) {
[self cancelPad];
}
[userTextView resignFirstResponder];
userTextView.textAlignment = UITextAlignmentRight;
}

simple question about UITextField and not working secureTextEntry

I have a password field with text "Password" and when user clicks, it gets cleared. Moreover I want to set the type of this textfield as secure but this doesn´t work.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([textField.text isEqualToString:#"Password"] || [textField.text isEqualToString:#"Email"])
{
textField.text = #"";
textField.secureTextEntry = YES;
}
}
I've just had the same problem. I was changing the property from within the textFieldDidBeginEditing: call.
Moving the property change to the textFieldShouldBeginEditing: call fixed the problem. I believe the trick is to change it while the text field isn't the becomeFirstResponder.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:_passwordField]) {
textField.secureTextEntry = YES;
}
return YES;
}
Simply put your text in a placeholder and make your password textfield secure. Your problem will be solved :).
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([textField.text isEqualToString:#"Password"] || [textField.text isEqualToString:#"Email"])
{
textField.text = #"";
[textField resignFirstResponder];
textField.secureTextEntry = YES;
[textField becomeFirstResponder];
}
}
it can solve your problem , but it has a big bug. the shit UITextField .
set the property in interface builder or when you initialize textfield remove the line from
-(void)textFieldDidBeginEditing:(UITextField *)textField {
}
I think you should set placeholder text as email or password and pre select the secure text entry from interface builder.That's it...
I realize this is a little old, but in iOS 6 the UITextField "text" is now by default "Attributed" in Interface Builder. Switching this to be "Plain", which is how it was in iOS 5, fixes this problem.
If your keyboard is present, secureTextEntry won't work. Because the UITextInputTraits Protocol doesn't change the textfield if the keyboard shows.
You can dismiss the keyboard, and it will work
[myTextField resignFirstResponder]

UITextfield clear button

How can I clear the field which currently has the cursor placed inside it?
I have tried:
if(textfield.isEditing)
textfield.text = #"";
This works for me, but if I select all 3 fields, and press the 'clear field button' all the fields clear Also instead of isEditing, I have tried to use tags on the UITextfields and do :
if(textfield.tag == 1)
textfield.text = #"";
but this has the same effect.
How can I solve this problem?
try one or both of these...
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.clearsOnBeginEditing = YES;
Then add delegate...
- (BOOL)textFieldShouldClear:(UITextField *)textField {
return YES;
}
Try out this in TextField's Delegate method didBeginEditing:
if ([textfield isFirstResponder]) {
textfield.text = #"";
}
Hope this helps you.

Add 'Done' and 'New line' buttons on keyboard in iPhone application

I have created a window based application with a UITabbarController as the RootViewController.
In one of the tabs, i have provided UITextField and UITextView.
I want to provide two buttons on the keyboard itself:
Done - which will hide the keyboard.
Enter - for new line.
Please post your answer if anybody has some idea how to do it.
For the UITextField you can change the return key to a done key by setting the following:
targetTextField.returnKeyType = UIReturnKeyDone;
However, you won't be able to have a Enter and Done key at the same time without custom addition of views to the keyboard.
Also, to control the done behavior of the keyboard you have to implement a UITextFieldDelegate method:
targetTextField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES; //dismisses the keyboard
}
I know you can set the returnKeyType for a UITextView but I'm not sure if you can manipulate the return key behavior.
You have a tutorial on how add subviews to the iPhone keyboard here :
http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/7350-adding-subviews-custimize-keyboard.html
Hope this helps,
Vincent
For some reason return YES; didn't work on its own. that worked for me :
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.returnKeyType == UIReturnKeyNext) {
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
}
}
if (textField.returnKeyType == UIReturnKeyDone) {
[textField resignFirstResponder];
}
return YES; //dismisses the keyboard
}

Can I hook into UISearchBar's Clear Button?

I've got a UISearchBar in my interface and I want to customise the behaviour of the the small clear button that appears in the search bar after some text has been entered (it's a small grey circle with a cross in it, appears on the right side of the search field).
Basically, I want it to not only clear the text of the search bar (which is the default implementation) but to also clear some other stuff from my interface, but calling one of my own methods.
I can't find anything in the docs for the UISearchBar class or the UISearchBarDelegate protocol - it doesn't look like you can directly get access to this behaviour.
The one thing I did note was that the docs explained that the delegate method:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
is called after the clear button is tapped.
I initially wrote some code in that method that checked the search bar's text property, and if it was empty, then it had been cleared and to do all my other stuff.
Two problems which this though:
Firstly, for some reason I cannot fathom, even though I tell the search bar to resignFirstResponder at the end of my method, something, somewhere is setting it back to becomeFirstResponder. Really annoying...
Secondly, if the user doesn't use the clear button, and simply deletes the text in the bar using the delete button on the keyboard, this method is fired off and their search results go away. Not good.
Any advice or pointers in the right direction would be great!
Thanks!
Found the better solution for this problem :)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText length] == 0) {
[self performSelector:#selector(hideKeyboardWithSearchBar:) withObject:searchBar afterDelay:0];
}
}
- (void)hideKeyboardWithSearchBar:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
}
The answer which was accepted is incorrect. This can be done, I just figured it out and posted it in another question:
UISearchbar clearButton forces the keyboard to appear
Best
I've got this code in my app. Difference is that I don't support 'live search', but instead start searching when the user touches the search button on the keyboard:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
if ([searchBar.text isEqualToString:#""]) {
//Clear stuff here
}
}
Swift version handling close keyboard on clear button click :
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.characters.count == 0 {
performSelector("hideKeyboardWithSearchBar:", withObject:searchBar, afterDelay:0)
}
}
func hideKeyboardWithSearchBar(bar:UISearchBar) {
bar.resignFirstResponder()
}
You could try this:
- (void)viewDidLoad
{
[super viewDidLoad];
for (UIView *view in searchBar.subviews){
for (UITextField *tf in view.subviews) {
if ([tf isKindOfClass: [UITextField class]]) {
tf.delegate = self;
break;
}
}
}
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
// your code
return YES;
}
I would suggest using the rightView and rightViewMode methods of UITextField to create your own clear button that uses the same image. I'm assuming of course that UISearchBar will let you access the UITextField within it. I think it will.
Be aware of this from the iPhone OS Reference Library:
If an overlay view overlaps the clear button, however, the clear button always takes precedence in receiving events. By default, the right overlay view does overlap the clear button.
So you'll probably also need to disable the original clear button.
Since this comes up first, and far as I can see the question wasn't really adequately addressed, I thought I'd post my solution.
1) You need to get a reference to the textField inside the searchBar
2) You need to catch that textField's clear when it fires.
This is pretty simple. Here's one way.
a) Make sure you make your class a , since you will be using the delegate method of the textField inside the searchBar.
b) Also, connect your searchBar to an Outlet in your class. I just called mine searchBar.
c) from viewDidLoad you want to get ahold of the textField inside the searchBar. I did it like this.
UITextField *textField = [self.searchBar valueForKey:#"_searchField"];
if (textField) {
textField.delegate = self;
textField.tag = 1000;
}
Notice, I assigned a tag to that textField so that I can grab it again, and I made it a textField delegate. You could have created a property and assigned this textField to that property to grab it later, but I used a tag.
From here you just need to call the delegate method:
-(BOOL)textFieldShouldClear:(UITextField *)textField {
if (textField.tag == 1000) {
// do something
return YES;
}
return NO;
}
That's it. Since you are referring to a private valueForKey I can't guarantee that it will not get you into trouble.
Best solution from my experience is just to put a UIButton (with clear background and no text) above the system clear button and than connect an IBAction
- (IBAction)searchCancelButtonPressed:(id)sender {
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
// some of my stuff
self.model.fastSearchText = nil;
[self.model fetchData];
[self reloadTableViewAnimated:NO];
}
Wasn't able to find a solution here that didn't use a private API or wasn't upgrade proof incase Apple changes the view structure of the UISearchBar. Here is what I wrote that works:
- (void)viewDidLoad {
[super viewDidLoad];
UITextField* textfield = [self findTextFieldInside:self.searchBar];
[textfield setDelegate:self];
}
- (UITextField*)findTextFieldInside:(id)mainView {
for (id view in [mainView subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
return view;
}
id subview = [self findTextFieldInside:view];
if (subview != nil) {
return subview;
}
}
return nil;
}
Then implement the UITextFieldDelegate protocol into your class and overwrite the textFieldShouldClear: method.
- (BOOL)textFieldShouldClear:(UITextField*)textField {
// Put your code in here.
return YES;
}
Edit: Setting the delegate on the textfield of a search bar in iOS8 will produce a crash. However it looks like the searchBar:textDidChange: method will get called on iOS8 on clear.