Dismissing NIB loaded with loadNibNamed - iphone

I'm loading a nib (XIB) correctly from within a view controller with the following code:
self.myView.view = (MyView *)[[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self.myView options:nil] objectAtIndex:0];
Now, later on in my flow I have another UIView that gets displayed. However, I ONLY want this view to be displayed once a certain condition exists AND myView.view has been removed from the current self.view.
What is the correct way to remove a subview that has been added to the main view as I've done above? I can add my header and main of MyView if needed, but mainly just curious about the correct approach in general.
Thanks for the help in advance! This is bugging me terribly!

[self.myView.view removeFromSuperview];

Related

Adding UIView subView not working

I have a UIViewController class MyClass that initially had no XIB, and was initialized programmatically. Now, I don't want to load it from an XIB, but I do want to make a small 50x50 UIView (settings overlay view), and I want to add it MyClass, so instead of programatically declaring the new settings overlay view, I thought I would create an XIB file, set the file's owner to MyClass, declare in MyClass.h an IBOutlet UIView *settingsOverlay, and link it in the XIB.
Then in the viewDidLoad method, I do [self.view addSubview:settingsOverlay], but for some annoying reason it doesn't work. It just doesn't appear. I tried creating a simple UIImageView programmatically and adding it to the subView, and it works just fine, but when done through the XIB, it doesn't work. Can anyone point out what could possible be wrong? Please let me know what other details I might need to include.
If you are trying to add a view using xib then you need to use loadNibNamed method.
[[NSBundle mainBundle] loadNibNamed:#"settingsOverlay" owner:self options:nil];
You can refer developer link for more info - http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/NSBundle_UIKitAdditions/Introduction/Introduction.html
Creating an XIB for the settings overlay view and setting it's owner to MyClass does not implicitly cause that XIB to be loaded as a result of manually instantiating MyClass. You would have to manually load the settings overlay view XIB in MyClass viewDidLoad and add it as a subview. Loading it and passing owner as self will cause it to be bound to the IBOutlet you created, but you still have to add it as a subview.
The code in viewDidLoad would look like this:
[[NSBundle mainBundle] loadNibNamed:#"OverlayView" owner:self];
[self.view addSubview:self.overlayView];
You need to load the MyClass xib before settingsOverlay will be set. Try adding this line before your addSubview call.
[[NSBundle mainBundle] loadNibNamed:#"MyClass" owner:self options:nil];

UIViewController subclass to support both NIB loading and non

I'm in the midst of creating a framework to be used internally, and I'm running into issues making a flexible view controller with a custom view.
The goal is to have an implementation similar to UITableViewController:
If the view controller is initialized with a nib (or if a nib of the same name is found), it uses the outlets defined therein to define it's view. It is somewhat up to the developer to ensure the right subclass of UIView is used.
If the view controller is initialized without a nib (or if no nib can be found), it creates it's own view, using the correct UIView subclass.
As a first stab, I overrode -loadView to create the custom view. This is what you would do if you weren't concerned with nibs, and it works great. However, if I try to load a nib (using, for example, -initWithNibNamed:bundle:) the -loadView method executes and the nib is ignored.
I've also tried using -nibName to determine if a nib name was passed, and this PARTIALLY works, but fails if nil was passed (which is still valid, and should absolutely still work if there is a nib of the same name as the view controller).
Keep in mind that I'm building a framework that will be used by other developers. "Flexible" and "Foolproof" are the keywords.
Any help at all would be more than I have now. Thanks much.
Edit: Solved
As was alluded to by #bunnyhero's suggestion below, checking for the availability of the nib during -loadView and attempting to recreate it's default implementation turned out to do the trick. I've settled on something like this:
- (void)loadView {
NSString *nib = self.nibName;
NSBundle *bundle = self.nibBundle;
if(!nib) nib = NSStringFromClass([self class]);
if(!bundle) bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:nib ofType:#"nib"];
if(path) {
[bundle loadNibNamed:nib owner:self options:nil];
return;
}
// Create custom view programmatically here.
}
Thanks #bunnyhero
What if you combine your nibName nil check with a manual check for a nib with the same name as the view controller?
Maybe something like this (untested code off the top of my head):
if ([self nibName] != nil || [[self nibBundle] pathForResource:NSStringFromClass([self class]) ofType:#"xib"] != nil)
{
// nib file exists...
}
I admit this is kind of crude and indirect :)

iPhone SDK: CustomControls as in C#

I have a UIViewController. The UIViewController has a NIB with one outlet - a UIView, containing several buttons and labels. Imagine, it is something like a UIDatePicker.
In order to not be forced to copy and paste all the controlling code into a new environment, I was trying to encapsulate the UIView into a separate UIView subclass with an own NIB, sort of a C# CustomControl approach.
From a controlling (other) UIViewController I'm instantiating the view from the NIB
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
selectorView = (DateTimeSelectorView*)[nibObjects objectAtIndex:0];
selectorView is a property in the using UIViewController class. The problem: "initWithFrame" of my UIView is never called. My UIView is covering the whole space (320x480), whereas it should have a smaller size. How can I achive this? Furthermore the UIView seems to hide all other controls, instantiated from the UIViewController class.
Regards
When a view is instantiated by the nib loader, initWithFrame: isn't called. The nib loader calls initWithCoder:. You should implement initWithCoder to perform any initialization, including setting the frame.
So here is a "HowTo" for the question: How to embed a self-contained UIView into my UIViewController with NIB?
1) Say you have a UIView with a button and a textfield, making some login. You have the UI in a separate NIB called Login.xib. The functionality is in Login.m and Login.h, a subclass of UIView. The class name is "Login". Take care, that the class is set properly in Login.xib. Everything is fine.
2) Now you want to use this "out of the box" in a new app.
3) Drag the three files (Login.m, Login.h and Login.xib") into your new UIView based project
4) Add a property in your UIViewController class, pointing to your Login class (of course, include the Login.h first)
5) Open IB with Login.xib and set the file's owner to your current UIViewController class
6) Connect the main view of your Login.xib with the property defined in UIViewController (!! this is important !!)
7) Add the following to your viewDidLoad in UIViewController (supposed, the name of your property is "myLogin")
[[NSBundle mainBundle] loadNibNamed:#"Login" owner:self options:nil];
myLogin.frame = CGRectMake(0 ,100, 320, 200); // Optional, you may also use the initial bounds
[self.view addSubview:myLogin];
The view will appear where you let it appear. Other controls from your superview will be available too.
It took me several hours to find that out. There is here and there some scattered info, but I didn't find a complete "how to" for that simple task up to now.
Regards

How do I get a view in Interface Builder to load a custom view in another nib?

I am making a custom widget that I would like to use in multiple nibs. So I make a new view nib Screen3, add some buttons, and now want my UIAwesomeSauce widget.
If I just add a view and then change the Class Identity, it doesn't get the subelements from the UIAwesomeSauce nib. Same thing if I go to the Library and switch to Classes. It seems only a UIViewController has the field for "Load from nib", which would be beautiful.
I know I can load the UIAwesomeSauce nib from code, get the top level objects, and place it by hand. But the point of IB is so you don't have to place things in code. Even better would be if I could get UIAwesomeSauce to show up in the Library list itself.
SOLVED - by Nimrod - READ ON FOR EXPLANATION AND CODE
Anyway, dood, that is great. I can make my own widget classes now for goofy Awesome stuff. Make the FileOwner of your UI nib your class, and in it just have a normal UIView with all your stuff. (The single view in the widget's nib can't be the class itself, or you get recursive in initWithCoder.) Then in the nib you want to use it, make a vanilla UIView and change its class. You won't be able to actually see the widget inside that square, but deal.
Self is now a blank view, and tMyActualSelf is the single view that you did the work in in the other nib. Yay!
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
UIView *tMyActualSelf = nil;
// Initialization code
NSArray *tNibItems = [[NSBundle mainBundle] loadNibNamed:#"UIAwesomeSauce" owner:self options:nil];
for (id object in tNibItems)
{
if ([object isKindOfClass:[UIView class]])
tMyActualSelf = (UIView *)[object retain];
}
if( tMyActualSelf )
{
tMyActualSelf.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self addSubview:tMyActualSelf];
}
}
return self;
}
I feel your pain when it comes to the UIViewController being the only thing you can specify a nib load with, but I think it's because there has to be a File's Owner that's a controller, or something like that, though I'm not sure why a UIView can't be a File's Owner.
I think you can put things in the library under Custom Objects but I think you'd be limited to putting a UIView in there with the class just set to a custom subclass of UIView. I actually looked into trying to extend interface builder and it looks like you can do it for OS X stuff, but not for iPhone stuff. It involves compiling code for the new widget in interface builder to tell it how to draw the widget just within IB. I think there's actually an OS X template for this.
I think what I'd probably do is create a main view in a nib file that contains all the subviews, then in initWithCoder: in the UIAwesomeSauce thing open the nib file, load the main view, and just place it inside self so that you have an "unnecessary" view between the UIAwesomeSauce view and the subviews (or sub subviews as the case would be.) I'm not sure how inefficient that is, but in messing with some stuff it looks like some views like UIWebView seem to have one of these seemingly unnecssary intermediate views (IIRC).
If you come up with a better solution please tell me because I'd like to know too.
EDIT
Now that I look at it, I think the UIView CAN be the file's owner, you just can't set it up in IB. Basically you'd call the nib load something like this in the UIView subclass:
[bundle loadNibNamed:#"UISpecialSauce" owner:self options:...]
I think if you create outlets in UISpecialSauce or whatever then they should be assigned correctly because of the owner:self.
Theres a cocoapod that might actually help with that:
https://github.com/mobilejazz/NibWrapper
It provides a wrapper view that loads a child view from another NIB. You can specify the name of that NIB inside interface builder via a runtime attribute.

Switching nib files

I have a View Controller inside my MainWindow.xib file that loads the nib "Cover" when loaded. Cover (and the rest of my pages) is simply a nib file containing it's Owner, First Responder and a View. There is also an associated class declaration.
In MainViewController.m I have:
- (void)viewDidLoad {
[[NSBundle mainBundle] loadNibNamed:#"Cover" owner:self options:nil];
[super viewDidLoad];
}
This successfully loads the Cover of my app. On a button press I'd like to have a function switch Cover with Page1. I tried:
-(IBAction)funcGoToPage:(id)sender{
[[NSBundle mainBundle] loadNibNamed:#"Page1" owner:self options:nil];
}
This function is also in MainViewController. I know the function is being called but It doesn't seem to do anything. Is the new nib showing up underneath the current nib? Do I have to release the current nib?
Depending on how you want users to navigate, you either need to add the view of the nib as a subview to your current view, or push a new viewController on to the stack using a navigationController. Here is a tutorial on using a navigationController. Or, if you just want to exchange them, here is a link to a SO answer for just that.