Custom UIView's labels not showing - ios

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.

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)

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];

How to Add a Semi Transparent UIView to a UIViewController

Upon a certain user action, I wish to add to my UIViewController another UIView that will be half transparent; i.e. when it loads, the UIViewController view in the back will still be visible in the background, and the new UIView will appear as a layer above it.
The "Half Transparent" UIView should have several images and buttons in it, so I prefer to create a separated h, m and xib files for it so I can control it.
How should I do that?
Try this:
UIView *view = [[UIView alloc] init];
[view setAlpha:0.5];
[mainview addSubview:view]
Subclass UIView, create the nib file
Change the nib class to your custom subclass name
Change the file owner to become your view controller
In your view controller, declare a #property for the custom view using IBOutlet
Select the nib, drag from the file owner to the custom view and connect the outlet
In your button action, when you are ready to load the view, use
[[NSBundle mainBundle] loadNibNamed:#"NibName" owner:self options:nil];
Once this is done, your custom will be loaded from the nib and assigned to the property you declared.

Add subview from a xib or another scene with storyboard

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.

Resources