Get the text from a Text Field / Text View - ios

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.

Related

UIView (or UILabel ?) with links to another View in the storyboard

I need to have a text part in my iOS application (in Objective C), with different names written. Each name must be linked to a view with the person's informations.
I don't know how to do that, what to use to be able to generate multiple links in one text, and link each name do the right action. So when I click on the link it should send the name I clicked on to required action.
Anyone knows how to do that with UIViews ? Or UILabels ? or anything..
Also, whenever I put a UIView in my View, it takes longer to load, do you know why ?
Thanks.. Hope that was clear enough !
HermyKa
You can use RTLabel for that,
Add this library in your code and than follow this step,
In .h file add this code
//Import RTLabel
#import "RTLabel.h"
// Add delegate
#interface ViewController : UIViewController<RTLabelDelegate>
#end
And in .m file
- (void)viewDidLoad {
RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-262)/2, (self.view.frame.size.height-203)/2, 262, 203)];
label.delegate = self;
// You have to create link for each name
NSString *searchString = #"Dilip Dev Ram";
}
//RTLabel Delegate Method
- (void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url
{
//When user click on one of the name this method will called and url will return the name which user has tapped. You can add condition on name that which view will display.
NSLog(#"did tap on name %#", url);
}
You can use the set of the UILabels with gesture recognizer connected to each. Or the set of UIButtons.
http://www.raywenderlich.com/104744/uigesturerecognizer-tutorial-creating-custom-recognizers

How to ensure a UIView has loaded?

This may sound silly, but read on...
I want to set the text of a UILabel from outside of a UIViewController that is instantiated by a storyboard. I need to make sure that the label property of the view controller is set when I set its text otherwise the label's text won't be set(because it won't be loaded yet to receive a text value).
Here's my current solution:
// Show pin entry
if (!self.pinViewController) {
// Load pin view controller
self.pinViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"pinScreen"];
self.pinViewController.delegate = self;
if (!self.pinViewController.view) {
// Wait for pin screen to fully load
}
[self.pinViewController setMessageText:#"Set a pin for this device"];
}
Initially I had a while loop that looped until the value of view was not nil, But it seems the very act of checking the view loads it(as mentioned here: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW37)
I tried using the isViewLoaded method with no success. It just looped forever.
I've gone forward with the above code as my current solution, but it feels wrong.
Is there a better way ensure a UIView has loaded?
I want to propose an alternative way where you don't have to rely on the availability of the view.
If you need to wait for the view to load before you can call other methods on your viewController you break encapsulation, because the viewController that calls your PinViewController has to know about the inner workings of your PinViewController. That's usually not a good idea.
But you could save objects like NSStrings in the PinViewController instance, and when the view of the PinViewController will appear you set its views according to the properties you have set before.
If you need to change the text of an label from outside your viewController you can also create a custom setter that sets the label.text for you.
Your .h
#interface PinViewController : UIViewController
#property (copy, nonatomic) NSString *messageText;
// ...
#end
And your .m
#implementation PinViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.messageLabel.text = self.messageText;
}
// optional, if you want to change the message text from another viewController:
- (void)setMessageText:(NSString *)messageText {
_messageText = messageText;
self.messageLabel.text = messageText;
}
// ...
#end
viewDidLoad should solve this I guess.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
I would rather see you change your logic and do it the way that #MatthiasBauch shows in his answer. However, to answer your actual question, you can simply set a view property in order to force it to load:
self.pinViewController.view.hidden = NO;

How to clear the text within a UITextField using an IBAction?

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.

UITextField Delegate not working

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;?

Editing multiple dynamically generated UITextFields at runtime

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

Resources