Single UIViewcontroller, how to switch among multiple xibs? - ios

I have a subclass of UIViewController:
#interface KBViewController : UIViewController
with multiple xibs, for example, one is a Qwerty and the another is Dvorak layout:
KBViewControllerQuerty~iphone.xib
KBViewControllerDvorak~iphone.xib
So when the user click a button, Qwerty is switch to Dvorak. As you may see, the code logic is identical for both keyboard layouts. What I need is to reload the view with another xib.
Hopefully, all the buttons in the Dvorak xib will be hooks to the responding IBOutlets in KBViewController.
What is the right way to switch between the two xibs?

All Nibs has the designated File's Owner. IBOutlet and IBAction linking is done based upon the File's Owner. So you could define a view controller and two Nibs, with each Nib file's File's Owner set to the defined view controller.
That is, if you set File's Owner of all KBViewController*.xib files to KBViewController and have a KBViewController object somewhere you could load the KBViewController*.xib you want by initWithNibNamed method (recreate the view controller)
If you should maintain the same KBViewController object all along, you could create a KBViewController object without Nib. In KBViewController.m, implement loadView and manually load the UIView object with -[NSBundle loadNibNamed] method (load and change self.view programmatically).
UIView *someView = [[[NSBundle mainBundle] loadNibNamed:#"SomeNibFile"
owner:self
options:nil] objectAtIndex:0];
self.view = someView;
Note owner:self in above code. It must match with File's Owner of the #"SomeNibFile".
To change already loaded view:
id superview = self.view.superview;
[self.view removeFromSuperview];
UIView *someView = [[[NSBundle mainBundle] loadNibNamed:#"SomeNibFile"
owner:self
options:nil] objectAtIndex:0];
self.view = someView;
[superview addSubview:self.view];
More detailed explanation:
Resource Programming Guide - Loading Nib Files Programmatically

You can load your nib file manually and change the view attribute of your view controller or change any view you want, respectively.
UIView *someView = [[[NSBundle mainBundle] loadNibNamed:#"SomeNibFile"
owner:self
options:nil] objectAtIndex:0];
The objectAtIndex:0 is only correct when there is only one UIView root object in your nib file
After loading your view, you can set
self.view = someView;
Of course, if you have a subview in your view controller, then you can change that, too.

Related

Custom UIView subclass add nib subview

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.

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.

Loading custom class UIView from NIB (IBOutlets nil)

I'm trying to modularize a complex UI in several xib's.
I want to programaticly load each additional xib from file.
The xib has controls which are connected to the custom view code.
Then I try to load the xib the following way inside the main view controller:
WIFClientData *clientDataView;
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"WIFClientData" owner:self options: nil];
clientDataView = [nibViews objectAtIndex:0];
[clientDataView configure];
The problem is that when I try to configure the view:
-(void)configure
{
self.name.label.text = #"Name";
self.mobile.label.text = #"Mobile";
}
name and mobile are nil
I currently don't have File's owner connect, or set to any custom class. I only have the view which is layed in the xib, set to the custom class.
I have been reading a lot of things regarding to this but haven't found the answer yet.
Can someone help me?
Nuno
If you have a custom class WIFClientData and set a top-level view's class to WIFClientData in nib (which you then get via [nibViews objectAtIndex:0]), have IBOutlets connected to that view (not to file owner) then this should work. (works for me for custom cells)
Maybe you've misaligned something or mistyped?

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.

Resources