Interface Builder - How to create a custom UIView with many subviews - ios

How can I create a custom UIView (with many subviews, UITextFields etc) in interface builder?
I don't want a viewController with NIB just a simple UIView, with lots of subviews, created in IB that I can then just alloc init and use, is this possible?

Yes, you can create a UIView in a nib -- when you create a view based nib, that's what you're creating, a UIView. There is no view controller (though often, you make a view controller the File's Owner of the nib).
You would need to create a custom view class, and change the class of the view on the xib to that custom class, to hookup IBOutlets in that view. When you want to use the view in a controller, you can instantiate it like this:
UINib *nib = [UINib nibWithNibName:#"CustomView" bundle:nil];
CustomView *view = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
The limitation of this method, is that your outlets belong to the view class and not the view controller, which may not (but could be) be the right thing to do in a MVC sense.

Related

When subclassing UIView with a XIB file, why do I need to have a contentView property as a subview to display the XIB?

So I have a subclass of UIView - let's call it CustomView - with a XIB file for the layout of its subviews.
In interface builder I set the Custom Class of the top level view to CustomView and made a class method to load the XIB and return that top-level view when I need to use it elsewhere in the app.
CustomView.m
+(instancetype)newCustomView
{
CustomView *customView = [[[NSBundle mainBundle] loadNibNamed: #"CustomView" owner: nil options: nil] firstObject];
...
return customView;
}
ViewController.m
-(void)viewDidLoad
{
...
CustomView *firstCustomView = [CustomView newCustomView];
[self.view addSubview: firstCustomView];
}
This all works and I can access CustomView's outlets just fine, but it feels pretty hacky. Looking into the conventional way to initialise a UIView subclass with an associated XIB I found a single article using this same method, but the majority of tutorials use some variation of:
Set the File's Owner to the UIView subclass. (Don't set a custom class for the top-level view)
Add a contentView outlet to the class and set it to the top-level view in interface builder.
Make a method to load the Nib and add the contentView as a subview of the class.
Call this method in both initWithFrame: and initWithCoder:.
This works too, but I don't understand is why it's necessary to have a contentView property set to IB's top-level view as a subview. If CustomView is already a UIView why can't I just set itself to that top-level view? Wouldn't that be more straight forward? I feel like the more I try to understand it the less it makes sense.
Cheers for any help!

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)

Load UIView with Nib and UIView class

I've tried this class
https://github.com/autresphere/ASDepthModal
i want it to popup like it does but i want to be able to set the labels programmatically, since i need the to change depending on what day it is.
I'm using storyboard, so i've created a .xib and uiview.h and uiview.m. In my main UIViewController i have:
xibContents = [[NSBundle mainBundle] loadNibNamed:#".xib" owner:self options:nil];
testView = [xibContents lastObject];
in my .xib i have set the file owner to my uiview class, this create a problem: NSUnknownKeyException
When i set the uiiew inside my .xib to my uiview class the application will load and i can open it just like it should, but i'm not able to change the state of the label programmatically? I'm complety lost here!
Typically speaking, UIViews do not have access to IBOutlets. Apple kind of intended xibs to only be assigned to UIViewControllers.
However, you can load a view from a xib in two ways:
1) Create an extra xib to use in your UIViewController. Set the File's Owner to your view controller, and the class name of the view to your custom view class. In interface builder, this is under "custom class". You can set the view as a IBOutlet, and iOS will create an instance of your custom class when your UIViewController loads the xib and sets itself as owner (like you tried above, but only from within a controller class)
2) Load a xib in a UIView class, and set self to the resultant object:
- (id)init {
self = [super initWithFrame:CGRectMake(0, 0, 1024, 352)];
if (self) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:#"TCSNumberKeyPad" owner:self options:nil];
[[nib objectAtIndex:0] setFrame:CGRectMake(0, 0, 1024, 352)];
self = [nib objectAtIndex:0];
}
return self;
}
In either case, you will need to retrieve your label via code rather than IBOutlet properties. You can find your label in the subviews property:
UILabel* myLabel = [self.subviews objectAtIndex:0];
Actually got this to work. I took the wrong approach. I found it simpler to just create the view and populate it with background image and labels when the button got clicked. would have been simple to do it in a UI designer, but this wasn't that hard actually.
Thanks to the people who helped me :)
The file's owner should be the view controller, not the view itself. The view can have outlets to the labels. The view should be set to your custom class in your nib.

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