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 :
Related
I have a custom class inherited from UIViewController. Let I call it "VC1". I need to subclass it (create "VC2" with its own xib). Then I try to create and use VC2. The problem is VC2 used but with xib from VC1.
How to solve this issue? May it happen because of VC1 is written in Obj-C and VC2 in Swift?
Initialization code:
let vc = ViewController2(nibName: "ViewController2", bundle: nil)
This way should usually work. But my case is special:
First view controller contains code:
NSArray* items = [[NSBundle mainBundle] loadNibNamed:#"ViewController1" owner:self options:nil];
IRSubscriptionInfoView* pageContentView = [items objectAtIndex:1];
So in the code for vc2 I still try to get some info from nib for vc1.
could be fixed by replacing #"ViewController1" -> [self nibName] and adding a check for items.count
This is the answer to my own question
I think many developer have tried this, how to make MainStoryBoard to load many UIViewController with xib or use NIB method, there is the explaination :
I have one MainStoryBoard and connected with class ViewController that use UIViewController
I have LoginViewController, HomeViewController also each .xib file
The problem is i want separate each logical code at each controller but i addSubview to MainStoryBoard exp: LoginViewController.xib the logical at LoginViewController.swift from there i want got to HomeViewController when loged in ...can anyone solve that's problem ?
You need to override the initWithCoder method in the object-c class.
if ((self = [super initWithCoder:aDecoder])) {
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
For more detail follow this link
Add subview from a xib or another scene with storyboard
Quick question. Is this possible to create storyboard to landscape orientation? I have to change some positions of elements in views and I can't use auto-layout to deal with it. Of course I can write it in code, but I think it's unnecessary. I'm quite new in iOS developer, but I was working as Android developer. There we could create different folders for every instance. I think it should look like this:
Main.storyboard
Main_iPad.storyboard
Main_landscape.storyboard
Main_landscape_iPad.storyboard
Of course creating different files is not a problem. Problem is: how to link it properly in my code/plist/etc.
Warning: Don't create a separate nib/storyboard for landscape version of the same view unless your layout is completely different. Auto-Layout should handle layout changes upon orientation change.
Assuming you have good reasons to do this:
You can have different nibs/storyboards for the same view/view controller. Just implement each as if you have only one nib/storyboard. Then create the view/view controller as follows:
For nib:
NSString *nibName = #"NAME OF ONE OF THE NIBS";
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
id view = [nibs firstObject]; // Ideally, iterate through the array and check class, then return the view.
For storyboard:
NSString *storyboardName = #"NAME OF ONE OF THE STORYBOARDS";
NSString *viewControllerIdentifier = #"VIEW CONTROLLER IDENTIFIER";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerIdentifier];
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.
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];