How to load UIView from UIViewcontroller? - ios

I have created a view and now I want to load that view from another viewcontroller without allocating the view. Is it possible or not. please clarify me.
I am doing like following
1)Added UIView in viewcontroller.xib and given view class name for the view.
2)created IBOutlet for the view and connected.
Now I want to access one property of a view without allocating. Is it possible to do or not.

If you've created an IBOutlet and hooked up the view, then you are done. The view is allocated when the xib loads and the reference is placed in the IBOutlet. You can access the view and it's properties through that without doing any allocating yourself.

When your view controller is loaded, your view connected to it (by IBOutlet) also gets loaded. In this case you have access to all view's property.
Look at "Looading up an XIB" section in this article.

Related

How to access programmatically a tableview added with storyboard

I added a tableview to my viewcontroller using the storyboard. What is actually happening behind the scenes? If a UITableview object gets created how can I access this object programmatically? What is the name of the instance? Is it a property of my viewcontroller object?
More specifically, what I need to do is force my tableview to refresh (from inside of my viewcontroller). I read in other posts that I should be able to do something like this [self.tableview reloadData]. I can't do this because my viewcontroller does not have a property called "tableview" (or anything similar)
Control + click and drag your tableView into your header file to create an IBOutlet
Set the delegate and data source by control click and drag from your table view to your view controller - make sure both dots are seen. Now
In the interface .h file add the delegate and datasource as such:
#interface HomeTableViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
Now your view controller should have access to the methods needed to use [self.tableView reloadData]
try this simple table view
and this work with custom tableview cell
try above tutorials and in your case to use [self.yourtablviewname reload]
you have to create an outlet of your table view and use it.
Remember that the storyboard defines a collection of objects which are created when you make an instance of say, a ViewController.
You are on the right track, you need to define variables in your view controller which are references to the objects in the storyboard. You mark them as IBOutlet in your code. You then 'connect' them to the objects in the storyboard. Right click on the viewcontroller in the storyboard and you will see the variables which have been designated as IBOutlet listed as 'outlets'. You can drag from the popup table to the objects in the storyboard to connect them. Now, when an ViewController is created, all the IBOutlet variables are set to point to the various objects (tables, text views etc) in the instance just created.
Remember that the 'connect' you do with the Interface Builder, happens at rum time when you create an instance of the object in IB.

How to access properties from another ViewController?

I have a ViewController and another one which is a TableViewController. So I have the tableview in a container view in the ViewController. Moreover I have a button which will save the values from the tableview. But how do I access these values? I've uploaded a screenshot so you can imagine it better :)
I don't understand why you use a container in this case.
But, have a look in
self.childViewControllers
It should contain your tableViewController.
You make an IBOutlet. Go to the storyboard and drag your embedded tableView into your other view controller. There you can reference all of its exposed stuff (in the .h), assuming its been loaded already.
Reference on outlets: https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Outlets/Outlets.html
Good post on using container views:
IBOutlet link to embedded view controller

Cannot create outlet connections to subviews in Interface Builder (Xcode 5)

I know this appears to be a duplicate of some other questions, but the answers are not working for me.
I have created a single view app.
In the storyboard I added a subview to my main view.
I have a label on my main view and another label on my subview.
I have created a class of type UIView and added it as the custom class for the subview.
I can ctrl-drag my label on my main view to the main view controller class. But when I try to ctrl-drag my label on my subview to my custom class, I cannot get the connection to occur.
I have even typed the property information and tried to make the connection manually to no avail.
Things have changed a bit in the latest version of Xcode's Interface Builder. Can somebody tell me what I am missing? There is literally no code here. I am just testing trying to connect outlets to a subview with a custom class.
The first image shows that I have set up the custom class and added a property but I cannot make the connection.
The second image shows the main view label is connected in the main view's controller.
The third image shows that there are no outlet connections for the subview's label.
You can manually write the IBOutlet property declaration in the #interface of the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.
Or, as you point out, Warren Burton suggested both this technique and another in his answer to this other question, Can't Wire to Subview in IB.
The issue has to do with the File Owner of the View Controller. It is probably set up as being IOViewController, thus you can only make property connections in that .h file.
What you can do, is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.
This is what I did (in Swift):
I Created a new ViewController (e.g. class MyViewController: UIViewController {})
In StoryBoard, I expanded the 'Scenes' (i.e. the tree view of all UI components) and selected 'MyViewController'
Using the 'identity inspector' I assigned the 'MyViewController' class (as oppose to the default UIViewController)
After that I was able to assign an action.
I suspect that for Obj-C it is similar process.
You don't create outlets in the subclass, you create the outlet on the view controller it is on. You need to #import the subclass into IDViewController.h and create an outlet there.
IDViewController.h
#import "IDSubclass.h"
...
#property (strong, nonatomic) IBOutlet IDSubclass *outletName;
Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

In IB, Cannot Connect IBOutlet Delegate to Another View Controller?

I have a view controller which as one of its views a container view (in IB storyboard) which embeds a table view controller, which in turn has a container view that embeds yet another view controller. In this last view controller I set up a delegate protocol with a weak synthesized delegate property as an IBOutlet. The very first view controller is what I want to receive the delegate methods from the last and I added the protocol <...> to it.
The problem is that I have not figured out a way while in storyboard (or otherwise) to link the IBOutlet delegate of the last view controller to the first view controller which follows the protocol so the last can send the first messages. I thought I could just drag and drop (with the control key) but all I find is segue options on the destination. It seems even though the delegate appears in the outlet connections window, it will not connect to ANY view controller in my project.
Can't ANY view controller be a delegate of another's protocol? And can be linked in IB? If I cannot do it with IB, I don't know how to make another VC a delegate upstream.
Any advice would be appreciated. Thanks.
You cannot connect IBOutlets between view controllers. You need to do it in code. You'll have to go through the chain of childViewController to get from the first controller to the last -- if I understand your structure properly, from the first view controller:
LastController *last = ((UIViewController *)self.childViewControllers[0]).childViewControllers[0];
last.delegate = self;
I asked a similar question Interface Builder won't allow connections to custom UIView class? I ultimately opened a bug with Apple. After a few follow up queries from Apple, I haven't heard any kind of resolution.

How to make a connection to UIContainerView

I have a standard extended UIViewController (called ParentViewController, or PVC), and want to have a container or child view which will be driven by ChildViewController or CVC.
I see that programmatically, that in PVC I can call addChild:CVC and manage calls such as didMoveToParentViewController, etc.... However, I am hoping that there is a way to do this with Interface Builder.
So I laid out an instance of PVC, then dragged a ContainerView onto PVC's view which creates a segue to a child window. In that window I assigned the class type of ChildViewController. This looks great so far, but how do I reference this programmatically from PVC? Let's try the usual: In PVC class extension, I have declared:
#property (nonatomic, strong) IBOutlet ChildViewController* cvc;
When I switch to IB, and highlight the PVC instance, and select the connections inspector, I see my cvc property but I cannot hook it up to my containerView instance (even though its class type is set appropriately). It allows me to drag (draw the line), but does not let me actually hook it up. Why??
I am thinking that this just isn't possible with IB, but perhaps I'm looking at this incorrectly?
I have read Apple's docs on child views and am prepared to do this programmatically if I can't get IB to do what I'm intending.
cvc is a child view controller of PVC, so you can get a reference to it with the childViewControllers property, which you would do in code rather than hooking up an outlet in IB. In PVC's viewDidLoad method, do this:
self.cvc = (ChildViewController *)self.childViewControllers[0];
Xcode will not allow you to hook up outlets between two different controllers, so even though cvc is embedded as a child view controller, it's still a different view controller.

Resources