IBOutletCollection for UITextView - ios

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.

Related

Unable to add IBOutlet in swift

I'm trying to add an IBOutlet in swift but I only have the option to add an action.
Here is an image of what I'm talking about.
]
Is there any way I can fix this?
I can't change connection type either.
This is happening because you are trying to connect an outlet from the Interface Builder to a non-corresponding View Controller file, so that's why it only gives you the option to add an Exit action. Just make sure that you are in the same View Controller both in the IB and in the Assistant Editor.
P.S. I recommend you renaming properly every view controller so it will be easier to avoid this (make sure to change the name not only in the class but also in the IB). You can use the cmd + click when selecting the class name on the code and click on Rename... to change all at once. If you already changed it in your code, you have also to do it then manually in the IB, selecting the corresponding one from the drop-down menu of the Identity Inspector:
Try ctrl dragging it from the text on the left ()
Restart your Xcode and try to connect #IBOutlet
Simply change the connection type or one more thing you can do. you can do this. By opening connection inspector and drag the outlet into view controller like shown in image..
To Change Connection Type
You should use the same class in your view controller (call it View Controller.swift)

Swift Error IBOutlet with xib file

In my project, I want to display a popup and the view of this popup is defined in a .xib file.
To do that I use this line of code :
myPopupView = NSBundle.mainBundle().loadNibNamed("NibView", owner: self, options: nil)[0] as! UIView
Until then, it works. But the problem is when I try to add an IBOutlet. My Xib file is connected to a uiviewcollection, and I manage to create an IBOutlet on the Textfield I have in the xib file. But when now I run again my app, I have this famous message :
this class is not key value coding-compliant for the key...
So if I look on Internet, everybody says to delete the IBOutlet by clicking on the cross button in Inspector, BUT I don't want to delete it because I need this IBOutlet. So isn't it possible to have IBOutlet in a xib file ?
Thanks :)
This is very common error which means the IBOutlet you have taken in .xib is not connected to the relevant class. This kind of error I experience when i have taken a outlet and given some name like lblUserName but later i renamed that to lblFriendName, then the label in xib is still connected to lblUserName as well as lblFriendName, so at the time of compilation the xib did not find the connection for lblUserName and throws error.
So go to your xib, right click on the outlet in the xib, then you will see the cross button to disconnect. So disconnect it and connect it again.
During that also you can see the connection name, it must be wrong name or there would be two connection foe the same label in which one would be correct and another must be wrong (This case when you rename a connection as i explained)
Hope you understand, and it helps...
Happy coding ...

Hide Outlets from Connections Inspector for Custom View

I have created a custom view (Quantity View) with nib file in Swift. I have created some IBOutlets & IBActions (for buttons, labels etc.) in my custom view.
I tried to use this custom view (Quantity View) by assigning class name to a UIView in my storyboard.
It's showing me all the IBOutlets & IBActions in the Connections Inspector, as shown in this screenshot: .
I just want to show only delegate for the Custom view.
Possible Answer:
I thought I can use the -viewWithTag to get the views instead of Outlets.
But, I want to know if it's possible with having Outlets also or if there is much better way to do this?
What are the other possible ways (optimum) to handle this situation?
You can also consider the following solution:
You can take the subviews of your QuantityViews(custom view) and you can identify the specific views by its frame origin.
Note : you should know the customview subviews frame
Its not possible to hide IBOutlets from storyboard if you declare the class members as IBs (IBOutlets or IBActions).
The IBOutlets or the IBActions are just indicators to the interface builder so that it can show the names on it when you try to bind them it actually calls the setValue: forKey: method to set the view's reference to the IBOutlet property.
Now if you try to access an subview from the file's owner class without any IBoutlets you need to have a pointer to point it, so for that either you can get the reference using ObjectID which is assigned to the subview by the interface builder or you can get it using the viewWithTag: method.
The ObjectID you need to find all time when you add or replace a subview from the view, so better and convenient approach is to use tag property of UIView class.
So my conclusion to this problem is to access the views using the viewWithTag method you mentioned earlier.
I think your way is correct. But sometimes Xcode doesn't work correctly.
The following makes the IBOutlets and IBActions reappear and work properly:
Clean project your project in Xcode.
Quit Xcode completely.
Delete all contents of ~/Library/Developer/Xcode/DerivedData/.
Restart MacOS just in case.
I hope you will resolve that :)

How to find the UI element connected to a specific IBOutlet?

Is there any way to find which UI element an IBOutlet declared in code is connected to?
Go to your view controller file where you have declared IBOutlet (.h or .m). Then you can see left side of each variable declaration there is a dark grey round image. (It is filled if you have connected that IBOutlet with Storyboard/Xib unless it is unfilled.)
By clicking that image you can see small popover which shows connection of outlet. See below image will show you.
You can search for the IBOutlet property name in the project directory.
There you will find the corresponding xib file and UI element.

how to connect an IBOutlet to textview control in 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.

Resources