Custom Navigation controller in Storyboard - ios

Inside this i am creating the custom navigation controller and i have view as storyboard but that view is not loading
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//create the navigation controller and add the controllers view to the window
_navigationController = [[UINavigationController alloc] init];
[self.window addSubview:[self.navigationController view]];
//check if the first viewcontroller eixsts, otherwise create it
if(self.firstViewController == nil)
{
ViewController *firstView = (ViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"mainView123"];
// ViewController *firstView = [[ViewController alloc] init];
self.firstViewController = firstView;
}
//push the first viewcontroller into the navigation view controller stack
[self.navigationController pushViewController:self.firstViewController animated:YES];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

firs you should set your navigation as rootviewcontroller then try something like this.
UIStoryboard *storyboard =[UIStoryboardstoryboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
self.firstViewController = myVc;

Related

Switching root view controller

I've an app with UINavigationController already, but i want to switch to UITabBarController, the problem is when i switch to UItab from beginning it doesn't work, so i'm switching it in a delegate method but it doesn't work either!
all code in the app delegate.
self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
self.tabBarController = [[UITabBarController alloc] init];
if ([PFUser currentUser]) {
// Present wall straight-away
[self presentWallViewControllerAnimated:NO];
} else {
// Go to the welcome screen and have them log in or create an account.
[self presentLoginViewController];
}
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
The delegate method i want to switch on it:
- (void)presentWallViewControllerAnimated:(BOOL)animated {
NSLog(#"Called:presentWallViewControllerAnimated ");
// self.navigationController = nil;
self.tabBarController = [[UITabBarController alloc] init];
PAWWallViewController *wallViewController = [[PAWWallViewController alloc] initWithNibName:nil bundle:nil];
wallViewController.delegate = self;
// Set up the first View Controller
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor orangeColor];
vc1.tabBarItem.title = #"Orange";
vc1.tabBarItem.image = [UIImage imageNamed:#"heart"];
// Set up the second View Controller
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor purpleColor];
vc2.tabBarItem.title = #"Purple";
vc2.tabBarItem.image = [UIImage imageNamed:#"star"];
// Set up the Tab Bar Controller to have two tabs
[self.tabBarController setViewControllers:#[ vc1, vc2]];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
// [self.window addSubview:tabBarController.view];
// [self.navigationController setViewControllers:#[ tabBarController ] animated:animated];
}
Remember to handle the view transition:
UIViewController *vc = // any vc that's initialized properly
window.rootViewController = vc;
[UIView transitionWithView:window
duration:0.3 // 0.0 for immediate
options:UIViewAnimationOptionTransitionCrossDissolve // several enums to choose from here
animations:nil
completion:nil];
and you don't need makeKeyAndVisible after the first time.
You called - (void)presentWallViewControllerAnimated:(BOOL)animated but in the end of didFinishLaunchingWithOptions you called
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
So the tab bar will never works !

Navigation controller is nil in the second view controller

So I've setup the AppDelegate to load a root view vc which has a navigation controller:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *vc = [[SecondViewController alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window makeKeyAndVisible];
return YES;
In my SecondViewController I have a method that then calls a push to present the third view controller:
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
[self.navigationController pushViewController: thirdVC animated:YES];
Although this doesn't work. On closer inspection the self.navigationController property on the SecondViewController is nil. Is there something I've missed on the AppDelegate to make this non-nil?
Try this as i was facing same issue but i resolved it by setting same class name and Storyboard ID name under The Custom Class header of your project.
registerViewController *regVC = [self.storyboard instantiateViewControllerWithIdentifier:#"registerViewController"];
[self.navigationController pushViewController:regVC animated:YES];
Try this:
SecondViewController *vc = [[SecondViewController alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
THere is no need of making the root view controller nil in the app delegate.

Show UITabBar without beeing RootViewController

I'm having an LoginViewController which is the RootViewController. This ViewController should not have the TabBar included. The Rest of the ViewController should contain the UITabBar, But it is not showing. If i make the tabBar the rootController it will show the tabBar in the viewcontrollers. This would also make the firsttab the rootviewcontroller which it should not.
My question is then how can i make the loginview the rootViewController without an tabBar and still show the tabBar in the other viewcontrollers?
My code:
tabBarController = [[UITabBarController alloc] init];
MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];
FixtureViewController *secondTab = [[FixtureViewController alloc] initWithNibName:#"FixtureViewController" bundle:nil];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondTab];
WorldCupViewController *thirdTab = [[WorldCupViewController alloc] initWithNibName:#"WorldCupViewController" bundle:nil];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdTab];
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navController, navController2, navController3];
navController.tabBarItem.image = [UIImage imageNamed:#"message-7"];
navController2.tabBarItem.image = [UIImage imageNamed:#"football-32"];
navController3.tabBarItem.image = [UIImage imageNamed:#"trophy-32"];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:110/255.0f green:89/255.0f blue:196/255.0f alpha:1.0f]];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
What you need to do is transition the application from the LoginViewController to the Tabbar. What I would suggest doing is replacing the LoginViewController with the TabBar as the rootViewController.
Here is some sample code, perform this action in your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
return YES;
}
-(void)transitionToTabBar
{
// Set the TabBar how you are in your sample code, this is just an example.
[self.window setRootViewController:[[UITabBarController alloc] initWithNibName:#"SomeNib" bundle:Nil]];
[UIView transitionWithView:self.window duration:0.5f options:UIViewAnimationOptionTransitionCurlDown animations:^{
[self.window makeKeyAndVisible];
} completion:nil];
}
You should use main View controller (for MenuViewController) and by using segue you can present another view controller which has TabBar in it.

TabBarController and NavigationController

I am making an application but I'm still a beginner and I'm trying to get used to the RootViewController and how it should be set.
At the beginning my application launches, I want there to be a View which is not in my tabBarController (which is set to be my rootViewController).
What I am trying to ask is, Can I have another view which is outside my UITabBarController launch first without it being in the tabBarController's items list?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FacebookFeedViewController *facebookClass = [[FacebookFeedViewController alloc] initWithNibName:#"FacebookFeedViewController" bundle:nil];
TwitterFeedViewController *twitterClass = [[TwitterFeedViewController alloc] initWithNibName:#"TwitterFeedViewController" bundle:nil];
LinkedInFeedViewController *linkClass = [[LinkedInFeedViewController alloc] initWithNibName:#"LinkedInFeedViewController" bundle:nil];
FTLFullFeedViewController *masterClass = [[FTLFullFeedViewController alloc] initWithNibName:#"FTLFullFeedViewController" bundle:nil];
/// tab button title
facebookClass.title = #"Facebook";
twitterClass.title = #"Twitter";
linkClass.title=#"LinkedIn";
masterClass.title=#"FTL";
// tab button Images
facebookClass.tabBarItem.image = [UIImage imageNamed:#"facebook_32"];
twitterClass.tabBarItem.image = [UIImage imageNamed:#"twitter_32"];
WelcomeViewController *welcomeClass= [[WelcomeViewController alloc] initWithNibName:#"WelcomeViewController" bundle:nil];
navController = [[ UINavigationController alloc] initWithRootViewController:welcomeClass];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:facebookClass];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:twitterClass];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:linkClass];
UINavigationController *navController5 = [[UINavigationController alloc] initWithRootViewController:masterClass];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,navController5,navController2,navController3,navController4,nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I know you already selected an answer but all that's doing is pushing a UITabBar view on top of an existing view, not creating a new UITabBarController view. Based on our brief conversation (latest XCode, no StoryBoards, using XIBs) you're going to want to create a xib as a UITabBarController then push it into view...
View *view = [[View alloc] initWithNibName:#"myUITabBarXIB" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: view animated:YES];
This will present your XIB file but not on top of the existing view controller when the desired action takes place.
yes! ofcourse you do.
[self.view addsubview:yourTabbar.view];
Hope this will help you.

Presenting Modal View Controller From Xib

I would like to show a modal dialog from a xib. The code that shows my window is:
self.vcSettings = [[ViewControllerSettings alloc] initWithNibName:#"ViewControllerSettings" bundle:[NSBundle mainBundle]];
[self presentModalViewController:self.vcSettings animated:YES];
When this runs though, I get a blank screen, and not what was inside of my ViewControllerSettings.xib. I imagine I'm showing the view incorrectly somehow. Any advice is appreciated.
EDIT:
I think this should be
[self.navigationController presentModalViewController:self.vcSettings animated:YES];
but for some reason self.navigationController is nil.
EDIT:
Self is a UIViewController instantiated in my AppDelegate like so:
UIViewController* viewMain = [[ViewController_iPhone alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = viewMain;
[self.window makeKeyAndVisible];
You need to create a UINavigationController, set it's root view to the XIB controller that you want to present (in your case vcSettings), then present the UINavigationController
self.vcSettings = [[ViewControllerSettings alloc] initWithNibName:#"ViewControllerSettings" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vcSettings];
[self.navigationController presentModalViewController:controller animated:YES];

Resources