I made a Text Field with interface builder. How can I get its text to use somewhere else?
Is there something like:
string text = myTextField.Text;
If so, how can I name my text field?
So the first thing you want to do is create a property in the .h file of the view controller files associated with your xib file like so:
#property (strong, nonatomic) IBOutlet UILabel *aLabel;
Next in the .m file you need to synthesize it like so:
#synthesize aLabel;
Within you implementation tag.
Now in interface builder control click where it says "File's Owner" on the left side of the screen in Xcode 4.3 hold down and drag over the label and let go. Then in the popup select the name of the property you created. Now in the viewDidLoad method of your view controller you can get at your value like so:
self.alabel.text
As Greg Price mentioned, to get a name for your text field, you have to create an Outlet.
go to your ViewController.h file and create a property for your text field:
#interface YourViewController: UIViewController
#property (nonatomic, retain) IBOutlet UITextField myTextField
#end
With the keyword IBOutlet you say the Interface Builder that there's an outlet to connect with this property. On the other hand you'll use the keyword IBAction to connect methods to the objects.
Don't forget to synthesize your property, right under your implementation, then the compiler will create you some standard setter and getter:
#synthesize myTextField;
Then go to the interface builder and right click on the File's Owner then drag a line from myTextField to the TextField on the View.
Now you can access the text property in your code:
NSString *string = self.myTextField.text;
First you will need a #property called myTextField.
After that you can get the text of the textfield myTextField.text. For more information take a look at the class reference UITextField
Maybe you want to get it while the user is typing. If so you can take a look at UITextFieldDelegate
-
Edit: A cool link to take a look if you are a objective c student, you can learn more at: UITextFields examples
Best Regards.
Its quite simple just drag Text Field from View Area to your Storyboard. Then activate assistant editor so that you can view both code window and storyboard at the same time.
Then press control button and drag the Text Field from your storyboard to code window and thats it.
//created a string variable
var name: String = ""
//our label to display input
#IBOutlet weak var labelName: UILabel!
//this is the text field we created
#IBOutlet weak var textFieldName: UITextField!
#IBAction func buttonClick(sender: UIButton) {
//getting input from Text Field
name = textFieldName.text!
}
Source: Xcode Text Field Tutorial
Related
Unlearned programmer here. Last time I used a NSTextView I was able to do it by adding a class to my xib file with all my outlets declared as such:
#interface Fusebox : NSObject
{
IBOutlet id object1;
IBOutlet id object2;
....
}
Now I am trying to code my interface properly, using the application delegate because Xcode 5 does adding a separate class file differently, if at all. I attempted to have Xcode's IB create the property of a NSTextView. But it wont creates this:
#property (weak) IBOutlet NSScrollView *results;
instead of what I need, which I think is this:
#property (weak) IBOutlet NSTextView *results;
An NSScrollView is completely useless for my purposes of outputting formatted (mainly line breaks) strings. When i try to hand code it as a NSTextView, it wont let me connect the outlet, only bind it. I am grabbing on a text view object from the library in the interface builder, so what am i doing wrong?
I think you aren't actually hitting the NSTextView - by default, it's imbedded in an NSScrollView. Xcode probably thinks you're trying to connect the outlet to the scrollview.
Try expanding the left pane of Interface Builder and connect the outlet to the sidebar:
Right-click-dragging from that highlighted row above to the code gives the expected result:
I just jumped into Objective C.
When I create a button and connect it to my code, I get the following line of code in my property section:
IBOutlet UIButton *btn;
I've learned that a property syntax is [class] *[variable name].
What is IBOutlet in this case?
No. IBOutlet is simply a macro that resolves to nothing.
Their purpose is to simply let Interface Builder know that your variables (in your case UIButton *btn) can be used to link UI elements to your code within Xcode.
That is not a property. That is just a variable declaration. The property version of that would be
#property (nonatomic, retain) IBOutlet UIButton *btn;
And then in your implementation file, you would place
#synthesize btn
just below the #implmentation line.
Have you ever used interface builder? IBOutlet is a macro that lets you refer to views in interface builder from your code. In your case, it lets you hook up the UIButton to interface builder so that you can use a reference to it from within your code. Other than being used to let interface builder you want to hook up the variable to a view, it's not used at all and actually resolves to nothing.
All,
In Apple's sample code "DateCell"
http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
the ivar "pickerView" is declared in MyTableViewController.h like this:
#interface MyTableViewController : UITableViewController
{
#private
UIDatePicker *pickerView;
UIBarButtonItem *doneButton; // this button appears only when the date picker is open
NSArray *dataArray;
NSDateFormatter *dateFormatter;
}
#property (nonatomic, retain) IBOutlet UIDatePicker *pickerView;
...
It is synthesized in the class file MyTableViewController.m like this:
#implementation MyTableViewController
#synthesize pickerView, doneButton, dataArray, dateFormatter;
...
When this app runs, I can insert NSLog(#"%#",pickerView) into ViewDidLoad and see that, sure enough, the ivar pickerView is real and has a value. Nowhere, though, does this class alloc/init pickerView. And that's the root of the question: how's it getting done if it's not being done explicitly?
Well, I naively copied this stuff to my code into my RootViewController.h and .m files figuring I could do the same, but pickerView stubbornly remains uninitialized (and my NSLog calls return "(nil)" as its value) no matter what I try short of explicitly alloc/initing it. Certainly RootViewController is being instantiated, or the RootView wouldn't be showing up, right? So shouldn't my pickerView be coming along for the ride just as it does for Apple?
So... do I have to manually alloc/init the pickerView instance variable? If so, where's Apple doing it? Or how are they doing it somehow otherwise?
I think I'm missing something very basic here, but I have no idea what it is. I can't see anything in Interface Builder or XCode that looks different between mine and theirs, but I've got tunnel vision at this point and can't see anything clearly anymore.
Thanks,
Bill
The IBOutlet modifier on this line is the key...
#property (nonatomic, retain) IBOutlet UIDatePicker *pickerView;
IBOutlet is a decorator that indicates that the object will be hooked up/connected/initialised when the corresponding xib (Interface Builder) file is loaded. The sample application you're looking up will contain a UITableViewController is a xib which has a connection to a UIPickerView.
You can either go the route of creating your own custom xib file and wire to an instance of UIPickerView or you can manually initialise the picker yourself.
Interface Builder (nib or xib) treats automatically IBOutlet ivar with connection of components.
IBOutlet is a special keyword that is
used only to tell Interface Builder to
treat an instance variable or property
as an outlet. It’s actually defined as
nothing so it has no effect at compile
time.
Your First iOS Application - The
View Controller Interface
Declaration, Making Connections
Interface Builder User Guide -
Defining Outlets and Actions in
Xcode
How to get object id from UIButton ?
I want to know which uibutton is pressed currently when i don't save the pointer to uibutton.
Not alot to your question so it's hard to understand what you're asking... But, I'll hazard a guess by making the assumption that you're asking for how to get access to a UIButton from within your code? If so, then I'd need to also assume you'd added this button via Interface Builder in which case you simply need to add an IBOutlet pre-processor directive for each button object in your view controller (or view's) interface as such:
#interface MyView : UIViewController {
UIButton *myButton;
}
#property (nonatomic, retain) IBOutlet UIButton *myButton;
// Button's action which you probably already figured out
- (IBAction)buttonPressed:(id)sender;
Then you'll need to make the connections in Interface Builder. Drag FROM the object that needs to know TO the object it needs to know about. So you'd control-drag from the File's Owner to the UIButton to connect the IBOutlet...and then control-drag from the UIButton back to the File's Owner to connect the IBAction.
I need to set the labels of the UI dyanmically..
I want be read the text from an xml file and would like to set the text to the controls in the NIB.
I guess i can recognise the conrol by using the TAG attribute of the control.
Now i would like to get all the objects in the window(controls in the Nib) into an array?
Please suggest me on this.
In your code you'll want to create a link to your control. In xcode, in your .h file put something like:
#interface Mycontroller : UIViewController {
IBOutlet UILabel *namelabel;
}
#property (nonatomic, retain) IBOutlet UILabel *namelabel;
-(void)ChangeName:(NSString *)toName;
#end
Then in your .m file put something like:
#implementation ProjectCell
#synthesize namelabel;
-(void)ChangeName:(NSString *)toName {
[namelabel setText:#"your new string"];
}
You then want to open your nib in interface builder. Select your label and go to the inspector (Tool Menu > Inspector). Go to the Connections tab (blue circle w/ white arrow, and then click and drag the circle by New References Outlet from there to File's Owner in the nib window. Select "namelabel" from the popup. They're now linked and changing namelabel in code will change that specific label that you setup in interface builder.
I agree with the above soultion....
I had to set the title of each textfield and buttons in applicationdidFinishLaunching function.