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.
Related
I have a custom UIView subclass let's call it CustomViewA which I init with initWithFrame: and add some UIViews programatically (like a UILabel and so on). Now there is need for another view to be added to CustomViewA so I created a nib which I lay out some GUI elements inside (one being a UISegmentedControl)
Now I'm having some issues on how to correctly add this nib as a subview to CustomViewA. Do I need to create .h/.m files for the nib? I want CustomViewA to receive the actions when the segmented control changes values.
[[NSBundle mainBundle] loadNibNamed:#"MyNibName" owner:self options:nil];
[self.view addSubview:self.nibView];
In the nib, make sure that the File Owner's class is set to the view controller you are adding it to.
You can add properties and IBActions like normal from the nib as well.
Do I need to create .h/.m files for the nib?
No, you needn't.
How to receive the actions when the segmented control changes values ?
You can set a tag number for segmented control in your xib, it should be unique in all the subviews of the view in your xib.
You can get the segmented control with the code. UISegmentedControl *segmentedControl = (UISegmentedControl *)[view viewWithTag:1024];, once you get the segmented control, you can add an action with the code [segmentedControl addTarget:self action:#selector(action:) forControlEvents:UIControlEventValueChanged];
Edit: How to get the root view of xib?
Use the code below:
UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:#"YourXibName" owner:nil options:nil] firstObject];
I finally figured out what was happening. The nib that I added to CustomViewA was added outside CustomViewAs frame. So apparently when a subview is outside the superview's frame it will not intercept touches.
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.
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.
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
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];