Objective-C: UITabBarController Black Screen - ios

Bellow i have some code that is place in the app delegate.m and is used to create a c between two ViewControllers. The creation of the tab bar works fine but when i select the setting tab there is no view it is just black.
Here is the code:
import "ViewController.h"
#import "Settings.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
UITabBarController *tbc = [[UITabBarController alloc]init];
ViewController *vc1 = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
Settings *vc2 = [[Settings alloc]init];
[vc1.tabBarItem setTitle:#"Browse"];
[vc2.tabBarItem setTitle:#"Settings"];
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]];
[self.window setRootViewController:tbc];
return YES;
}

You write the code for setting screen like this
Settings *vc2 = [[Settings alloc]init];
where is the nib file for setting screen ,
Once try like this
UITabBarController *tbc = [[UITabBarController alloc]init];
ViewController *vc1 = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
Settings *vc2 = [[Settings alloc]initWithNibName:#"Settings" bundle:nil];
[vc1.tabBarItem setTitle:#"Browse"];
[vc2.tabBarItem setTitle:#"Settings"];
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]];
[self.window setRootViewController:tbc];
[self.window makeKeyAndVisible];

try with:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tbc = [[UITabBarController alloc]init];
ViewController *vc1 = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
Settings *vc2 = [[Settings alloc]init];
[vc1.tabBarItem setTitle:#"Browse"];
[vc2.tabBarItem setTitle:#"Settings"];
[tbc setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil]];
[self.window setRootViewController:tbc];
[self.window makeKeyAndVisible];
return YES;
}

Related

How to set Root View Controller programatically in Objective C?

I am a newbie in iOS Development trying to learn how to create and set views programmatically.
i am trying to do swift statement in Obj-C
window?.rootViewController = UINavigationController(rootViewController : ViewController())
Project: Single View Application . Trying to link default Created ViewController.h
As per Krunals Answer i updated code but Navigation Controller is not shown in simulator
Cmd+Click on controller does not navigate to ViewController File
#import "AppDelegate.h"
#import "ViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIScreen *screen=[[UIScreen alloc]init];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.makeKeyAndVisible;
ViewController *controller = [[ViewController alloc] init];
window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;
Initialise your view controller ViewController before you add (use as root controller of navigation) into navigation controller stack.
Here is sample code to initialise simple view controller
UIViewController *controller = [[UIViewController alloc] init];
Here is sample code to initialise using storyboard
ViewController *controller = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"<ViewController - string identifier of your view controller>"];
Here is sample code to initialise using NIB/Bundle
ViewController *controller = [[ViewController alloc] initWithNibName:#"<ViewController - string NIB name>>" bundle:nil];
According to your code and following comment try this code only (remove other codes from your app delegate launch):
// make sure your NIB name is 'ViewController'
ViewController *controller = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
if (controller != nil) {
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: controller];
self.window.makeKeyAndVisible;
} else {
//print - your view controller is nil
}
Thanks to Krunal for detailed answer .
Thanks to dan for support
i found issue instead of self.window.rootViewController , i typed window.rootViewController.
setting self.window.rootViewController solved issue.
i dont know difference between self.window.rootViewController and window.rootViewController and reason for issue.
If some one knows answer please provide answer on comment
#import "AppDelegate.h"
#import "ViewController.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIScreen *screen=[[UIScreen alloc]init];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.makeKeyAndVisible;
ViewController *controller = [[ViewController alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:controller] ;
Delele the file Main.storyboard and let the Main interface option empty before you do it.
And add this code:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UPDATE: If you want to use storyboard with UINavigationController, try this:
Adding UIViewController and adding with UINavigationController
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];
[self setRootViewController:rootViewController];
#pragma mark - Set RootView Controller
-(void)setRootViewController:(UIViewController *)rootViewController {
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController= [storyboard instantiateViewControllerWithIdentifier:kIdentifier];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self setRootViewController:navController];
-(void)setRootViewController:(UINavigationController *)rootViewController {
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}

Modal view does not fire from child controller

My MainViewController loads another view modally.
#implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *uiViewController = [storyboard instantiateViewControllerWithIdentifier:#"splashViewController"];
[uiViewController setModalPresentationStyle:UIModalPresentationCustom];
[uiViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:uiViewController animated:YES completion:nil];
}
When I load the MainViewController directly from the AppDelegate, the modal view is loaded.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController *rootController = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
[navigationController setNavigationBarHidden:true];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
}
If I load the MainViewController as a child controller of another controller, then the modal view fails to load.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.drawerViewController.leftViewController = self.leftDrawerViewController;
self.drawerViewController.centerViewController = self.mainViewController;
self.drawerViewController.animator = self.drawerAnimator;
UIViewController *rootController = self.drawerViewController;
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
[navigationController setNavigationBarHidden:true];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
}
The main view still loads. It's only that the modal view is not created.
What's causing the problem and how can I resolve this?
You should not present another view controller from viewDidLoad method ,
by that time , current view is NOT finished with its view-hieararchy changes ,
You can present new viewcontroller after viewDidAppear is called ,
so you can move that code to viewDidAppear

I try to pushViewController but i cant

I try to push Viewcontroller it doesn't work.
AppDelegate.m (I import ViewController to use it as rootView)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
ViewController *view = [[ViewController alloc]init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:view];
return YES;
}
Next is my Viewcontroller.m
-(void)goToView{
ViewController2 *view2 = [[ViewController2 alloc]initWithNibName:#"newView" bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
I got error message:
Cannot find executable for CFBundle 0x8f9bd80</Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.p‌​latform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles‌​/CertUIFramework.axbundle>(not loaded)
2014-07-11 17:10:47.372 pushTest[2007:60b]***Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/home/Library/Application Support/iPhone Simulator/7.1/Applications/0D78CE53-F02B-467F-8250-8D2D3639A301/sta.app> (loaded)'with name 'newView'
This error can occure when you rename some files outside XCode. To solve it you can just remove the files from your project (Right Click - Delete and "Remove Reference") You re-import the files in your project and everything will be ok !
another choice
in your error code Could not load NIB in bundle means
ViewController2 *view2 = [[ViewController2 alloc]initWithNibName:#"newView" bundle:nil];
the Name newView does not match with bundle
just use this
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
in your app delegate.m add the valid Navigation controller
try this Navigation Controller Push View Controller
try this:
in AppDelegate
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
RootViewController *rvc = [mainStoryboard instantiateViewControllerWithIdentifier:#"RootViewController"];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:rvc];
self.window.rootViewController = nc;
in your Storyboard add a new ViewController with the storyBoard Identifier: "RootViewController".
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navc;
[_window addSubview:navc.view];
[self.window makeKeyAndVisible];
return YES;
}
Hope it helps.

Can't add NavigationController on TableView using UISplitView

I am having a little hard time here, forgive me if you think my problem is so easy for you.
I am trying to create app using UISplitView. The 1st View on the left is a TableView and the other one on the right is just a normal view.
This is my Code for in AppDelegate.m for UISplitView.
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "DetailViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterViewController *masterVC = [[MasterViewController alloc]init];
DetailViewController *detailVC = [[DetailViewController alloc]init];
UISplitViewController *splitVC = [[UISplitViewController alloc]init];
[splitVC setViewControllers:[NSArray arrayWithObjects:masterVC,detailVC,nil]];
[self.window setRootViewController:splitVC];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now, I want to add a Navigation Bar on the TableView, I just don't know how to add if I am using SplitView,but I can when I am using a single TableView.
This is my Code in AppDelegate.m using a single View Application that uses TableView. (This is working)
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//create UINavigationController
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Hope you can understand what I'm trying to say. I can't post images since i don't have enough reputation. AGAIN.. The question is "How can I add Navigation Controller in my TableView if I used UISplitView?"
Do you think it will be easy for me if I use storyboards instead of using two XIB files?Hope you can help me.
Thanks in advanced!
try this code
in AppDelegate.h file
UINavigationController *detailNavigationController;
UINavigationController *masterNavigationController;
UISplitViewController *HomeSpilitView;
HomeSpilitViewController *HomeMster;
HomeDetailsViewController *HomeDetailsViewControllers;
in AppDelegate.m file
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSMutableArray *array = [NSMutableArray array];
HomeSpilitView = [[[UISplitViewController alloc] init]autorelease];
HomeMster = [[HomeSpilitViewController alloc] initWithNibName:#"HomeSpilitViewController" bundle:nil];
masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:HomeMster] autorelease];
HomeMster.title=#"Title home";
masterNavigationController.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
[array addObject:masterNavigationController];
HomeDetailsViewController *HomeDetailsViewControllers = [[HomeDetailsViewController alloc] initWithNibName:#"HomeDetailsViewController" bundle:nil];
detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:HomeDetailsViewControllers] autorelease];
detailNavigationController.navigationBar.tintColor =[UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:0.1];
HomeDetailsViewControllers.title=#"details title";
HomeMster.objHomeDetailsViewcontroller=HomeDetailsViewControllers;
HomeSpilitView.delegate = HomeDetailsViewControllers;
[array addObject:detailNavigationController];
[HomeSpilitView setViewControllers:array];
[self.window setRootViewController:HomeSpilitView];
[self.window makeKeyAndVisible];
return YES;
}

login view does not load before main ViewController in Xcode 4

Here is the layout of my app.
ApplicationName
LoginViewController.h
LoginViewController.m
LoginView.xib
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController_iPhone.xib
ViewController_iPad.xib
Currently in my AppDelegate.m I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
self.loginViewController = _loginViewController;
[_loginViewController release];
[_window addSubview:[loginViewController view]];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
My LoginView.xib has it's File's Owner defined as LoginViewController.
I was at first getting an error stating: reason: '-[UITableViewController loadView] loaded the "LoginView" nib but didn't get a UITableView.'"
I changed UITableViewController to UIViewController and I was able to run the app without an error. The only problem now is that my LoginViewController does not load. I see the blank grey ViewController_iPad.xib loading.
What am I missing here?
I can post up any other code that would be useful.
Thanks in advance!
You should be setting your window's root view controller to self.loginViewController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
[self.loginViewController release];
self.window.rootViewController = self.loginViewController;
[self.window makeKeyAndVisible];
return YES;
}

Resources