Objective-C: how to set keywindow to different viewcontroller in Xcode? - ios

I am using storyboards and this code but nothing is appearing in my current viewConntroller but is in my first viewConntroller. Can anybody eli me set the keywindow to the current view i am on?
Here is the code i am using to show my UINavigationBar
[[[UIApplication sharedApplication] keyWindow] addSubview:bar];
Thanks in advance

you dont set the Viewcontroller to a Window
you need to do something like this:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
this is how you add a uinavigationcontroller:
Add a UINavigationBar to a UITableViewController without a UINavigationController
if you want to present a new view controller
you simply use presentModalViewcontroller:
UIViewController *vc = [[UIViewController alloc] init];
[self.navigationcontroller vc animated:YES];
// I have written it all down just check the case and use it

Related

iOS navigation controller without navigation bar

This is how I launched by main controller in my iOS app.
UIViewController *rootController = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
With this I am getting a blank bar at the top.
I want to use a navigation controller to control the flow of screens that users are able to reach by pressing back but I don't want a navigation bar. How can I change my code to get what I want?
You can use this code right after you declare navigationController = ... in the code sample you provided:
[navigationController setNavigationBarHidden:true];
And then whenever you need to go "back," your app would run this code from within the current view controller:
[self.navigationController popViewControllerAnimated:true];

Objective-C: Status bar hides when I create the window programmatically

I am trying to create window programmatically in App delegate, but status bar hides. In info.plist it is is not set to YES. The app is perfectly working in this way, but I need to show status bar too.
I have window and navigationController property in AppDelegate.h
here is my didFinishLaunch
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//even when I used customized CGRect and used it initWithFrame, there is no status bar
self.navigationController = [[IEENavigationController alloc] init];
XYZViewController *homeViewController = [[XYZViewController alloc] initWithNibName:#"XYZViewController" bundle:nil];
[navigationController pushViewController:homeViewController animated:YES];
self.window.rootViewController = self.navigationController;
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
Thank you in advance
You could add this in the info.plist:
UIViewControllerBasedStatusBarAppearance - YES
then add this method in the view controllers you want to hide the status bar.
-(BOOL)prefersStatusBarHidden { return YES; }

Can [self.window makeKeyAndVisible]; be called before setting rootviewcontroller

My requirement is that UITabBarController is the rootviewcontroller and on very first time of app launch I want to show login procedure which is inside UINavCon, and I am displaying it through presentViewController.
I dont want the UITabBarController visible for first time and dont want to how login UINavCon popping as modal.
I want to make user experience that if app starts for first time login UINavCon should be visible. So here is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];//is it correct to call it here?
LoginVC *loginObj = [[LoginVC alloc]init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:cellPhoneNumber];
self.tabBarController = [[UITabBarController alloc]init];
self.window.rootViewController = self.tabBarController;
[self.tabBarController presentViewController:self.navigationController animated:NO completion:^{}];
return YES;
}
I am calling [self.window makeKeyAndVisible]; on second line right after uiwindow alloc init. Is it correct do this or I can experience problems like viewcontroller not receiving events or orientations notifications?
you can call it whenever you want. Calling it affects the window's z-index and screen property.
it doesnt depend on any specific content being set.
You haven't mentioned that whether you got the code working or not by using your implementation. Anyways I have done similar kind of implementation recently where we need to present login controller and then tabBarController after logging in, so just sharing my implementation.
Create your login controller and present it in didFinishLaunching method.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
LoginController *loginCObj= [[[MainScreenController alloc]init]autorelease];
UINavigationController *navigationControllerObj = [[[UINavigationController alloc]initWithRootViewController:loginObj]autorelease];
self.window.rootViewController = navigationControllerObj;
[self.window makeKeyAndVisible];
After that on succesful login in your login view controller, call an appDelegate public method
In login controller
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDel applicationLoggedInSuccesfully];
In your appDelegate file, add a method like this:
-(void)applicationLoggedInSuccesfully{
UINavigationController *nv1 = [[[UINavigationController alloc] initWithNibName:nil bundle:nil]autorelease];
TabController1 *v1 = [[[TabController1 alloc] initWithNibName:nil bundle:nil]autorelease];
[nv1 pushViewController:v1 animated:NO];
UITabBarController *tabController = [[[UITabBarController alloc] init]autorelease];
tabController.viewControllers = #[nv1];
tabController.delegate = self;
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
}
Hope it will help you.

Sidemenu with tabBarControllers in Storyboard

Hi Fellow iOS Developers, I am a newbie developing a project with 5 tab Views and on the first and second tabs I have slide out menus using Container views from example code by Michael Frederick on his GitHub page Project Link: https://github.com/mikefrederick/MFSideMenu. He is using a nib (.xib) files though I am using Storyboard to achieve the same and struck with defining the container and child views. can kindly some one advice how to modify the below code to accommodate in my storyboard.
the original code in the AppDelegate.m is
- (DemoViewController *)demoController {
return [[DemoViewController alloc] initWithNibName:#"DemoViewController" bundle:nil];
}
- (UINavigationController *)navigationController {
return [[UINavigationController alloc]
initWithRootViewController:[self demoController]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
[self navigationController], nil]];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarController
leftMenuViewController:leftSideMenuController
rightMenuViewController:rightSideMenuController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}
#end
how to modify the code to accommodate the container parent view and child views ?
where should i instantiate the code for the parent and child of the 2nd tab view ? in AppDelegate or the View Controller ?
If any other Details are required leave a comment please. Any Help Will be greatly appreciated. thanks in Advance.
I don't know if you still need this, but i had the exactly same problem today, too. What you need to do is:
remove the both methods over your app Delegate
put this in your app Delegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YOUR_STORYBOARD" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"THE_IDENTITY_OF_YOUR_SIDEMENU"];
UITabBarController *centerViewController = [storyboard instantiateViewControllerWithIdentifier:#"IDENTITY_OF_YOUR_TABBARCONTROLLER"];
[container setCenterViewController:centerViewController];
[container setLeftMenuViewController:leftSideMenuViewController]; //for the right Side, its the same way...
[container setPanMode:MFSideMenuPanModeNone]; //remove this line, if you need the pan mode
return YES;
In your Storyboard you have to put a ViewController as a subclass from "MFSideMenuContainerViewController". Mark this View as the "Initial View Controller" in the Attribute Inspector. Now use a Segue from your new Initial View Controller and let it "push" to your TabBarController. To avoid a Warning rename the Segue.
After you have done this, you can add a UIBarButtonItem to every View, you like to add the SideMenu. In the Action Method of this UIBarButtomItem you only need to do this:
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{}];
finally make sure you have a UIViewController or a UITableViewController, that is your "SideMenu" and set the right Storyboard ID.
if you are still need help, comment this...
and sorry for my english :)
You can use https://github.com/ozcanakbulut/VoovilSideMenu. It's easy to embed in a tabBarController. It uses Storyboard and Arc.

setRootViewController: doesn't work as expected after setViewControllers: have been called

I created a view controller programmatically and set it as a root controller. All worked perfect as it was expected:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
[[self window] setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
Then I added an UITabBarViewController, set its 'viewControllers' property to point to (an array to) the main viewController 'vc'.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
NSArray *controllers = #[vc];
[tbc setViewControllers:controllers];
[[self window] setRootViewController:vc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
and the view stoped to show at the screen upon launch, also there is warning about the root view controller wasn't set. After adding the string below the view of 'vc' controller is finally loaded, but there is a blank line at the bottom of the screen, as if the UIBarController modified [[UIMainScreen bounds].
[[self window] addSubview:vc.view];
I'm new to iOS development, and I'm a bit confused. It seems I don't understand some very fundamental things about the view controllers hierarchy, but after reading the "View Controller Programming Guide" by Apple, I still don't understand where am I wrong.
The setRootViewController: method should auto assign the _view of argument view controller as default view of the window, but it doesn't happen if the named view controller was already previously pointed by viewControllers property of UITabBarViewController. Though I checked the debugger and found that 'vc' object isn't changed after setViewControllers: method is called.
Could you please explain me what is going on or point me to a documentation I should read?
UPDATE: I'm not going to insert the 'vc' controller into the 'tbc' controller. What I'd like is to display the 'vc' view fullscreen, as it would normally displayed without the code about 'tbc'.
From my point of view, adding another view (tbc in my case) should NOT affect this behaviour.
Of course, that's pretty useless from practical point of view, but I'd like to know what's going on under the hood.
You should set tbc as rootViewController.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CustomViewController *vc = [[CustomViewController alloc] init];
UITabBarController *tbc = [[UITabBarController alloc] init];
NSArray *controllers = #[vc];
[tbc setViewControllers:controllers];
[[self window] setRootViewController:tbc];
[[self window] addSubview:tbc.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
When you add vc to the tab bar controller, it becomes a child of that controller. The root view controller of the window can't be a child, which is why you get that error. If you want vc to be full screen, then don't put it in the tab bar controller, and then at some point, you switch the window's root view controller to be the tab bar controller (if that's what you want).
You haven't said what you want to use vc for. A better way, depending on its use, might be to present it modally (so it takes the whole screen) from whichever controller is in the first tab of your tab bar controller. Do this from viewDidAppear, and it will be the firsts thing the user sees when the app starts up.

Resources