how to connect an IBOutlet to textview control in iOS? - ios

I'm really a newbie on iOS and I don't know how to connect the UITextView object to the textview control on *.xib file? Is there any way to set the unique ID to a control on iOS?
Currently, I have declare an IBTextView as below:
HelloViewControler.h file:
#interface HelloViewController : UIViewController{
IBOutlet UITextView *txtTextMe;
}
#property (retain,atomic) UITextView *txtTextMe;
- (IBAction)buttonPressed:(id)sender;
#end
HelloViewControler.m:
#synthesize txtTextMe;
- (IBAction)buttonPressed:(id)sender{
txtTextMe.text = #"Button clicked";
}
Code to update a string to textview control:
txtTextMe.text = #"Button clicked";
No Error. But when I run the code, the string "Button Clicked" wasn't updated to the textview.

Hold CTRL and click and drag from the item in the .xib into the code. This will create either an IBOutlet or IBAction (you chose via popup) and link it to the object on the .xib.
It can also be done via the left hand inspector if you have already created the code.
If you are new to iOS programming and Xcode I would very strongly recommend you look at Apple's tutorials on the developer portal. They have some excellent introductory guides with step by step instructions. The second one covers using storyboards and is a really great introduction to storyboards, Xcode and the iOS SDK - link below.
Your Second iOS App: Storyboards

Right-click on the textview control in the NIB file. A HUD-style menu should appear.
Under "Referencing Outlets", drag from the circle to the right of "New Referencing Outlet" to "File's Owner" in the "Placeholders" section to the left. When you release the mouse button, another popup menu should appear.
Pick txtTextMe from the menu.
Build and enjoy :-)

This is my fault. I drag a UITextField to *.xib file but I declare an IBTextView in my code. Thanks all.

Related

IBOutletCollection for UITextView

Can someone please tell me what are the steps in InterfaceBuilder to connect a set of UITextView objects to an IBOutletCollection? In my XIB file (say myfile.xib) I have a row of 8 UITextView objects. In my myfile.h file I declare an IBOutletCollection:
#property (nonatomic, retain) IBOutletCollection(UITextView) NSArray *textViews;
Now I want to connect the XIB UITextView objects to the IBOutletCollection. I tried to CTRL drag from the first UITextView in the XIB to FileOwner, but that didn't show me any IBOutlets. I tried to CTRL click (right click) on FileOwner and that popped up some little black menu. That showed my textViews outlet collection, so I tried dragging from the little circle by textViews to my UITextView objects in the XIB, but that didn't do anything. I searched google to find some sort of info on this, but I only found statements like "connect your UITextView to the IBOutletCollection in the usual way." I don't know what "the usual way" is. Please help.
Are you sure you were were dragging from the correct "little circle" to your text views, because that works fine for me.
Another way is to do it just like you connect an outlet by opening the assistant editor so you can see both the xib file and the controller's .h or .m file. Starting with no code in the controller, select the text view and control-drag from it to the area under #interface, and it should say, "Insert Outlet or Outlet Collection". When you let go, fill in the name, and then change the "connection" pull-down from "outlet" to "outlet collection". After the first one is done, control-drag from the other text views to that line of code.
Make sure the class of File's Owner is set to the class you are trying to hook up the outlet collection to. Open the Identity Inspector (Ctrl + Cmd + 3) and type the name of your class in the box. Then try hooking the outlets up again.

No Action Connection Option

I'm following along with Big Nerd Ranch's iOS programming book. In chapter 11, it has us control drag from a camera button into the method area of a DetailViewController.h file. The instructions tell us to select action from the connection drop down menu, however, the only options that are available when I do this are outlet and Outlet Collection.
In the book, the result is supposed to look like
- (IBAction)takePicture:(id)sender;
However, in my case, I get this when I choose the outlet option
#property (weak, nonatomic) IBOutlet UIToolbar *takePhoto;
I'm concerned that if I just delete the code and replace it with what it says in the book, I'll still have the problem of not having established the proper connections via the control click and drag. Can you tell me what I might have done wrong when it failed to create an action?
The problem was that I didn't control drag from the camera button, but rather the toolbar in which it was contained. Hence the action button wasn't available.

How to establish outlet connection in Xcode

I am following a tutorial HERE on creating a table-view controller in Xcode. I am using the latest version of Xcode (Xcode 5). At the top of the tutorial it says its for iOS 5.
In the tutorial a cell is created and an UIImage and a few labels are dragged onto the cell. It goes on to say to Ctrl-Click the white space of the cell and then drag to the labels and image to establish the outlet connection. See screen grab from tutorial.
When I do this, I dont get the same options as per the tutorial. The only options I get are shown below in this screen grab (note I chose to only add one label and leave out image for the purpose of my application).
Can anyone please let me know what Im doing wrong or how else I can create this connection? I have Googled it for the last hour and all I could see was how to create IBOutlets.
Alternatively, does anyone know of any tutorials similar to this one? This one suits my needs perfect as I will be creating a view similar to the second part of this tutorial. Like I mentioned Im working with Xcode 5.
Although this might be a little late, I recently just stumbled upon this problem myself and figure out the answer. I used the same tutorial as you, and could not seem to generate the proper options when I did the control + drag.
Instead of dragging the labels to the whitespace of the custom cell, you will be dragging from the Connections tab of the custom cell. To do this:
First, make sure your labels are properly declared in CarTableViewCell.h and synthesize them in CarTableViewCell.m.
Second, change the custom class in the storyboard for the prototype cell from UITableViewCell to CarTableViewCell.
Next, click on the cell prototype in storyboard. Using Utilities, go to the connections tab (the last one with the arrow). If everything is linked properly in your header files, you should see "makeLabel", "modelLabel", and "carImage" under the Outlets section.
Finally, click the little circle next makeLabel and drag it over to the proper label in your custom cell. Do this for modelLabel as well as carImage, just using the associated outlet and label or imageView for each.
This solved my connection problem, and after following the rest of the tutorial, everything worked!
Is your cell a custom cell. If not click on your cell and go to the attribute inspector then under table view cell choose the style as custom
Is your TableViewCell connected to the "CarTableViewCell"?
Click on the cell and make sure the custom class is set to CarTableViewCell.
And your .h file matches the one in the tutorial
#import <UIKit/UIKit.h>
#interface CarTableViewCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UIImageView *carImage;
#property (nonatomic, strong) IBOutlet UILabel *makeLabel;
#property (nonatomic, strong) IBOutlet UILabel *modelLabel;
#end
Try again, and it should be like the tutorial.
-Cong

Xcode 4.6 Can't add outlet for text field as "connection" in popover is greyed out

I need help adding an outlet for a text field in XCode 4.6.3. (By way of background, I'm a newbie following Apple's "hello world" tute and am up to here.)
I control-drag the text field inside the method declaration then release. In the pop-up that is displayed, "Connection" defaults to "Action" and is greyed out. I can't change it to "Outlet".
How can I make it editable so I can choose "Outlet"?
Sorry about the lack of images but I'm too junior on Stack Overflow. If you want a visual, feel free to go to the Apple tute, search for the text "you can configure the action connection you just made", then look at the image underneath.
Would appreciate any help as I'm pretty stuck.
D
That happens if you control-drag into the implementation part of the view controller
in the .m file:
#implementation HelloWorldViewController
...
#end
Outlets connect a property of the view controller with an object in the xib file,
and properties are declared in the interface of an object.
Therefore, to create an outlet connection, control-drag into the public interface part
in the .h file:
#interface HelloWorldViewController : UIViewController
...
#end
This is what the tutorial shows.
Alternatively, you can make the property "private" to the implementation by
control-dragging into the private extension interface in the .m file:
#interface HelloWorldViewController ()
...
#end
This works with newer Xcode versions and helps to keep the public interface clean.
Drag and drop textfield to view
click on editor as shown below blue circle
right click on textfield and drag it to viewcontroller.h as shown below
you will seet the box as shown below: enter your name for textfield
click connect.
its done..
hope helps you

How to get UIWebView working

(Link to errors here: http://imgur.com/a/AF87N)
I'm starting to work on iOS development, and I'm relatively new.
I was looking for tutorials on how to display web content in the app, and so I used the UIWebView.
In the tutorial I found I followed the steps exactly, but when I went to do the last step (linking the outlet) I got an error.
The two files I edited are DataViewController.h and DataViewController.m
This is the tutorial I followed: http://www.youtube.com/watch?v=NFffF9tRbak
I'm using XCode 4.6
Am I doing something wrong?
Renaming may still cause some erors, there is a chance, so its better to set the connections again.its quite simple.
You can solve this by following these steps:
1.
Click on the cross sign left to Web View , that will remove the invalid outlet.
2.then, connect the existing #property(nonatomic, retain) IBOutlet UIWebView *myWebView by control dragging the '+' sign left to the property outlet to your webview in the Interface Builder.
This will solve your problem, happens just because you are new to xcode. Will get better as soon as you get familiar.
EDIT:If its still not clear, see the screen shots below;
your outlet in yourviewcontroller.h may be like this now,
and in your xib,
it may look like this.
So what you should do is, just control drag the while blank circle left to #property outlet (first image) to the webview in your xib (highlightened one in second image). It will connect itself.
hope its clear now
Regards
Change your outlet name from webView to myWebView in outlets
Edit:
or change your UIWebView object name from myWebView to webView in your code

Resources