how to use Stroryboard ViewController File in xib? - ios

I want to use storyboard view controller in my xib file .
for example
I have an XIB file(like Purchase_Bill) and storyboard view controller file
(like Bill_Payment),
then I want to use storyboard view controller file(like Bill_Payment) in my xib file(like Purchase_Bill).
Can you give me a suggestion?

You will need to tie it together with code in -viewDidLoad and/or actions. One way is to load the nib:
[self addSubview:[[NSBundle mainBundle] loadNibNamed:#"MyXibView" owner:self options:nil][0]];
And the same method can be used for controllers
A second and often preferable method is to use convention to load the xib.
[[MyXibViewController alloc] init]
will load from MyXibViewController.xib if it creates a nib in the bundle.
Once you have a view controller object, you can create a segue manually like
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:#"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];

Related

How to make a view controller without a xib or a storyboard vc identifier?

I wanted to know how to make a viewcontroller without the use of xib or storyboard. So in this case, I would be getting the viewcontroller as such
PopupViewController* vc = [[PopupViewController alloc] init];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
I know it has to do something with overriding the init method since we are not using initWithCoder ?
I know I have to create a view and set it as the self.view for the PopupViewController, but I was wondering how I could do that.
EDIT: Yes it may be much easier just to make an xib file or add a view controller to the storyboard, this is to understand deeply how view controllers work.
The best place to init the view is in PopupViewController's loadView method. Something like:
- (void)loadView {
self.view = [MyViewClass new];
}
Then, in MyViewClass initWithFrame: method build all subviews and set constraints to it. If you're not using constraints, override layoutSubviews method.

Cocoa replaceSubview for nib loaded from storyboard

I've got a .storyboard that loads a .nib into it's view controller using:
NSViewController* vc = [[otherView alloc] initWithNibName:#"otherView" bundle:nil];
[[[self window] contentView] addSubView:[vc view]];
This works fine for loading in the initial NSView, however, the .nib contains several other views I would like to be able to replace with a different view when a button is clicked.
How do I specify which view in the .nib I would like to replace with another?
You have to connect other view as outlet to the NSViewController and you can call replace.

If I'm using Storyboards and want to present one view atop all my others occasionally, how do I build it?

I have my normal view controller, but once in awhile I'll present something of an options screen atop the view controller. I don't know how to set this up in a Storyboard though, as if I had it on top and then set hidden to true, it would obstruct all the other views and be rather annoying to fiddle around with.
What should I be doing in this case?
You can create the view in a separate XIB file, and then load the view from your view controller programmatically. If this view is showed often you can keep the view in a property, otherwise you can just load it from the XIB every time. To load a view contained in a XIB:
UIView *view = [[[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil] firstObject];
[self.view addSubview:rootView];
If this view is something like a settings, I'd recommend to use a separate view controller. You could use a Storyboard and then programmatically add it as a child view controller.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateInitialViewController];
[self addChildViewController:vc];
content.view.frame = self.view; // here you set the frame of your view
content.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
Are you trying to create a view that is smaller than the view controller?
If that's the case then you can create one programmatically by sub classing UIView.
But if it will cover your entire view controller then you can create a view controller with a .xib file. You can then draw everything on it using IB and then call that view controller morally.

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];

How to instantiate a custom iOS UIViewController and use it's NIB under Storyboard?

I have a project with StoryBoard (iOS 5+), with two UIViewController.
The task would be to instantiate programmatically a second UIViewController (CoordinationController) but I'd like to use it's XIB file for the user interface design.
The evident workaround to copy the XIB to StoryBoard is not working or not usable for undocumented reasons.
Even the rule would be that in case the view controller of the second storyboard has no view it will look for the same named XIB... is not working.
How to instantiate the second UIViewController and use its XIB file?
You can load the nib file and set your view in an initWithCoder: method of CoordinationController:
-(id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
UINib *nib = [UINib nibWithNibName:#"CoordinationController" bundle:nil];
NSArray *nibObjects = [nib instantiateWithOwner:self options:nil];
self.view = nibObjects.lastObject;
}
return self;
}
A very simple solution is to override the view controller's nibName property and return the name of the nib that encodes the view you want loaded:
- (NSString*) nibName
{
NSLog( #"p.s. this is the nib that the storyboard generated: %#", [super nibName]);
return #"MyViewController";
}
Note that you can access the name of the nib generated by the storyboard compiler, which would have been used. This nib is included in the app bundle, packaged inside the storyboard.storyboardc bundle.
I am somewhat surprised that you cannot return nil from this nibName implementation and get the default behavior where the loader searches for a nib using the rules described in the docs.
Alternatively (and this is cheating) you could set the desired nib name in the Storyboard itself for the view controller via a user-defined runtime attribute with key nibName. This works because there is an undocumented setter for the property, so don't actually do this!
I think this is a better solution than the accepted answer because the view will be loaded in the normal course of the view-controller's lifecycle - that is, it will be lazily loaded before the controller is presented vs up front in the init method. And it is less code.

Resources