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];
Related
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.
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];
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.
In FatEditViewController, has a custom UIView.
FatEditViewController.h
#import <UIKit/UIKit.h>
#interface FatEditViewController
#property (weak, nonatomic) IBOutlet UIView *myview;
#end
In another UIViewController, I want to get the myview object.
I do like this:
FatEditViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"FatEdit"];
UIView *view = [vc myview];
NSLog(#"%#, %#", vc,view);
And the myview is null.
How to get the myview?
The problem here is that your myview gets instantiated only when your FatEditViewController's method viewDidLoad is called, which of course is called only when the view controller is presented on screen. One possible way to achieve what you need is to build myView programmatically (it wouldn't be an IBOutlet anymore), possibly by lazy initialising it in the getter. You would have to add the view to your FatEditViewController in the viewDidLoad method programmatically as well.
-(UIView *)myview
{
if(!_myview){
_myview = [[UIView alloc] initWithFrame:whateverFrameYouNeed];
//build the view
}
return _myview;
}
The property would have to be strong as well.
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!