Setting the UI Labels of NSTextfield dynamically in cocoa - objective-c

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.

Related

How to connect an IBOutlet to an NSTextView

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:

Unable to drag and drop Outlet to my View Obj-C iOS development.

I am facing a weird problem that i do not understand. I have outlet displaying in Outlet tab but when i try do drag and drop it, it does not appear and thus i am unable to use it. I haven't faces such issue before so help appreciated.
As you can see on image when i try to drag either directly from File's owner or from the Outlet tab the "pointobject" which i want to use does not appear.
In my ViewController you will find this
#import "PointObject.h"
#interface LandscapeViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, PointObjectDelegate>
{
// not relevant code up here
//----------------------------------------
/*
* for graph
*/
LandscapeGraphViewController * landscapeGraphViewController;
IBOutlet PointObject *pointObject;
IBOutlet UIButton *ZoomIn;
IBOutlet UIButton *ZoomOut;
}
// not relevant code below
In my PointObject.h you will find this
#class PointObject;
#protocol PointObjectDelegate
-(double)expressionResultForXValue: (CGFloat)x requestor: (PointObject *)pointObject;
#end
#interface PointObject : UIView
{
id<PointObjectDelegate> delegate;
CGFloat scale;
CGPoint originOffset;
}
-(void)reset;
#property (assign) id <PointObjectDelegate> delegate;
Some code has been cut out as it is not relevant to the question. Thank you
Make sure that you have assigned class PointObject to a view, you want to use as PointObject
Just see following image...
As shown in image select your view which is used as PointObject then in third tab of property window assign class PointObject .
Hope it will help you........

How to get a Text Field's text in XCode

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

Why can I not wire up my IBOutlets in storyboard?

I have a ViewController that has a couple of IBOutlet properties defined for UITextView. In the storyboard I have assigned my viewcontroller to the custom view controller. When I cntr + click on the viewcontroller I can see my two IBOutlets. When I try to drag them to the UITextViews on the storyboard they will not highlight and cannot be assigned.
I'm new to xcode 4.
Here is the property definitions from the viewcontroller:
#interface CreateUserViewController : UIViewController<UITextInputDelegate>
#property(nonatomic,retain) KeychainItemWrapper *keyChainWrapper;
#property(nonatomic,retain) IBOutlet UITextView *userNameTxt;
#property(nonatomic,retain) IBOutlet UITextView *passwordTxt;
These properties are synthesized in the implementation. I can assign each of the textfields' delegate to the viewcontroller without a problem. Am I missing any step?
Thanks
Make sure that the views you're trying to connect really are UITextViews and not UITextFields. It's not difficult to get them mixed up.

How to get object id from UIButton?

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.