Set up View Controllers in App Delegate Xcode 5 - ios

I wanted to know how to set up the app delegate in Xcode 5 since it's different than it was in previous versions. I want the generic view controller files (ViewController.h and .m) to be the files that control the rootViewController I set in my app delegate. Does this happen automatically or do you need to do something in the code? This is how I set up my appDelegate.m
*(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect viewRect = [[UIScreen mainScreen]bounds];
self.window = [[UIWindow alloc]initWithFrame:viewRect];
UIViewController *viewController = [[UIViewController alloc]init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?

To do it programmatically you can set it to be a UITableViewController since it is a subclass of UIViewController. If you want to use the already create ViewController just change the subclass in the .h file from UIViewController to UITableViewController and add the tableview delegates and datasources into the .m.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewControllerWithIdentifier:#"myViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
or if you want to just draw the view in the view controller create a class and do this
MyViewController *viewController = [[MyViewController alloc]init];
self.window.rootViewController = viewController;
If you are wanting to set your root view via storyboards you can just check the __Is Initial view controller` option

If I wanted my rootView Controller to be a table view controller or something else, would I need to embed it in a basic VC first?
No.
You can set any UIViewController class to be the rootViewController. I am assuming you are building your viewController hierarchy in code, and you are not using InterfaceBuidler. Although also with Interface builder, you can use any viewContorller as the rootviewcontroller, either using storyboards or plain xibs.

Related

Storyboard view resizing trouble

Full disclaimer- I'm pretty new to iOS. I created a tableview with custom cells using the storyboard with a navigation controller as the initial entry point, and my tableview as the navigation controller's root view. When I run the app in the simulator, it seems as though everything is oversized/zoomed in, though my storyboard looks like this:
I've tried with iPad and iPhone and in both devices my story board doesn't pop up properly. I instantiate my root view controller thus:
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//get a pointer to my main storyboard
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
//instantiate my nav controller + item controller through the storyboard
UINavigationController *nav = [mainStoryBoard instantiateViewControllerWithIdentifier:#"navStoryBoard"];
ItemsViewController *ivs = [mainStoryBoard instantiateViewControllerWithIdentifier:#"tableStoryBoard"];
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
self.window.backgroundColor = [UIColor clearColor];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
Why won't my tableView appear in the correct size when I run my app? Am I instantiating my views incorrectly?
From the screenshot it looks like you are using Size classes (which is usually enabled by default). And hence the zoomed or scaled up behaviour.
If you are developing for a particular form factor, you may disable the "Use Size Class" property of the View Controller. More details here: https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/EnablingAdaptiveSizeDesign.html
However, if you want your UI to be scalable on different form factors - you should keep this checked and use Autolayout constrains for your Table View. More details here: https://developer.apple.com/library/ios/recipes/xcode_help-IB_auto_layout/chapters/UnderstandingAutolayout.html

obj c, xcode making a view transition

I am trying to make a button on a navigation controller that causes a transition to another view controller (that has a back button to the previous nav view).
I'm trying to do this programmatically.
I made a UINavigationController and a UITabBarController. The nav controller is the opening tab of the tab controller.
I made a subclass of the UIViewController called SubVieController. I haven't added anything to the Sub classes, only the auto generated material.
I've made all my edits to the didFInishLaunchingWithOptions method in Appdelegate.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];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SubViewController * firstTab= [[SubViewController alloc] initWithNibName:#"SubViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab];
SecondViewController *secondTab = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
SubTableController *table = [[SubTableController alloc]initWithNibName:#"SubTableController" bundle:nil];
navigationController1.tabBarItem.title = #"First Nav";
secondTab.tabBarItem.title =#"Second";
table.title = #"Third Table";
tabBarController.viewControllers = #[navigationController1,secondTab, table];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
And I try to move a Button onto the SubViewController.xib file to connect it into the code so I can make a button action. But when I ctrl+drag, nothing inserts into the code like usual.
What do I do to have the button cause a transition from the starting view to a separate view that is still considered to be on the 1st tab, with a back button to the first view which is also in the first tab?
EDIT:
This code seems to work for the button action:
- (IBAction)transition:(UIButton *)sender{
SubViewController * view2 = [[SubViewController alloc]init];
[self.navigationController pushViewController:view2 animated:YES];
}
As you have correctly done, the UITabBarController should not push a UIViewController but a UINavigationController. That UINavigationController should have a rootViewController.
Not being able to Control-Drag in Interface Builder generally means that the Custom Class in the Identity Inspector is not set properly.
Connecting IBAction
In Xcode project, Select SubViewController.xib
Select your button
Xcode > View > Utilities > Show Connections Inspector
Xcode > View > Assistant Editor > Show Assistant Editor
Ensure that Automatic > SubViewController.m > No Selection is present. If not, you may not have the Custom Class set properly in IB
In Connections, click Touch Up Inside grommet and drag onto Assistant, right below #implementation SubViewController
Enter name, such as buttonTappedAction
Add code to push the UIViewController as needed:
Obj-C
UIViewController * vc = [[UIViewController alloc] initWithNibName:#"id" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
Swift
let vc = UIViewController(nibName: "id", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
Storyboard
Architecture suggestion:
Start over using Storyboard
There is no need to write a single line of code to implement the above solution using a Storyboard!

UISplitViewController: Does not show up when setting it up pragmatically

I am trying to setup a splitviewcontroller using storyboards. The code below is what I have so far. However, it is showing a black screen. I have a storyboard name Main. I have two viewcontrollers in the storyboard. I read how to do this from an article, but can't get it to work. I must be missing something small. Any help is appreciated.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
ViewController *firstVC = [sb instantiateViewControllerWithIdentifier:#"ViewController"];
ViewController1 *secondVC = [sb instantiateViewControllerWithIdentifier:#"ViewController1"];
CGRect frameFirstVC = firstVC.view.frame;
frameFirstVC.size.width = 100;
CGRect frameSecondVC = secondVC.view.frame;
frameSecondVC.size.width = 100;
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
splitVC.viewControllers = [NSArray arrayWithObjects:firstVC, secondVC, nil];
[self.window addSubview:splitVC.view];
[self.window makeKeyAndVisible];
return YES; }
You shouldn't add the split view controller's view as a subview of your window directly. Instead, set the window's rootViewController property:
self.window.rootViewController = splitVC;
In addition to configuring the view hierarchy, this sets up additional state and layout information for the app to properly display and use the split view controller.
(I should point out that instead of writing any of this code, you could put the split view controller in your storyboard, mark it the initial view controller, then use that storyboard as your app's main interface file. That's a bit of a bigger change, though.)

set current tableview inside new uiview

I've been making an App completely programmatically, but I would like to add some more buttons etc.
Currently my main view is a full-screen TableView.
I would like to load a UIView (from a NIB) which has some buttons / labels and my curreny TableView in the middle (full width) with sort of a header and footer with my buttons / labels.
(Since I suspect that changed need to be made here...) My AppDelegate currently has the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
AFViewController *viewController = [[AFViewController alloc] initWithStyle:UITableViewStylePlain];
//AFViewController *viewController = [[AFViewController alloc] initWithNibName:#"mainView" bundle:nil];
self.viewController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You need to create a UIViewController instead of UITableViewController. You can easily do in your current AFViewController which actually a UITableViewController, just follow these steps
Open AFViewController.h change UITableViewController to
UIViewController.
Then open AFViewController.xib and add a
UIView from controls.
Drag UITableView inside UIView.
Right click File Owner connect view to the UIView and create IBOutlet for the UITableView inside the UIView.
Thats it you are ready to go.. And at last..
AFViewController *viewController = [[AFViewController alloc] initWithNibName:#"AFViewController" bundle:nil];
Why don't you create the desired structure in one nib itself? Create a nib with view controller. On the nib place your buttons and tableview as per your layout.
Interface builder is intended to reduce your coding effort when it comes to design so use it. Make sure to make your new view controller as the root controller in app delegate.

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.

Resources