I thought this would be pretty simple but apparently it isn't, at least not to me. I have a UITextField declared in my .h file and declared its property, as well as an IBAction to clear the text within the field.
IBOutlet UITextField *textField1;
#property(nonatomic, retain) UITextField *textField1;
-(IBAction)clearText:(id)sender
Then in my implementation file (.m) this is how I've written the method to clear the text.
-(IBAction)clearText:(id)sender {
textField1.text = NULL;
}
But nothing happens when I press the button in the simulator. Why?
Did you actually connect the outlet?
Also I'd suggest using nil or #"" instead of NULL
This must work:
textField1.text = #"";
further check if the outled of textFiel1 is connected (black filled circle in XCode)
Add a log statement to
-(IBAction)clearText:(id)sender {
NSLog(#"clearText");
textField1.text = #"";
}
to see if it is called.
If it is not called, check the outled for clearText:(id)sender , too.
Related
I'm actually in big troubles on how to get the Text from a TextField (and optionally a TextView ).
I'll try to make it simple: I have my PostIt.xib which is composed of 2 labels (which I don't really care about) and also one TextField and one TextView. Here is how I tried to get the text from these:
First, in my PostIt.h :
#interface PostIt : UIView {
IBOutlet UITextField *titre;
IBOutlet UITextView *commentaire; }
Then secondly, in my PostIt.m : (the real action of this method is that it close a view and normally throw back the information I want to get to another view, here: parent )
-(IBAction)doubleTap:(UITapGestureRecognizer *)recognizer{
[_parent setTitre:titre.text];
[_parent setCommentaire:commentaire.text];
[_parent setIsEdited:true];
[self removeFromSuperview]; }
My problem here is, when I call a NSLog (for example) to show me the Strings which are caught (probably a mistake here? sorry) it show me every time : (null)
I have been looking and trying a lot of answer i found but no one seems to be able to solve my problem...
If someone could help me it will be really nice, thanks in advance :)
Is there any more code pertaining to the UITextField?
Based on what I see here you need you first convert the input from the UITextField
into a string and then you can set the string where you want.
Updated to add the conversion code,
NSString *stringFromTextField = [yourTextField text];
Here is some more details,
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *yourTextField;
#end
Your Action,
- (IBAction)yourAction:(id)sender {
//Converting UItextfields into strings
NSString *stringFromTextField = [self.yourTextField text];
}
Here is a sample project I made for you on GitHub -
stringFromTextView
You should try to input to your code an object part if that's not already done, where you could directly pick up the data that you need and that's also from here that you should update your view.
I have this code:
- (IBAction)setButtonPressed:(id)sender {
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.TodayExtensionSharingDefaults.santiapps.com"];
NSLog(#"nsuserdef %#", [sharedDefaults objectForKey:#"MyNumberKey"]);
NSLog(#"self.textField.text %#", self.textField.text);
//[sharedDefaults setInteger:[self.textField.text integerValue] forKey:#"MyNumberKey"];
[sharedDefaults synchronize]; // (!!) This is crucial.
NSLog(#"syncd");
}
and for some reason when I tap the button, I get:
-[UIView text]: unrecognized selector sent to instance
but my property is set as:
#property (weak, nonatomic) IBOutlet UITextField *textField;
I get a crash if I uncommment the sharedDefaults setInteger line.
I get a crash if I leave the line where I read the self.textField.text.
I get the final syncd NSLog only if I comment-out the setInteger or self.textField.text NSLog lines.
Why is it thinking textField is a UIView without a text property?
Ok, don't mean to toot my own horn here, but I thought the answer might help someone.
Xcode had incorrectly linked my view controller's UIView to the outlet. And I know some will think I would have done it by being careless, except that I did it as I very seldomly do the connection:
I control dragged from the textfield to the assistant editor's interface where xcode itself created the IBOutlet UITextField for me. So I know it wasn't my mistake. :)
Xcode is buggy. So watch out :-)
I don't know if I have chosen right title for my question.
I have developed an app and now I want to process text before assigning text to UILabel or UIView text property.
instead of
myLabel.text = story.text
do this:
myLabel.text = [story.text substituteCharactersOfText];
substituteCharactersOfText is a method of Category I have added to NSString class
so if I have a lot of label or another views, it will be difficult or errorProne to manually call this category method. (maybe I forgot one for anotherLabel.text)
so is there anyway to call this method automatically before assigning text to UILabel.text?
I think maybe there is way in objective-c I don't aware of (maybe an special use of delegate)!!
Subclass UILabel and override setText:
#implementation HALabel
- (void)setText:(NSString *)text {
[super setText:[text substituteCharactersOfText]];
}
#end
I have the following code:
#interface MyCell : UITableViewCell<UITextFieldDelegate>
{
IBOutlet UITextField *txtFields;
}
- (IBAction)textFieldAction:(id)sender;
#property (nonatomic,retain) IBOutlet UITextField *txtFields;
#end
I also have the following delegate function:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
However, I notice that it's NEVER being called. I set the delegate from the interface builder as well as from code as per: [txtFields setDelegate:self]; but neither seems to work. Is there something else i'm missing for this?
You are obviously using this in conjunction with a UITableView. First, if you want to support user interaction, the txtFields must be a subview of the cell's contentView, not the backgroundView.
Assuming that the txtFields object is a subview of the contentView, then lets look at the connections.
The tableView has a a method cellForRowAtIndexPath: where you either return a new cell or a recycled cell. At the very bottom of that cell, add:
NSLog(#"textFields=%# delegate=%#", cell.txtFields, cell.txtFields.delegate);
assert(cell.txtFields.delegate == cell); // lets make sure this is proper
If in fact both arguments are there, you now know that the txtFields object is in the proper container (contentView), that the property is working, and that the delegate is set to the cell.
If that is all proper and you do not get the keyboard when you tap, then most likely something else is overlaying the txtFields - some other transparent view and its eating the touches.
In that case you should throw together a little demo app using the MyCell class, with even just one hardcoded cell, that demonstrates the problem, then upload that (zipped) to your DropBox account where others like myself can take a look at it and find the problem.
Try removing:
{
IBOutlet UITextField *txtFields;
}
since you have a #property already.
Also, did you #synthesize txtFields;?
I have made an application in which user may generate as many UITextField as he/she wants and those will be automatically placed over a UIView. Functionality is that user may drag the any of the UITextField at any point of screen.Till this part every thing is quite working. Now if he wants to edit the UITextField he taps(2 times) on UITextField and edits.This part of mine is only working for the recent generated UITextField not for all. How can i do this? I fnay one wants my previous code i can post. Plese respond it sonn. Thanks in advance.
add textFieldArray to .h file then specify <UITextFieldDelegate> and
sythesize that array.
while creating the each textfield
specify delegate like this
mytextF1.delegate=self;
mytextF2.delegate=self;
.....
after that add all text field object to an textFieldArray which should be declared .h file and synthesize them .(dont forget to alloc and init this array in ur viewdidload).
self.textFieldArray=[[NSMutableArray alloc]init];
then add each text field to this array
[self.textFieldArray addObject:mytextF1];
[self.textFieldArray addObject:mytextF2];
.......
then use this delegate methode to update
- (void)textFieldDidBeginEditing:(UITextField *)textField{
for (UITextField *textF in self.textFieldArray) {
[textF setText:[textField text]];
}
}