How I can get the value of my UITextField? - objective-c

how I can get the value of my UITextField ? When I declare my UITextField in the Storyboard, I know but like this, I don't know.
(sorry for my English, I'm French)
Thank you in advance for your answer.
//Ajout d'un Text Field
CGRect rectTF = CGRectMake(10,70,100,20); // Définition d'un rectangle
UITextField *articleSaisi = [[UITextField alloc] initWithFrame:rectTF];
articleSaisi.borderStyle = UITextBorderStyleLine;
articleSaisi.placeholder = #"Article";
[self.view addSubview: articleSaisi];

I am not quite sure whether you want to access the value or you want to associate your UI element to your code:
Try this if you are saying that you want to access the text value.
atricleSaisi.text
Try to control drag the UI element to either your corresponding .h or .m file so that it can create IBOutlet for you if you are saying that you want to connect your UI element to your code.
Highly recommend you go check the documentation.

if you want to know when the user pressed return, as per your comment, then you should create a UITextViewDelegate for your text view and define its textFieldShouldReturn method:
Discussion
The text field calls this method whenever the user taps the return button. You can use this method to implement any custom behavior when the button is tapped.
Also, give a look at textFieldDidEndEditing, which is called whenever there is a focus change and you should define to correctly handle user input.
Old answer:
If I do not understand you incorrectly, you want to create a UITextField programmatically (i.e., not through a Storyboard).
In this case, you should put you initialisation code inside the viewDidLoad method of your view controller and make sure that you define a property for the text field instead of a local variable:
#interface MyViewController : UIViewController
....
#property (nonatomic, strong) UITextField* articleSaisi;
...
#end
- (void)viewDidLoad {
CGRect rectTF = CGRectMake(10,70,100,20); // Définition d'un rectangle
self.articleSaisi = [[UITextField alloc] initWithFrame:rectTF];
self.articleSaisi.borderStyle = UITextBorderStyleLine;
self.articleSaisi.placeholder = #"Article";
[self.view addSubview:self.articleSaisi];
}
If you do so, you can access the text field value from any other method in the view controller through its text property:
self.articleSaisi.text

Related

Set textField.text from other class of my app

I am trying to solve my problem with searching in Developer Library but probably i dont know for what should i search.
I have HeadViewController.h where i set property of my IBOutlet textField.
In that view i have another view (containerView) with ContainerViewController class.
My problem is that i cant set textField text in my HeadViewController even if i try to set property of my HeadViewController class in my ContainerViewController.h
Here is what am i trying to:
ContainerViewController.h
#class HeadViewController;
#property HeadViewController *hvc;
ContainerViewController.m
#import "HeadViewController.h"
// in my IBAction method
self.hvc.textField.text = #"text from container view";
I also trying to use [textField setText:#""] and [textField inserText:#""] but with same result. If anyone know where is my problem or for what i have to search in Developer Library i will appreciate that.
You need to set the value of your property, hvc. hvc is a pointer, but just declaring it doesn't make it point to anything, it just says what class of object it should point to. Since ContainerViewController is embedded in a container in HeadViewController's view, that makes HeadViewController the parent. So you can reference HeadViewController from ContainerViewController by using self.parentViewController.
// in my IBAction method
self.hvc = (HeadViewController *)self.parentViewController;
self.hvc.textField.text = #"text from container view";

In Xcode, make a button look like a text field or a textfield look like a button

In Xcode (iOS), I have a screen with a combination of buttons and text fields. However, they look different. Is there a way that I can have a button look like a textfield? Or is there a way I can make a textfield act like a button (perform some action when pressed and don't bring up the keyboard)?
Thanks
You can try one of these:
place a transparent (custom with no image) button on top of the text field and attach the desired action to it
show your view in the implementation of the textFieldShouldBeginEditing: UITextFieldDelegate protocol method
Subclassing is also an option, but it will probably complicate your task too much.
If You want to perform an action and do not bring up the keyboard when pressed on UITextField You can do it like this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// do here everything you want
NSLog(#"Pressed on TextField!");
[self.view endEditing:YES]; // Hide keyboard
return NO;
}
This function will be called every time when pressed on textField.
** Don't forget to delegate UITextField.
Note: With this You will face the problem: if keyboard is shown it will not hide, so You need also add -endEditing:YES.
You need to custom one of them ... or both
Exemple : create a class CustomTextField : UITextField
in this class use :
#import <QuartzCore/QuartzCore.h>
Play with style
Late answer, but nevertheless. I did this to make a UIButton look like a UITextField
#property (weak, nonatomic) IBOutlet UIButton *resolutionButton;
And in viewDidLoad:
dispatch_async(dispatch_get_main_queue(), ^{
[[self.resolutionButton layer] setBorderColor: [UIColor colorWithWhite:0.6f alpha:0.6f].CGColor];
self.resolutionButton.layer.cornerRadius = 5.0f;
self.resolutionButton.layer.borderWidth = 0.5f;
}

Custom Keyboard on iOS: How do I access the UITextField?

I have a UIView subclass that I assign to a text field as follows:
self.textField.inputView = [[HexKeyboard alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
and this works (i.e., the keyboard comes up). However, how should the HexKeyboard instance know about the textField?
[Of course I can add a property to the HexKeyboard to achieve this (and call it delegate), but I figure there's a built-in mechanism for this...]
You don't really need a complex delegate pattern for this. Just create a property of type UITextField on your HexKeyboard class, and make it an unsafe_unretained reference so you don't get a retain loop:
#interface HexKeyboard
#property (nonatomic, unsafe_unretained) UITextField *textField;
#end
Then set it when you set your inputView:
self.textField.inputView = [[HexKeyboard alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.textField.inputView.textField = self.textField;
There seems to be no built-in mechanism for this, as the other answerers have pointed out. As Nick says, you don't need a complex delegate pattern for this. Or rather, you use the delegate pattern, but you get the delegate class for free. In this case it's the UITextInput protocol.
So your keyboard probably looks like this (and has a NIB)
#interface ViewController : UIViewController
// use assign if < iOS 5
#property (nonatomic, weak) IBOutlet id <UITextInput> *delegate;
#end
When you create the keyboard controller, you assign the UITextInput conformer to it, so something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
HexKeyboardController *keyboardController = [[HexKeyboardController alloc] initWithNibName:#"HexKeyboardController" bundle:nil];
self.textField.inputView = keyboardController.view;
keyboardController.delegate = self.textField;
}
However, I thought, there MUST be a way to define this keyboard just once and get the keyboard to "automatically know" who the UITextInput object that summoned it is. But I've looked around to no avail... you cannot figure out who the firstResponder is unless you troll the view hierarchy yourself or retain your delegates in a list (which would cause a retain loop). Plus, this isn't so bad because the HexKeyboardController will unload, too, when the textField is dealloced.
I dont believe there is a built in mechanism for this, you probably want the a delegate in the hex keyboard that will receive the "keystrokes" from it and then append it to the textfield, or whatever it is you need to do..

How to programmatically change label in UIView created in a nib file?

I have trouble changing the text in a label programmatically.
When I run the following code, NSLog does display "Setting myLabel to = Hello World!", but the label on the screen is not changed.
UIViewOverlay *overlayWindow;
overlayWindow = [[[NSBundle mainBundle] loadNibNamed:#"UIViewOverlay" owner:self options:nil] objectAtIndex:0];
[self addSubview:overlayWindow];
[overlayWindow setMyLabel:#"Hello World!"];
My NIB file has a 300x300 window with some labels and buttons.
There is a label, which is connected to myLabel in the outlet. The UIView does display, just that the text cannot be changed programmatically.
UIViewOverlay.h
#import <UIKit/UIKit.h>
#interface UIViewOverlay : UIView {
IBOutlet UILabel *myLabel;
}
- (void)setMyLabel:(NSString *) label;
#end
UIViewOverlay.m
#import "UIViewOverlay.h"
#implementation UIViewOverlay
- (void)setMyLabel:(NSString *) label {
myLabel.text = label; // THIS LINE IS NOT WORKING! :-(
NSLog(#"Setting myLabel to = %#", label); // This line is working.
}
#end
Thanks in advance..
You are using an incorrect accessor name for your method to set the label string.
In cocoa, setFoo is the method by which an instance variable called foo is assigned. This isn't just a convention, many areas of functionality depend on it, for example the use of properties, key value coding etc.
In your code, your label is called myLabel. Your method to set the text of that label is called setMyLabel. Either this is causing your outlet to not be connected when the nib is loaded, as the runtime may be trying to use that method to assign the label to your instance variable in the first place, or all of the above has no effect and you have just not connected your outlet.
Make sure you have an object in your code for the label, like this example:
IBOutlet UILabel* aLabel;
And in interface builder (you may have already done this): Connect the aLabel (or whatever name you use) outlet to the actual label. This can be done by control clicking and dragging from the File’s Owner object, in the document window, to the label, in the view. A small, gray window will appear with at least two options, one will be the aLabel defined earlier, and the other will be the view (this is a default outlet required for viewcontrollers, it will have a dash to indicate it is already connected to something). Click on the aLabel option to select it. (I'll be honest, without my mac in front of me I copied most of this paragraph's instructions from this link.)
You have an object called aLabel that you can now treat like any other variable. If you want to change the text value, try this:
aLable.text = #"some text";
Maybe you did not make the connection. Can you try the following?
#import "UIViewOverlay.h"
#implementation UIViewOverlay
- (void)showMyLabel:(NSString *) label {
NSLog(#"My current label contents (myLabel) = %#", myLabel.text); // ** changed to myLabel.text
}
#end
If you aren't able to print the original value, then you aren't connected.
I hope that helps.

Populate text in one textfield from text in another

Working on an experiment on the iPad. Tried some variations on how to do this, but I can't seem to get it to work correctly...
I tap a UIButton on my MainViewController and a TextEntryModule is added to the view. TextEntryModule is its own class (for multiple instantiation) and it contains a UITextView called TextEntry (this all works at the moment).
I tap on the TextEntry UITextView and it brings up the keyboard and another view (located in MainViewController) with a UITextView called TextPreview. (this also works at the moment).
The part I'm having trouble with is synching the two UITextViews. The idea being that when I type into TextEntry, the text in TextPreview will also be updated.
Outlets are linked properly for the text fields, but I think I'm missing something "obvious":
TextEntryModule *tm = (AnnotationModule *)currentModule;
TextPreview.text = tm.TextEntry.text
Thanks in advance!
UITextView: delegate.
- (void)textViewDidChange:(UITextView *)textView
Then assign it the value of the other textview in this method.
Edit
#interface MainViewController <UITextViewDelegate> {
...
}
...
#end
Then you implement this method in the implementation file of MainViewController
#implementation MainViewController
//More code
- (void)textViewDidChange:(UITextView *)textView {
TextEntryModule *tm = (AnnotationModule *)currentModule;
TextPreview.text = tm.TextEntry.text
}
#end
Then you will have to set the TextEntryModule object's delegate to self since the controller now conform to the protocol and can "act" upon this notification.
You need to become a UITextFieldDelegate and monitor when text changes in the one field and then update the other field. Take a look at the documentation on it.