How to use MainStoryBoard to load and change UIViewController [IOS] - ios

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

Related

2 view controllers, each has its own xib, one of them is a subclass of another?

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

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 :

Why won't this view load correctly?

I have tried pushing to my ChatView view controller from my UserProfile view controller in a number of different ways:
UserProfile.m
ChatView *chatView = [[ChatView alloc] init];
[self.navigationController pushViewController:chatView animated:YES];
Also:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
ChatView *chatView = [storyboard instantiateViewControllerWithIdentifier:#"chat_view"];
[self.navigationController pushViewController:chatView animated:YES];
And also I have tried modal presentations:
[self presentViewController:chatView animated:YES completion:nil];
These don't actually crash, but they all fail to get past this bit of viewDidLoad:
JSQMessagesViewController.m
-(void)viewDidLoad
{
...
NSLog(#"We see this log");
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([JSQMessagesViewController class])
owner:self
options:nil];
NSLog(#"We do not see this log");
...
}
Additional info
The above code all works absolutely fine when I push to this view from my RecentChats view controller. The key difference, I think, is that my UserProfile view controller itself loads nibs, whereas RecentChats is a more straightforward Storyboard-built UITableViewController, and I wonder if is the nib loading in UserProfile this is causing issues.
The short answer is you are doing many fundamental things wrong and they are culminating in your code not working.
You should read Apple's View Controller Fundamentals.
To name a few problems, you are using the wrong initialize function for a View Controller. Unless a subclass designates otherwise, -initWithNibNamed:bundle:(typing from memory on my phone; sorry if I'm not exact) is the correct method.
Your view controller's main view is loaded in the -loadView method, not -viewDidLoad. If you are using a NIB, the name of the NIB should be passed into the initialize method, not loaded via -loadNibNamed:.
I could go on for a while, but you get the point.

how to use Stroryboard ViewController File in xib?

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

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