adding a uinavbaritem across all views - objective-c

I have a uitabbarcontroller with uinavigation controller for 2 view controllers. i am trying to have a uinavigationitem (something like "settings" that exist in most of the apps). I am wondering if I need to define this item in every view (tabOneViewController, tabTwoViewController, etc.), or is there a global way to define this buttons so it will stay when I move between different tabs?
here is how I create my tab/nav controllers in my AppDelegate:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

Related

Orientation issue in ios8 when setting RootViewController

I'm working on an application in which i'm setting the rootViewController as,
if(token){
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* viewController = [sb instantiateViewControllerWithIdentifier:#"Home"];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:viewController];
[navigationController setNavigationBarHidden:YES animated:NO];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
[self.window setRootViewController:navigationController];
}
when this piece of code works the orientation is not working properly, the status bar rotates but the view stays the same.
This works perfectly in iOS7.1 and below but when it runs on iOS8.1 we get this issue.

Set view controller as the first controller in UITabBarController

I have this code in my appDelegate.m that implements a UINavigationController with a UITabBarController:
FristViewController *primeiro = [[FristViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] init];
[nav1 pushViewController:primeiro animated:YES];
FilesViewController *segundo = [[FilesViewController alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] init];
[nav2 pushViewController:segundo animated:YES];
InfoViewController *terceiro = [[InfoViewController alloc] init];
UINavigationController *nav3 = [[UINavigationController alloc] init];
[nav3 pushViewController:terceiro animated:YES];
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.viewControllers = [NSArray arrayWithObjects:nav2, nav1, nav3, nil];
nav1.tabBarItem.image = [UIImage imageNamed:#"tab1.png"];
nav2.tabBarItem.image = [UIImage imageNamed:#"tab2.png"];
nav3.tabBarItem.image = [UIImage imageNamed:#"tab3.png"];
self.window.rootViewController = tab bar;
It's all right with this code, in my case when I beginning my app appearing on the first controller is FilesViewController because it is the first in the order of Tab Bar, but in my case I would like the first controller was the FristViewController without changing the order of items in the tab bar, how can I do this?
Go to your storyboard or xib where you created your TabBar. Drag the First controller to 1st position. Or programmatically you could set tabBar.selectedIndex = 0;

UINavigationController with UISegmentedControl in AppDelegate to switch rootviewcontroller of UINavigationController

I would like to create an app with a navigation controller as a window root view controller and a segmented control in a title view of the navigation controller to switch its root view controller
Problem: segmented control is not present after adding it to nag controller
Code:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
FirstViewController * fc = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:fc];
[fc release];
self.window.rootViewController = nc;
NSArray * array = [NSArray arrayWithObjects:#"GPS",#"List",#"Map", nil];
UISegmentedControl * sc = [[UISegmentedControl alloc] initWithItems:array];
sc.frame = CGRectMake(0, 0, 250, 50);
sc.segmentedControlStyle = UISegmentedControlStylePlain;
[nc.navigationItem setTitleView:sc];
[sc release];
[nc release];
[self.window makeKeyAndVisible];
return YES;
}
You are setting titleView for NavigationItem instead set Item for it.
NSArray * array = [NSArray arrayWithObjects:#"GPS",#"List",#"Map", nil];
UISegmentedControl * sc = [[UISegmentedControl alloc] initWithItems:array];
sc.frame = CGRectMake(0, 0, 250, 50);
sc.segmentedControlStyle = UISegmentedControlStylePlain;
To set in title view use this line
[ViewController.navigationItem setTitleView:sc];
To set Left or Right You can not directly add Item to Navigation bar, so create BarButtonItem with your segmentControl
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:sc];
add it to ViewController not to NavigationController
[ViewController.navigationItem setRightBarButtonItem:segmentItem];

App Opening to Blank Black Screen

I am making an app that embeds a navigationcontroller into a tabbarcontroller. Now when I open the app I am getting just a blank black screen.
Here is my code
PDCFirstViewController *viewController1 = [[PDCFirstViewController alloc]
initWithNibName:#"PDCFirstViewController" bundle:nil];
PDCSecondViewController *viewController2 = [[PDCSecondViewController alloc]
initWithNibName:#"PDCSecondViewController" bundle:nil];
ViewController *viewController3 = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc]
initWithRootViewController:viewController3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray
arrayWithObjects:viewController1,viewController2,navigationcontroller, nil];
[self.window makeKeyAndVisible];
Do I need to add something or do something different to make the app display? Any assistance would be great! Thank you!
You are missing rootviewcontroller
Add this
self.window.rootViewController = self.tabBarController;
Hope it helps you..
self.window.rootViewController = self.tabBarController;
self.window.rootViewController = self.tabBarController;
That should do it

Root View Controller items are not clickable

After some great trouble, I was finally able to load my first Navigation / ViewController, only now none of the sub items are clickable. I mean, I have a two text fields, two buttons, and a switch, but none of them do anything when clicked. I know it is not a problem with my xib file, as these elements behave in the xib simulator for iPad.
This is all the code my application uses so far, and I don't see why it should not be clickable.
MainWindow.xib = empty nib file
int retVal = UIApplicationMain(argc, argv, nil,
#"iPadAppDelegate");
application: didFinishLaunchingWithOptions:
self.window = [[UIWindow alloc] init];
UIViewController *login = [[UIViewController alloc] initWithNibName:#"iPadLoginView" bundle:nil];
UINavigationController *nav_controller = [[UINavigationController alloc] initWithRootViewController:login];
self.window.rootViewController = nav_controller;
[self.window makeKeyAndVisible];
return YES;
replace your
self.window = [[UIWindow alloc] init];
with
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Replace
self.window.rootViewController = nav_controller;
with
[self.window addSubview:nav_controller.view];