I'm learning Objective-C.
I have a problem with new storyboard feature. I would initialize a subview inside a main view with xib.
Without using storyboard, I could do it using:
controller = [[UIViewController alloc]initWithNibName:#"NibName" bundle:nil];
[self.view addSubview:controller.view];
Now, I wouldn't use an external xib, but I want manage a view in a class and use it like a subview in another class.
I know that is possible use a xib and use a similiar code to load it, but it must be out the storyboard.
First create the view in your storyboard and then instantiate it with the following code. Also make sure you give it an identifier via the Attributes Inspector.
controller = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];
[self.view addSubview:controller.view];
First create subview with design in some viewcontroller xib after that copy that view and paste in sub of ur viewcontroller in story board and give connections. Hope it will work.
Related
I have ViewcontrollerA , which is a view made in storyboard.
In this view, i have a UIView that i have added in storyboard and create an outlet to it called containerView.
I wants to add some other viewControllerB (also made in storyboard) to the container.
Tried that :
//add to container a new view from storyboard,with id called serviceView
UIViewController *sv = [self.storyboard instantiateViewControllerWithIdentifier:#"ServiceView"];
[self.containerView addSubview:sv.view];
and got a crash.
How can i do that ?
Thanks .
It most likely crashed because sv is nil, though you should have posted what reason it gave for the crash in your question. If this is the reason, it's probably because your storyboard doesn't contain a view controller with that storyboard identifier. But even if you fixed this, this isn't the proper way of doing view controller containment.
As for adding a view controller to another view controller, you need to use the view controller containment API added in iOS 5. In fact you can even use the storyboard to do view controller containment without writing code. From the Object Library, you can drag out a "Container View". It will let you attach another view controller from your storyboard graphically.
Is it possible to quickly present a UIView in a UIPopoverController without having a UIViewController managing the UIView?
Currently I have a "DelegateViewController" that gets my view passed. Then I use that controller for presentation. But I'm wondering if there is an easier way?
If you have a UIView, then you can easily create a plain UIViewController as a container.
UIViewController* controller = [[[UIViewController alloc] init] autorelease];
controller.view = myView;
Is it possible to quickly present a UIView in a UIPopoverController
without having a UIViewController managing the UIView?
No. UIPopoverController manages a view controller, not a view. When you create a popover controller, you have to provide the view controller that will manage the content. That doesn't mean that you have to create a special view controller subclass in every place where you use a popover -- as bendytree points out, you can use a plain old UIViewController if you want. But you can't just pass UIPopoverController a view -- it has no way to accept it, and wouldn't know what to do with it if did.
It usually makes sense to have a UIViewController for a view since the controller handles all the interaction and setting up of the view. Although you can in some situations put "naked" views on screen the UIPopoverController is designed to work with a UIViewController and the ViewController paradigm is very well established and encouraged in the iOS world, so even if you think you don't seem to need a view controller it should not be harmful to have one and you might always want to extend the current functionality, right?
Please Note: If you are on iOS 5.0 creating views in Popovers is very simple and a matter of dragging-and-dropping the view controllers and hooking them up on the storyboard. Example: How to create Popovers with Xcode Storyboards
What is the correct way to manually add a xib file i.e. if while creating the class, we uncheck "With XIB for interface"...then how do we connect both the IB & VC.m file later, to ensure that the view gets loaded from xib ?
Typically this is done when you instantiate your sub-classed UIViewController. Your call would look something like this:
MyViewController *viewController = [[MyViewController alloc]
initWithNibName:#"NibName" bundle:nil];
Checkout UIViewController initWithNibName: bundle: documentation for more details. You can also look at the documentation on Custom View Controllers for a good bit of information on sub-classing UIViewControllers.
To allow setting actions and outlets at design time in IB, once you create the xib file, change File Owner to your controller class. To do this, in IB, with File Owner selected, press Command-4.
I have a root view controller with no nib file,
I tried adding this at cellForRowAtIndexPath as that passes in a UITableView as tableView.
So I put :
tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
It ran with no error but it didnt seem to change anything.
Your code snippit won't change anything, because the -tableView:cellForRowAtIndexPath: method is only called when an existing table view needs to know how to draw its content. No existing table view, no getting called.
Remove that line from your -tableView:cellForRowAtIndexPath: method, and define a UITableView object either:
In the XIB file (this is the easiest approach)
In the initialisation code for your view controller
As #FenchKiss Dev wrote, if you set up the table in code you need to add it as a subview to an existing view, for it to be displayed.
Did you add the tableView to your view ?
[self.view addSubview:tableView];
I've created a ChildViewController class, and then a nib that uses that class.
Then I created a BaseView, that includes some buttons, and some text that I'll be changing programmatically.
Then I created two more views (Boy and Girl), that I want to be able to lay behind the baseview so that the background color is different along with some graphics in an ImageView. I've named the views that I created in IB 'Boy' and 'Girl'...
But when I go back to my code where I'm calling ChildViewController, I'm not sure how to access the views I created so I can call insertSubView. Do I need to instantiate them in code? (in ViewDidLoad perhaps?) Does the nib create the instances when it loads?
I'm confused about how to handle multiple views for a single ViewController
edit =================
#Pablo Santa Cruz
Your answer assumes that i have two nibs and two view controllers (one for each view). I want to know if I can use one nib and one controller, and load in UIViews. It seems silly to create another nib and controller, when all want to do is change the background color and some graphics. Can't I programatically load in UIViews into a UIViewController?
Add IBOutlets in your App Controller class in Xcode then link them in IB (ctrl-click or right-click) from the connections tab in the Inspector to the object.
Then you will be able to send method calls to the objects.
The code in Xcode should look like this:
#interface AppController : NSObject
{
IBOutlet Girl girlIvarName1;
IBOutlet Boy boyIvarName2;
}
#end
You can access a UIView programatically by assigning a value to its tag property, which can be set in IB on the first tab of the inspector (Command 1)
The tag value defaults to zero, so if you want to access it specifically, make it non zero and unique. e.g. 100, which I will use in the example code below
Once the tag is set you can access the view using the following code in your UIViewController that was initWithNibName for the NIB containing the tagged view
UIView *aView = [self.view viewWithTag:100];
You can get instances for your IBuilder views with this piece of code:
boyViewController = [[BoyViewController alloc] initWithNibName:#"BoyViewController" bundle:nil];
girlViewController = [[GirlViewController alloc] initWithNibName:#"GirlViewController" bundle:nil];
Assuming your NIB file names are BoyViewController and GirlViewController. With those instances, you can do whatever you need to. I.E., adding them to a parent view (with addSubView message on the parent).