UITableViewCell with TextField and checks input - iphone

I am currently create a UITableViewCell with a UITextField in it.
On click of the text field, I want to bring up a number keyboard that I created. And as I type, the textfield should check the input for me; on click of other place, the keypad should be dismissed.
Code:
UITableViewCell *sizeCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"sizeCell"];
sizeCell.textLabel.text = #"Size";
UITextField* sizeField = [[UITextField alloc] initWithFrame:CGRectMake(185, 10, 100, 28)];
sizeField.text = #"0";
sizeField.textAlignment = UITextAlignmentRight;
sizeField.textColor = [UIColor colorWithRed:50.0/255.0 green:79.0/255.0 blue:133.0/255.0 alpha:1.0f];
sizeField.backgroundColor = [UIColor clearColor];
sizeField.keyboardType = UIKeyboardTypeDecimalPad;
[sizeCell.contentView addSubview:sizeField];
rows = [[NSArray arrayWithObjects:switchCell, typeCell, sizeCell, nil] retain];
I tried to implement UITextFieldDelegate like:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[sizeField resignFirstResponder];
return YES;
}
but the keyboard doesn't go away...
How do I validate the input and dismiss the keyboard?

You never set the delegate on your textfield so that textFieldShouldReturn: gets called. Make sure your class conforms to UITextFieldDelegate and then do the following:
...
UITextField* sizeField = [[UITextField alloc] initWithFrame:CGRectMake(185, 10, 100, 28)];
sizeField.delegate = self; //This is important!
sizeField.text = #"0";
...

A few observations:
As another poster suggested, make sure you set the keyboard's delegate correctly.
If you want to dismiss the keyboard on keyboard return, make sure you have one on your custom keyboard and it's correctly set up to call the ...ShouldReturn method.
If you want to dismiss on taps outside, you'll have to do that on your own.
You are declaring sizeField inside the method where you are setting it up, then calling it from another method outside that scope. I assume you have a class variable called sizeField or you'd be getting a compiler error. However, declaring it again when you're setting it up like you do shadows the class variable declaration so it never gets set up. Incidentally, that's a memory leak.
This shouldn't affect the actual running of the program if all else is correct (but it will if, e.g. 4 is the problem and not fixed), but I think it's better form to call [textField resign...] instead of [sizeField resign...]. At the least you should assert(textField == sizeField).

Related

how to disable the actionsheet button in ios

In my project i have created textfield using actionsheet button.how can i disable the userinterface after first creation.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
text1 = [[UITextField alloc] initWithFrame:CGRectMake(x2,y2+3, 300, 25)];
text1.backgroundColor = [UIColor whiteColor];
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
text1.font = [UIFont systemFontOfSize:15.0f];
text1.placeholder=#"ENTER HOME LOAN INTEREST";
[text1 setKeyboardType:UIKeyboardTypeNumberPad];
text1.textAlignment=NSTextAlignmentCenter;
text1.userInteractionEnabled=YES;
[scrollview addSubview:text1];
scrollview.contentSize=CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+to);
to+=to;
y2+=30;
img6.frame=CGRectMake(0, y2+4, 320, 60);
y2=y2+62;
img7.frame=CGRectMake(0, y2+5, 320, 60);
y2=y2-62;
}
else if (buttonIndex==1)
{
text2 = [[UITextField alloc] initWithFrame:CGRectMake(x2, y2+3, 300, 25)];
text2.backgroundColor = [UIColor whiteColor];
text2.clearButtonMode = UITextFieldViewModeWhileEditing;
text2.font = [UIFont systemFontOfSize:15.0f];
text2.placeholder=#"ENTER EDUCATION EXPENSE";
[text2 setKeyboardType:UIKeyboardTypeNumberPad];
text2.userInteractionEnabled=YES;
text2.textAlignment=NSTextAlignmentCenter;
[scrollview addSubview:text2];
scrollview.contentSize=CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+to);
to+=to;
y2+=30;
img6.frame=CGRectMake(0, y2+4, 320, 60);
y2=y2+60;
img7.frame=CGRectMake(0, y2+5, 320, 60);
y2=y2-60;
}
}
how can i prevent user from creating same textfield many time
It looks to me as if the two textfields are the same, with the exception of the placeholder, and presumably what you do with the value later. You could easily use a single textfield that is accessible as a property in your class. If you are using storyboards, it's possible that you can place the textfield in your storyboard and perhaps set it to 'hidden'.
Once you have a common text field, you can simply set the placeholder text and make the field visible inside the actionsheet delegate method. If there are other differences in the layout, then those too can be handled at this point.
This approach would need a way to hold 'state' so that you know the context of the field later on. You could manage this by examining the placeholder text, but this would be fragile because the text may change on a whim. A better approach would be to set a tag value e.g. text2.tag = 1 for home load interest and text2.tag = 2 for education expense. You can later access the tag value to interpret the context.
Because there is only one field, and because it is accessible by a property, you can later remove the textfield from the superview, or hide it again and use another flag to know that it has been displayed to the user. Most likely you can check that you have a value for either home loan interest or education expense, in which case, don't show it again.
You can hide/remove/disable the button that invokes the actionsheet from within the actionsheet delegate callback method too, again you will need a property in order to access it.

How to assign UItextfield property to another UItextfield

I want to transfer a UITextField properties to another UITextField. How can I do so?
Here's my complete scenario:
I have created a custom cell in which I have a UITextField.
Now when I'm using this UITextField in my UITableView i want to assign already existing
UITextField's property to this cell's UITextField. I tried
cell.myCustomTextField = myAlreadyCreatedTextField;
Also tried to transfer individual properties like
cell.myCustomTextField.borderStyle = myAlreadyCreatedTextField.borderStyle;
but this also didn't work.
Please help
If you need multiple UITextFields with the same properties you can make an initializer like this.
myField = [self fieldWithLabel:#"Placeholder" withTextField:myField withFrame:CGRectMake(0,0,260,45) withKeyboard:UIKeyboardTypeNumberPad];
//
-(UITextField *)fieldWithLabel:(NSString *)name withTextField:(UITextField *)field
withFrame:(CGRect)frame withKeyboard:(UIKeyboardType)keyboard
{
CGRect fieldFrame = frame;
field = [[UITextField alloc] initWithFrame:fieldFrame];
field.placeholder = name;
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.textAlignment=UITextAlignmentLeft;
field.userInteractionEnabled = YES;
field.enabled = YES;
field.enablesReturnKeyAutomatically= NO;
field.clearsOnBeginEditing = NO;
field.delegate = self;
field.returnKeyType = UIReturnKeyDone;
field.keyboardType = keyboard;
return field;
}
Controls (like NSTextField) each have an associated cell which
contains the display attributes of the control. NSCell and its
subclasses implement copying, so you can copy the cell instead of the
view (which does not support NSCopying).
Alternatively, NSViews support NSCoding, so generally speaking if you
have a view or subclass that you need to replicate, one way to do it
is to archive it and then unarchive it for however many "copies" you
might need.
From Source.
IN SHORT : It is not possible to assign a property of one control to another control.
Because Apple/iOS does not provide this type of feature.
But you can get text of one UITextField to another UITextField, by
textField1.text = textField2.text;

How to make tableview cell "detailLabel" editable?

I am trying to implement a suggestion offered by already answered question, but I am stuck on getting it to work, so looking for some help.
Here is the post I am trying to implement: http://stackoverflow.com/a/3067579/589310
I am trying to use the solution offered by "ridale" on how to make a "detailLabel" editable as part of a TableView. I hope it will allow me to directly edit a cell and enter a number. It doesn't seem too common as a UI, but "SmartRecord" does it and I want to emulate it.
Here is the only line that gives me an error:
UITextField *tmpView = [self detailLabel:indexPath];
I get this error:
Instance method -detailLabel: not found (return type defaults to 'id')
I assume it is because my self is different than the original poster.
I added a TableView directly to my existing controller. It is not a TableViewController directly:
#interface EditViewController : UIViewController
{
IBOutlet UITableView *tableSettings;
}
I can fill the table and interact with it, so I know it works (at some level anyway).
I have tried changing self to my table control or the cell directly:
UITextField *tmpView = [tableSettings detailLabel:indexPath];
I can't find anything that responds to the "detailLabel" method.
I am also not sure if the proposed solution is complete or uses more code not shown.
This is the only error I get, so I am hopeful once I solve it, it will work ;-)
-(UITextField*)detailLabel:(NSIndexPath*)indexPath is not present in UITableView or any other classes provided by Apple.
The frame position is just for sample. Modify that such that it would come in place of your detailLabel.
You have to write your own method which returns a UITextField, something as following
-(UITextField*)detailLabel:(NSIndexPath*)indexPath
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textField.delegate = self;
//assuming myList is array of NSString
NSString* str = [myList objectAtIndex:indexPath.row];
textField.text = str;
return [textField autorelease];
}

UITextField isn't working

am new to objective-c programming, so sorry if it's a silly question but i looked thoroughly and couldn't find the error,
p.s: english isn't my first language so programming keywords might be unintentionally used
I've set up a txtfield and defined some of it's properties, such as the keyboard-number pad and stuff, the problem is, when i run it on the simulator, nothing happens, the defult keyboard is called.
i'm just trying to do simple stuff to get it in my head,
in my .h, i have
#interface practiceViewController : UIViewController <UITextFieldDelegate>
{
UITextField *userInput;
}
#property (nonatomic, retain, readwrite) UITextField *userInput;
#end
and in my .m
-(UITextField *)userInput{
if (userInput == nil) {
userInput=[[UITextField alloc]init];
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
}
return userInput;
}
i also tried to make it as an input, but for some reson it's not working
int nInput=[[userInput text] intValue];
whats pissing me off about this is that i did the same in a previous project and the "input worked" only, but now everything isn't.
and my property as i haven't defined anything.
i appreciate your help
I Think you have to set the frame for UITextField like this ...
userInput.frame=CGRectMake(100,100,60,31);
in your -(UITextField *)userInput method, that's y it didn't show ..
Well few things are hazy but ...
You are getting the value of the text field using,
int nInput = [[userInput text] intValue];
If you are not getting the correct value then nInput will be zero. That is because userInput must be nil. You can verify this by logging it by adding NSLog(#"%#", userInput); before the line above.
Whatever value is printed, it looks like your on screen text field is not referenced to by userInput.
Since you're doing the entire thing programmatically, you will have to add your userInput to the view hierarchy by doing this,
[self.view addSubview:self.userInput];
Using the property accessor is important here is important as otherwise userInput would be nil unless you've accessed it using self.userInput earlier.
Although this is tagged iPhone, do you happen to be simulating an iPad? If so, I have run into this. The iPad will not show a number pad keyboard.
userInput= [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, 30)]
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
You can just set the frame and add below property(to visible the textfield frame in View otherwise it is not visible in View).
userInput.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:userInput];
I hope it will be helpful to you.

Having Problem with dynamically created UITextField in iphone sdk

In my iphone app, I have created some UITextField in scrollView dynamically. And added one button in xib. On that button's TouchUpInside event, I opened UIImagePickercontroller to open photo library and taking selected image on UIImageView.
Now when modalview is dissmissed, values in my UITextField disappears.
How do I retain the UITextField values?
txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];
[txt addTarget:self action:#selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
txt.textColor = [UIColor blackColor];
txt.borderStyle = UITextBorderStyleBezel;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[fieldArray addObject:txt];
Using for loop I am adding txt to NSMutableArray fieldArray.
Here is the code where I fetch values from TextFields
NSMutableArray *insertValues = [[NSMutableArray alloc]init];
//Taking values from textFields
for (int i=1; i<=nooffields; i++) {
UITextField *tf = (UITextField *)[self.view viewWithTag:i];
NSLog(#"TF : %#",tf.text);
if (tf.text.length<=0) {
tf.text=#"";
}
[insertValues addObject:[NSString stringWithFormat:#"'%#'",tf.text]];
}
EDIT :
And also when I display values from database in this textFields and try to edit it. It gives me null values.
What may be the reason? If there any other alternatives? please help me
UITextField *textField=[[UITextField alloc]init];
[textField setFrame:CGRectMake(x,y, width, hight)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];
The information that your scrollview uses to get populated should be stored outside the scrollview, in a NSMutableDictionary for example. So everytime cellForRowAtIndexPath gets called it can retrieve the information from that source. So I advise you to store the UITextField's text within the dictionary. You can index it by its tag number. That way you won't loose whatever it contained.
I hope it helps you
Cheers
I finally got a work around for my problem.
I am temporarily storing my textField's values into database when presentModalViewController is called.
These values I retrieve back into the textFields on viewWillAppear.
This may not be a very efficient solution but it worked in my case.
Hope this helps someone.
Thanks for all your responses.