Add subview from a xib or another scene with storyboard - ios

I'm new to iOS and Xcode.
I can't figure out how to design a separated view and make it be added into the main UIViewController using storyboard.
I did different approaches..
Just grab an UI object from right-bottom corner window in the xcode, and then put it onto any space area of storyboard. But I can't drop the UI object like the way with xib.
Add a new UIViewController. Add a view into the UIViewController. In the main ViewController.m, I get the new UIViewController instance in the viewDidLoad, and then [self.view addSubview:newUIViewController.view]. But I can't see the added view.
I created a new xib file. And add a view into it. I also try to get the instance in the main ViewController. And addSubview with the xib's view. But it also failed.
Is there a correct way or any working solution to do so?

I figured out a way to do it.
Described as following:
Create a .xib file. For example: MyView.xib
Create a objective-c class. For example: MyViewClass.h and MyViewClass.m
Set the .xib File's Owner to the class.
Add a UIView element on the storyboard and set the custom class to the objective-c class name (MyViewClass).
The key-point is to override the initWithCoder method in the object-c class.
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
The idea is the custom class is loaded by the storyboard and initWithCode will be called.
The index 0 is the root view in the .xib interface builder.
It's kind of tricky but it works.

In storyboard, drag and drop a view controller. The view controller come with a main view. Select that main view by clicking outside of the added view controller and then clicking in the center of it. Now, just drag a uiview or whatever into that main view just like you did with IB.

Related

add a custom uiview with xib in my storyboard

I am using swift2.0 and I have just added a custom UIView called SortableTableTitleView:
and then I drag a UIView in my storyboard, and I changed the class of the view to SortableTableTitleView
When I tried to set the text of the label in the custom view, I just got a bad access error.
I tried to write the initwithcoder method of the view, but I don't know how I can load the element from xib. (It is somehow different with ObjectiveC). So, how can I solve this?
You cannot use xib files within the Storboard file. So either create the view fully in the Storyboard and use it as per normal. Or, add the view from your xib file add a subView to the view on the storyboard via code.
So something like this in the viewController:
UINib *nibFile = [UINib nibWithNibName:#"SortableTableTitleView" bundle:nil];
NSArray *views = [nibFile instantiateWithOwner:self options:nil];
UIView *sortableTableTitleView = views[0]; //Assuming it is the only view in the xib file
//Add it where you want as a normal view now:
[self.view addSubview: sortableTableTitleView];
I realise the code above is in objective-c - I'll get the Swift comparison when I get a gap (Being new to Swift and all)

tableView initwithframe and storyboard

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.
This is the code for calling the tableview on button click.
- (IBAction)botaoAdicionar:(id)sender {
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
In the tableview class I have this:
#implementation AtividadesPraticadasTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.
What am I missing?
Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.
You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.
In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.
You need to do the following:
Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.
instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells
Add this as a child view controller to your original view controller, using addChildViewController:
Add the table view controller's tableView as a subview
It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.
Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.
The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.
The thing You are missing called manual. Check this protocol for TableView dataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource
P.S. Here good tutorial for storyboards http://maniacdev.com/ios-5-sdk-tutorial-and-guide/xcode-4-storyboard

Custom UIView's labels not showing

I have custom UIView that has a xib file as well as .h and .m files. My custom view contains a number of UILabels.
I have added this view to a UIViewController's xib file by dragging in a UIView, going to the Identity Inspector and changing the class name to MyCustomView. MyCustomView is connected the the view controller with an IBOutlet property.
When the view controller is loaded, the MyCustomView's
- (id)initWithCoder:(NSCoder*)coder
method is called, but none of its labels show in the view controller's view. How can I fix this?
I already know how to add a custom UIView through code using:
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"MyCustomView"
owner:self
options:nil];
MyCustomView *view = [nibContents objectAtIndex:0];
[self.view addSubview:view];
but I want to know how to add through the xib file. I have searched for a while but haven't been able to find the answer, so if this is a duplicate please point me towards the correct answer.
Here are images of the custom view xib and the view controller xib:
I'm afraid You can not make UIViewController's xib load your own view's xib.
You'll either need to add those labels into you own view programly,
or
add the view into your viewcontroller's view programmly, as you showed in the code
I'd need to see more code to know for certain, but I suspect that the problem you are having is that the custom view (the parent of the UILabels) is not giving the UILabels dimensions. It's not calling -[UIView setFrame:]
To verify that, type this in the debugger:
po [[UIApplication sharedApplication] keyWindow] recursiveDescription]
That will print a description of the view hierarchy. Check what the frames of the UILbals are.

How can I create a view to add to a view controller?

I am using UAModalPanel to create a popover controller effect. I can get the popver box to display, but I am struggling to figure out how to create a view (graphically, in storyboard), instantiate that view in code, and add it to the UAModalPanel.
What I've Tried
Created a UIViewController in storyboard, set it's class to a custom class, instantiated that class in code, got it's view and tried to add it to the current 'scene'.
That's it. Surely there is a way that I can make a view in storyboard, have it make a sub-class of UIView which I can then grab in code where I need to use it? Instead of laying it out in code?
In storyboards you'll want to drag and drop a new UIViewController then give it an identifier here:
Then in code you can get the view property of this view controller with the following:
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:#"myIDfromTheLastStep"];
Now you can get the conrollers view property and make adjustments. Here's an example of frame change
[myController.view setFrame:CGRectMake(0, 0, 320, 320)];
Unless you need the segues you may better off creating a standalone XIB
Layout the view as you need, set its Class to your own (MyCustomView) then instantiate like this
NSArray *nib=[[NSBundle mainBundle] loadNibNamed:#"MyCustomView" owner:self options:nil];
MyCustomView *view = (MyCustomView*)[nib objectAtIndex:0];
As long as your view is the first/only object in the XIB this will instantiate the view for you

How to obtain a subview from a view controller located on storyboard

I have two view controllers, and each have a UIView that has a label on it. I want to set the view from the first view controller to be the second view when I press a button. When I go to do this, the second view controller doesn't load the view and the pointer is null to my UIView on my second view, so I can't do anything with it on my first view. All I am doing is this, as my secondViewNew is a property of my secondView. Is this possible to be done on the storyboard? I have also tried the [self.storyboard instantiateViewControllerWithIdentifier:#"beaconContentID"] method of doing things. In this project, secondViewNew is a UIView as well, and I have imported all the relevant classes.
SecondViewController *secondViewControllerInstance = [[SecondViewController alloc] init];
UIView *test = secondViewControllerInstance.secondViewNew;
NSLog(#"%#",test);
Your secondViewNew is not going to be loaded immediately after alloc/init.
You are only guaranteed to have it instantiated after the awakeFromNib method is called on your second view controller.
I'm not sure what you are trying to achieve but it sounds like you should separate your view from the view controller if it doesn't "belong" to one of them. Just subclass UIView and create an associated nib. This way you can instantiate the view whenever you need it.
Here are the steps:
Create a new subclass of UIView i.e. CustomView
Create a new nib file i.e. CustomView.xib
Add a view to your nib file and set its custom class to CustomView, add subviews as you like, etc
Finally, in the view controller where you want to use the custom view:
CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil] objectAtIndex:0];

Resources