I'm trying to push the Navigator Controller to a different view controller but I keep getting the error saying the nib bundle name can't be loaded. Here's the code I'm using:
SecondViewController *vc1 = [[SecondViewController alloc]
initWithNibName:NSStringFromClass([SecondViewController class]) bundle:Nil];
[self.navigationController pushViewController:vc1 animated:YES];
You have to pass the name of the nib file, not the class name:
// Assuming there is a properly set up SecondViewController.xib in your project.
SecondViewController *vc1 = [[SecondViewController alloc]
initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:vc1 animated:YES];
EDIT:
If you're using Storyboards, you have to load your GUI a bit differently:
UIStoryboard *sboard = [UIStoryboard storyboardWithName:#"StoryboardFileName"
bundle:nil];
SecondViewController *vc1 = [sboard instantiateInitialViewController];
Related
I am using SWRevealViewcontroller by using this sample:
I want to add loginviewcontroller before SWRevealViewcontroller.
What I've tried:
Added loginviewcontroller in Storyboard. Set it as InitialViewcontroller.
Inside button action.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MapViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"MapViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
But it is not working. Please help
viewController1
viewController2 *viewController2=[self.storyboard instantiateViewControllerWithIdentifier:#"viewController2Id"];
[self.navigationController pushViewController: viewController2 animated:YES];
viewController2
-(void)pushAfterLoginScreen
{
SidebarViewController *sidebarmemu =(SidebarViewController*) [self.storyboard instantiateViewControllerWithIdentifier:#"SidebarController"];
viewController3* viewController3 = (viewController2*)[self.storyboard instantiateViewControllerWithIdentifier:#"viewController2Id"];
SWRevealViewController *revealController;
UINavigationController *frontNavigationController;
UINavigationController *rearNavigationController;
frontNavigationController =[[UINavigationController alloc]initWithRootViewController: viewController2];
rearNavigationController =[[UINavigationController alloc]initWithRootViewController:sidebarmemu];
revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
self.swcontroller =revealController;
AppDelegate *app =(AppDelegate *)[[UIApplication sharedApplication]delegate];
app.window.rootViewController =self.swcontroller;
}
SWRevealViewcontroller which is already embed with the navigationController you need to pass control to SWRevealViewcontroller after successful login using presentViewController method by setting up the storyboard id to navigationController of SWRevealViewcontroller and navigate your app using below code:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"navContStoryboardID"];
[vc setModalPresentationStyle:UIModalPresentationCustom]; // optional
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; // optional
[self presentViewController:vc animated:YES completion:nil];
Hope this helps you.
i am using both storyboard and xib in my project and i want to go xib files from storyboard but can't reverse it. so how i go to storyboard from xib files. thnx in advance.
storyboard to xib:
Dashboard1ViewController *tempView = [[Dashboard1ViewController alloc] initWithNibName:#"Dashboard1ViewController" bundle:nil];
tempView.passemail = [matches valueForKey:#"email"];
[self.view addSubview:tempView.view];
it works fine but xib to storyboard i have used this code
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"loginUI" bundle:nil];
login2ViewController *vc = [sb instantiateViewControllerWithIdentifier:#"login2"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
but it is not working.
Change nil to [NSBundle mainBundle]
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"loginUI"
bundle:[NSBundle mainBundle]];
the following method is working for me. make sure that your identifier is correct
YourViewController *urViewController = (YourViewController *)[[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:#"storyboardID"];
[self presentViewController:urViewController animated:YES completion:nil];
I am having Loginview controller,I have created it with Xib and i want to push mainstoryboard on clicking loginbutton.
LoginViewController *lObjloginview = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *lObjtempview = [[UINavigationController alloc] initWithRootViewController:lObjloginview];
self.window.rootViewController = lObjtempview;
[self.window makeKeyAndVisible];
This will add loginview from xib.but after pressing loginbutton I want to load storyboard.Can anyone help me in pushing mainstoryboard which has tabbar controller inside mainstoryboard to link xib.
Load the initial view controller from the storyboard and present it
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController * initialVC = [storyboard instantiateInitialViewController];
self.window.rootViewController = initialVC;
If you know a priori the class you're instantiating
UITabBarController * initialVC = (UITabBarController *)[storyboard instantiateInitialViewController];
I'm trying to load UIViewController, but not knowing the type until runtime.
So I have some different UIViewControllers, each one with it's own .xib, and with the
following code, the .xib is showed correctly, but the UIViewController is never created, because
it's a child of UIViewController, not an exactly UIViewController:
UIViewController *vc = [[UIViewController alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
It works fine for example in this particular case, but obviously not for the rest:
Controller1 *vc = [[Controller1 alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
How can I solve it?
Thanks!
you can put your controller classe name in an NSSTring and create the object like this
UIViewController *vc=[[NSClassFromString(ControllerclassName) alloc] init];
[self.navigationController pushViewController:vc animated:YES];
i created a tabBar app with ARC. So the default set up would automatically provide 2 viewControllers;
1) FirstViewController.h,FirstViewController.m;FirstViewController_iPhone.xib, FirstViewController_iPad.xib
2) SecondViewController.h, SecondViewController.m, SecondViewController_iPhone.xib, SecondViewController_iPad.xib
I wanted to create a new view controller 'ViewController3' but during the file creation process, i can only opt to create for iPad or just iPhone (checkbox 'Targeted for iPad'). I need both iPhone and iPad xibs just like the FirstViewController and SecondViewControllers created for me. So i decided to create the xib manually and continued with the file creation without xibs.
So naturally after that i went on to manually create 2 news xibs; ThirdViewController_iPhone.xib and ThirdViewController_iPad.xib
i added this line into the original AppDelegeate file:
UIViewController *viewController1, *viewController2, *viewController3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController_iPhone" bundle:nil];
} else {
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPad" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPad" bundle:nil];
viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];
I then run the project and got this: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "ThirdViewController_iPhone" nib but the view outlet was not set.'
How do i set the outlet?
For your ThirdViewController_iPhone.xib, follow the instruction here. I think you need to do this step:
You should see "outlets" with "view" under it. Drag the circle next to it over to the "view" icon on the left bar