object instantiated by UINib not showing its proper class? - ios

I have a xib file, which is a view, and I have designated it as a FooView class.
When I allocate it:
UINib *fooViewXib = [UINib nibWithNibName:#"FooView" bundle:[NSBundle mainBundle]];
FooView *fooView = [[fooViewXib instantiateWithOwner:self options:nil] lastObject];
fooView.lolLabel.text = #"lol";
If I then ask it:
[fooView isKindOfClass:[FooView class]];
It says no...
Yet, if I NSLog the fooView object, it says it's a fooView. And if I NSLog the class of the fooView object directly, it shows as a FooView class.
Why does isKindOfClass not correctly identify this object, and how can I do so?

Instead of using lastObject, loop through the array until you find a view with the correct class. Do it like this
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"FooView" owner:self options:nil];
FooView *view;
for (id object in nibViews)
if ([object isKindOfClass:[FooView class])
view = object;
// do whatever you want with view
This will ensure that you get a view with the correct class.

There's no need to iterate through the array created by instantiating a nib when you can connect the object you want to an outlet. You don't say what class your code is part of, but lets assume it's a view controller. You can simply make fooView an outlet in your view controller:
#interface MyViewController : UIViewController
...
#property (strong, nonatomic) IBOutlet FooView* fooView;
#end
Set the type of File's Owner in the nib to MyViewController and connect the foo view to the fooView outlet of File's Owner. Now in your view controller you can just pass self as the nib's owner:
UINib *fooViewXib = [UINib nibWithNibName:#"FooView" bundle:[NSBundle mainBundle]];
[fooViewXib instantiateWithOwner:self options:nil];
After that, the fooView property will be connected to the right view.

Related

How to load view of xib file in another view controller

Xib File
#interface SearchingPropertyViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *searchingView
i added the below code to the button action in another ViewController
SearchingPropertyViewController *ps = [[[NSBundle mainBundle] loadNibNamed:#"SearchingPropertyViewController" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:ps.searchingView];
I just wanted to load searchingView in another view controller
Try this! Simple solution :) Please find view controller loadview method. https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview
SearchingPropertyViewController *searchPVC = [[SearchingPropertyViewController alloc]initWithNibName:#"SearchingPropertyViewController" bundle:nil ];
[searchPVC loadView];
[self.view addSubview:searchPVC.textLabel];

Extending MyViewController from another .storyboard

I try to create something like SDK - pack of default views, controllers wich developer can extends and customise
I have a problem with understanding how user can extends my UIViewController if his views didn't make programmatically, but created in separate .storyboard.
For UIView, we do something like this
-(id)init{
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"myXib" owner:self options:nil];
return [subviewArray objectAtIndex:0];
}
How UIViewController can get own view from .storyboard?
I can't use .xib files becose my viewControllers have TableViews with dynamically TableViewCells.
UPD:
I think it easy create UIViewController programmatically , add coupe views and all ChildViewControllers will have the same controls as his ParentViewController.
I want to add not programmatically this controls, but use .storyboard for this. And I can't imagine - how to bound this.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
MyViewController * myVC = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerIdentifier"];
The one who is using your library, can simply set the class of his ViewController to your ViewController from storyboard like :

Protocols & Delegate when using ContainerView

I have a UIViewController (root) with a ContainerView that loads a nib programatically:
UIView *containerView = [[[NSBundle mainBundle] loadNibNamed:#"KeyboardView"
owner:self options:nil] objectAtIndex:0];
[self.view addSubview:containerView];
I have a class file (.h and .m) hooked to this .xib file (UIView).
My .xib file (UIView) has a button and an action.
When this action is invoked and executed I removeFromParent my .xib file.
How can my ViewController.m (root) handle the exact time I removeFromParent my nib file?
I tried to use Protocol & Delegate but it failed.
I tried to set delegate=self on prepareForSegue Method but was unsuccessful.
My question is:
My UIViewController (root) invokes my keyboard (.xib) programatically.
All action in my keyboard are handled by the class hooked to the .xib.
After action’s job is done I removeFromParent the .nib file (I simulate a dismiss).
How can methods in ViewController.m (root) know that I dismissed my .xib file?
I'm not exactly sure what you are doing here, but I think I understand your question.
In your interface, declare a weak property for the keyboard view.
#property (weak, nonatomic) UIView *keyboardView;
Then when you load the keyboard
UIView *containerView = [[[NSBundle mainBundle] loadNibNamed:#"KeyboardView"
owner:self options:nil] objectAtIndex:0];
[self.view addSubview:containerView];
self.keyboardView = containerView;
Now you can test self.keyboardView != nil to know if the keyboard is loaded.
This works because keyboardView is weak so when it's removed from it's parent view the property will be set to nil.
NOTE: No protocol or delegate is needed.

UIViewController in storyboard with nib file

Hi can I ask if can I mix the xib with my UIViewController in storyboard? Like they share a controller in their files owner because I'm planning to create a expandable view by using nib as the expandedview and I want to pass a value from nib file to the UIViewController in storyboard. Thanks.
I don't recommend you mix xib and view controller from storyboard and hook them all together.
If you want to add a UIView as an extended view to your view controller you can do something like this:
// Load the first view inside our xib from main bundle
UIView *view = [[NSBundle mainBundle] loadNibNamed:#"nib_name" owner:self options:nil][0];
// Define new X,Y to our view
float new_x_position = 0;
float new_y_position = 400;
view.frame = CGRectMake(new_x_position, new_y_position, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame));
// UILabel inside our nib, '111' is the tag we set to our UILabel
UILabel *label = (UILabel*)[view viewWithTag:111];
// Add our view to the current view as a sub view
[self.view addSubview:view];
I hope I understood you correctly.
In storyboard you are not provided with xibs, but if you want to use them to load from nib then use :
MyViewController *viewController = [[MyViewController alloc] initWithNibName:#"CustomDownloadedXib" bundle:nil];

Connect a new xib to a class that inherits from UIView

I'm developing an iOS app with latest SDK.
I want to create a custom UIView and set layout using a XIB file.
To this XIB, I have added four UIButtons using Interface Builder.
Now I want to connect these four buttons to my custom UIView class and manage there IBActions. This is very important, I have to do it this way.
To load the xib I do:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder]))
{
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"MyCustomView"
owner:self
options:nil] objectAtIndex:0]];
}
return self;
}
I also have a storyboard and I have added an UIView to main ViewController using Interface Builder.
My question is: What do I have to do to connect the new XIB file to my custom UIView on Interface Builder?
I think I have to open this new xib on Interface Builder and set main ViewController as File's Owner, and set my custom UIView class as class for the view on this new XIB, but I'm not sure.
And, on main ViewController change the class for this new view to my custom UIView.
In Interface Builder, set the custom class to your CustomView.Make the connections to this custom class. In the whichever view controller you want to use this xib, Simply load the nib using loadNibNamed:owner:options: method.
CustomView *cView = [[[NSBundle mainBundle] loadNibNamed:#"CustomView"
owner:nil
options:nil] objectAtIndex:0];
[cView.button1 addTarget:self action:#selector(actnForBtn1:) forControlEvents:UIControlEventTouchUpInside];
[cView.label1 setText:#"sometext"];
[self.view addSubview:cView];
And do add the method actnForBtn1:(id)sender in your view controller to do different things in different view controllers.
In View.h
+(View *)loadViewFromNib;
In View.m
+(View *)loadViewFromNib{
return (View *)[[[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:0] objectAtIndex:0];
}
To load the View call
View *view = [View loadViewFromNib];
In your View.xib file set the Files Owner class to View and your Views class to View connect the Outlets only to the View, not to the FilesOwner!

Resources