UITextField isn't working - iphone

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.

Related

objective-c: Delegate object argument getting overwritten when i create multiple instances of custom class

EDIT: I apologize for wasting time, the erorr had nothing to do with what I'm taking about but rather some logic in my code that made me believe this was the cause. I'm awarding Kevin with the correct answer since using his idea to pass the whole AuthorSelectionView, and his note on correcting the NSNumer mistake. Sorry about that.
I've been trying to figure this out for hours, and even left it alone for a day, and still can not figure it out...
My situation is as follows:
I've created a custom class that implements 'UIView' and made this class into a protocol as follows:
custom UIView h file
#protocol AuthorSelectionViewDelegate <NSObject>
-(void)AuthorSelected:(NSNumber *)sender;
#end
#import <UIKit/UIKit.h>
#interface AuthorSelectionView : UIView
#property (nonatomic,assign) id<AuthorSelectionViewDelegate> delegate;
#property (strong,retain) NSNumber *authorID;
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID ;
#end
the implementation...
- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID
{
self = [super initWithFrame:frame];
if (self) {
self.authorID = [[NSNumber alloc] initWithInt:authorID]; //used to distinguish multiple instances of this class in a view.
...
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)];
[button addTarget:self action:#selector(CUSTOMBUTTONCLICK) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
- (void) CUSTOMBUTTONCLICK
{
[self.delegate performSelector:#selector(AuthorSelected:) withObject:self.authorID];
}
Now the method in my delegate object gets called just fine, but my major problem here is that something is going on with the object being pass through when i have multiple instances of the AuthorSelected class alloc'd.. (the NSNumber authorID). I'm getting some weird behavior with it. It seems almost random with the value being passed, but i'm detecting some pattern where the value passed through is coming up late..
thats confusing so ill try to explain:
I create two instances of the AuthorSelected view, one with authorID=1 and the other with authorID=2.
On the first press, lets say i press the first button, i'll get 1 as expected.
On the second press, if I press the 1st custom button, i'll get '1', but if i press the second i'll still get 1.
On the third go, either button will give me back '2'
I feel like this is some issue with pointers since that has always been a weak point for me, but any help would be greatly appreciated as I can not seem to figure this one out.
Thank you!
EDIT:
as requested here is how I create the AuthorSelectionView Objects...
AuthorSelectionView * asView01 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic1
withLabel:randomUserName
withID:1];
asView01.delegate = self;
AuthorSelectionView * asView02 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic2
withLabel:randomUserName2
withID:2];
asView02.delegate = self;
A detail that may be important:
As soon as i click on one of these custom views, my code is set to (for now) call the method that runs the above AuthorSelectionView alloc code, so that i can refresh the screen with the same layout, but with different userpic/userName. This is poor design, I know, but for now I just want the basic features to work, and will then worry about redrawing. I metion this tidbit, becuase I understand that objective-c 'layers' veiws on top of eachother like paint on a canvas, and had a thought that maybe when I click what I think may be my 2nd button, its really 'clicking' the layer beneath and pulling incorrect info.
Your description of the problem is a bit confusing, but this line in your init is very clearly wrong:
self.authorID = [self.authorID initWithInt:authorID];
In -init, your property self.authorID defaults to nil, so the expression [self.authorID initWithInt:authorID] is equivalent to [nil initWithInt:authorID], which evaluates back to nil. So you should actually be seeing nil in your action. You probably meant to say self.authorID = [NSNumber numberWithInt:authorID]
You're missing the alloc message, so this message:
self.authorID = [self.authorID initWithInt:authorID];
Is sent to a nil target, because self.authorID hasn't been allocated yet.
So first allocate it, then use the init method, or mix these two messages. A faster syntax allows to do it this way:
self.authorID= #(authorID);
EDIT
I don't see where you initialize the delegate, that method shouldn't even be called if you haven't initialized it. Show the code where you create the AuthorSelectionView objects and set the delegates.
instead of :
self.authorID = [self.authorID initWithInt:authorID];
put :
self.authorID = [NSNumber numberWithInt:authorID];
or
self.authorID = [[NSNumber alloc] initWithInt:authorID];
EDIT :
Don't you have errors or warnings in your code ? I can't see you returning self object in the init method ("return self;")

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

UITableViewCell with TextField and checks input

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

UITextField settext not working

This is frustrating the hell out of me. I am a beginner programmer and cannot figure out why the text is not being changed.
Here is my method which is supposed to set the text of a UITextField:
-(void)updateDays:(NSInteger)days
{
NSString* daysString = [[NSString alloc] initWithFormat:#"%d", days];
[daysTextField setText:daysString];
[daysString release];
}
For whatever reason nothing is happening.
Help appreaciated!
Anytime you have a frustration along the lines of "why isn't this line working", use the debugger or just add an NSLog before it to print out the relevant data:
NSLog(#"updateDays: %# %# <= %#", daysTextField, daysTextField.text, daysString);
Then you know the line (a) is getting executed, (b) the variables are the ones you think they are, and (c) the values are reasonable.
I have experienced this several times, but I think all of them was reasoned by that the UITextField was not initialized at that point.
Set a breakpoint and use debugger to make sure that your UITextField is not nil. You should also check the connection between the .xib file and your code.
Check if the method is called. If yes check if the textfield is set up properly. Cross check to see if the IBOutlet connections are made to the correct object.
I meet this problem too .
It's my code:(in viewdidload)
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,50)]];
tv.text = #"12345";
tv.textColor=[UIColor blackColor];
[self.view addSubView:tv];
And the text(12345) doesn't show;
Lastly,I found that when i set textColor to gray,or any color not black,it works;
I think that it's a bug of the simulator.
I use xcode 4.2 and the iphone5.0simulator.

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.