I have a Tab View Controller as my root controller in an App which consists of 3 tabs let's call them View A, View B, View C.
I want to load these tabs as soon as my app starts, I think it can be done within didFinishLaunchingWithOptions function but I'm not exactly sure, does anyone know how to do it?
Thanks
For these kinds of purposes I used this approach:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
rootView = [[SGMenuViewController alloc] init];
UIViewController *tab1VC = [[UIViewController alloc] init];
UIViewController *tab2VC = [[UIViewController alloc] init];
UIViewController *tab3VC = [[UIViewController alloc] init];
UIViewController *tab4VC = [[UIViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:rootView];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:tab3VC];
NSArray* controllers = [NSArray arrayWithObjects:navController, tab2VC, secondNavController, tab4VC, nil];
tabBarController.viewControllers = controllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You basically create a UITabBarController and you put all your view there. You create a UINavigationController if you need to push/pop views in the selected tab. In the example above only the 1st and the 3rd tab have UINavigationController and so I can use self.navigationController pushViewController: on them.
Related
In iOS 8, setting the preferredDisplayMode on a UISplitViewController to PrimaryOverlay is generating the following warning:
"Unbalanced calls to begin/end appearance transitions for UINavigationController"
There is no problem if I set preferredDisplayMode to AllVisible or don't set it at all. Problem occurs for all iPads and iPhones in the simulator that I've tried. Problem occurs whether the app starts up in portrait or landscape.
Here's some very simple code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITableViewController *tableViewController = [[UITableViewController alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *masterNavController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
UINavigationController *detailNavController = [[UINavigationController alloc] initWithRootViewController:viewController];
UISplitViewController *svc = [[UISplitViewController alloc] init];
[svc addChildViewController:masterNavController];
[svc addChildViewController:detailNavController];
//svc.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
svc.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
self.window.rootViewController = svc;
[self.window makeKeyAndVisible];
return YES;
}
Wrap your display code in dispatch_async. Otherwise iOS seems to get confused with other animations running at the same time.
dispatch_async(dispatch_get_main_queue(), ^{
svc.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
});
or
dispatch_async(dispatch_get_main_queue()) {
svc.preferredDisplayMode = .PrimaryOverlay
}
I get this error on Xcode 5:
Application windows are expected to have a root view controller at the end of application launch
My code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[Flurry startSession:#"HJ4X7X6PB3942VGHQWWZ"];
screenRect = [[UIScreen mainScreen] applicationFrame];
if (screenRect.size.height > 500)
{
isFive = YES;
NSLog(#"is iPhone 5");
}else{
isFive = NO;
}
[[SQLiteOperator sharedOperator] openDatabase];
[self setupLang];
[self handleTabBarLanguage];
[self handleAppUpdatePopUpLanguage];
_banner_view_controller = [[BannerViewController alloc] initWithNibName:#"BannerViewController" bundle:nil];
[_banner_view_controller send_request];
[_banner_view_controller didHidden];
[_navigation_controller.view addSubview:_banner_view_controller.view];
[CoreData sharedCoreData].banner_view_controller = _banner_view_controller;
// Create content and menu controllers
//
DEMONavigationController *navigationController = [[DEMONavigationController alloc] initWithRootViewController:[[DEMOHomeViewController alloc] init]];
DEMOMenuViewController *menuController = [[DEMOMenuViewController alloc] initWithStyle:UITableViewStylePlain];
// Create frosted view controller
//
REFrostedViewController *frostedViewController = [[REFrostedViewController alloc] initWithContentViewController:navigationController menuViewController:menuController];
frostedViewController.direction = REFrostedViewControllerDirectionLeft;
frostedViewController.liveBlurBackgroundStyle = REFrostedViewControllerLiveBackgroundStyleLight;
// Make it a root controller
//
self.window.rootViewController = frostedViewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
If you are using storyboard click on the project name in project navigator in Xcode. Then look for deployment info. Select Main Interface as your storyboard name. In app delegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// // Override point for customization after application launch.
// self.window.backgroundColor = [UIColor whiteColor];
// [self.window makeKeyAndVisible];
return YES;
}
And now try it.
Following are the main poins I have.
I'm new to IOS development
I need to support IOS 5 and IOS 6 in my IOS app
I have a UITabBarController in AppDeligate file as follows.
UIViewController *viewController1 = [[[Grid1ViewController alloc] initWithNibName:#"Grid1ViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[VideoListViewController alloc] initWithNibName:#"VideoListViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[MusicViewController alloc] initWithNibName:#"MusicViewController" bundle:nil] autorelease];
UIViewController *viewController4 = [[[GalleryViewController alloc] initWithNibName:#"GalleryViewController" bundle:nil]autorelease];
UINavigationController *myNav1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *myNav2=[[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *myNav3=[[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController *myNav4=[[UINavigationController alloc] initWithRootViewController:viewController4];
UIImage *navBackgroundImg = [UIImage imageNamed:#"aa.png"];
[myNav1.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
[myNav2.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
[myNav3.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
[myNav4.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:myNav1, myNav2, myNav3,myNav4,nil];
4.I want to lock the rotation of every controller which is directly connected to tabBarController( ex - Grid1ViewController). But I need to rotate the some controller which are used in the app. Please refer the following drawing. A,B,C,D and E are controllers.
I tried implementing following method in controllers and AppDeligate file. But didn't work. When rotation are allowed the whole app start to rotate.
(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation{
NSLog(#"5 rotation");
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
// For IOS 6
-(BOOL)shouldAutorotate {
NSLog(#"shouldAutorotate");
return YES;
}
(NSUInteger)supportedInterfaceOrientations {
NSLog(#"6 rotation");
return UIInterfaceOrientationMaskPortrait;
}
How can I achieve this? Please help. Thank you in advance.
I am having trouble in making a tableview with navigation bar or controller on top.
I have this piece of code,
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *addNavCon = [[UINavigationController alloc]initWithNibName:#"Welcome" bundle:nil];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[self createEditableCopyOfDatabaseIfNeeded];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
NSString *documentDirectory = [self applicationDocumentsDirectory];
NSString *path = [documentDirectory stringByAppendingPathComponent:#"notebook.plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.Notes = tmpArray;
[tmpArray release];
}
However, the navigation bar never show up, while the table is doing fine. May i know what's the problem with the code?
Many thanks
You have created an instance of UINavigationController but never added it to anything. You need to add it to the current hierarchy or it will not appear.
If you wish to add the navController to the entire app, then you should do this in the App Delegate by
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *rootController = [[MyRootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
[rootController release];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}
I can't get setViewControllers to set the view controllers for my UITabBarController.
In the implementation for my UITabBarController subclass, I have:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Test";
self.navigationItem.backBarButtonItem.title = #"To Test";
NSMutableArray *aViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
UINavigationController* aNavigationController;
aNavigationController = [UIViewControllerOne alloc];
[aViewControllersArray addObject:aNavigationController];
[aNavigationController release];
aNavigationController = [UIViewControllerTwo alloc];
[aViewControllersArray addObject:aNavigationController];
[aNavigationController release];
[self setViewControllers:aViewControllersArray animated:TRUE];
[aViewControllersArray release];
}
The aViewControllersArray has the two UIViewControllers, but the viewControllers property of the UITabBarController is nil.
What am I doing wrong?
Figured it out:
In creating my UITabBarController I had been doing
[MyUITabBarController alloc]
rather than
[[MyUITabBarController alloc] init]
Adding init saved the day.