set current tableview inside new uiview - ios

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.

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

How to create an iOS project with a XIB in Xcode 6?

I'm trying to create a project in Objective-C language without storyboard with Xcode 6 Beta 5. I have tried and created a empty project but it didn't work as Xcode 5.
I have read this topic How to create project without story board in Xcode 6 - Swift but it didn't help me.
What do you mean it didn't work as Xcode 5? You can create empty project without storyboard and then add your own class with XIB like in Xcode 5:
File -> New -> File -> Cocoa Touch Class -> Set "Subclass of:" as (for example) UIViewController and check "Also create XIB file".
You first make a new "Single View" project. This starts out with a Storyboard, but as Xcode 6 has removed the option for creating an Empty project, we will just work with it from here.
You then create a new file for this project, go into the "User Interface" category and select "View". I would name it the same name as your original ViewController as it will replace the storyboard we are about to delete from the project.
Once the XIB is created, you will want to select it and set the "File's Owner" to point to the "ViewController" class that you want this XIB to link with. That is done by going into the Identity Inspector of the File's Owner of the Xib, and changing the default of NSObject to the class name of your view controller.
Once done with that, you want to go to the Connections Inspector to link the view of the File's Owner to the view of the XIB. Just click the little circle across from "view" and drag it over to your view to connect it. You should then have a connection between view and View.
Now the important parts. Go into your project Target, under the "General" tab. There is a subsection called "Deployment Info". In that subsection there is a field for "Main Interface". This field should be showing the name of the storyboard. You need to delete the value shown in this field, so that the Main Interface is left blank.
Then go into the App Delegate and set the root view controller of your window like you have been for previous versions of Xcode. Once that is done you should have a running app using your XIB, and you can delete your storyboard from the project without any adverse affects.
I don't know why people are down voting this as it is a legitimate question, so here is what you need to do:
Create an empty project, create a new view controller (File/New/File) - with XIB file if you need one, import the new view controller into your AppDelegate, and set this view controller as the root view controller.
AppDelegate.m:
#import "AppDelegate.h"
// import the view controller you want to be displayed first
#import "FirstViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// create an instance of the view controller you want to be displayed first
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
// set it as the root view controller of the application's window
[self.window setRootViewController:firstViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Now, of course, if you wanted the create a tab bar or navigation controller, you would do this a bit differently, however this should be a good starting point for you.
AppDelegate.h
UINavigationController *nav;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *ll=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:ll];
[self.window setRootViewController:nav];
[nav setNavigationBarHidden:YES];
return YES;
}
It is very simple:
create empty application project
add to this project New File -> Objective-C class (with .xib file). My class is named "ViewController" :)
Now you must create UINavigationController in AppDelegate.h e.g.:
#property (strong, nonatomic) UINavigationController *navController;
than you must set your navcontroller in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
ViewController* homeViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
_navController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
self.navController.navigationBarHidden = YES;
self.window.rootViewController = self.navController;
return YES;
}
Thats all.

Set up View Controllers in App Delegate Xcode 5

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.

Adding toolbar to UIView using XIB

I am doing a simple example of tab bar based application for iPad. I have got a controller responsible to tabs management:
#interface MainTabBarController : UITabBarController
and some view controller passed to MainTabBarController as below:
#interface WorkspaceViewController : UIViewController
//
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.tabBarRootController = [[MainTabBarController alloc] initWithNibName:#"MainTabBarController" bundle:nil];
UIViewController *workspaceController = [[[WorkspaceViewController alloc] initWithNibName:#"WorkspaceViewController" bundle:nil] autorelease];
self.tabBarRootController.viewControllers = [NSArray arrayWithObjects:workspaceController, nil];
self.window.rootViewController = self.tabBarRootController;
[self.window makeKeyAndVisible];
[workspaceController release];
return YES;
}
The WorkspaceViewController has it own xib file. Using this XIB file I'm adding some controls to the Workspace view and everything is fine. But when I want to add (drag and drop in xib file) a toolbar it does not appear when app it run.
This is how my WorkspaceViewController XIB file looks like:
Toolbar is connected with controller via outlet (using xib).
Could you tell what could be a reason and how to solve it? My toolbar has to be visible only when workspace view is visible and it should iteract only with workspace (workspace will be responsible for some drawing stuff and the toolbar will have options like cut, paste, copy etc.)
You are releasing the workspaceController again after calling autorelease.

Embedding UIPageControl inside a TabBarController stack

I'm building an application which has a TabBarController at the top level. Contained within one of the tabs there is a tableview, and when a row is selected, I'd like to go to a set of 5 views, controlled via a UIScrollViewDelegate and UIPageControl.
I've built a basic app with the UIScrollViewDelegate and UIPageControl, based Derek Bredensteiner's code in this answer: How do I use UIPageControl to create multiple views?
It works fine as a stand alone application, ie: when the ViewController code is called directly from the AppDelegate via:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
But when I try to call the same code from my tableview code, I am getting a black screen. I've also tried using presentModelViewController and I get the UIPageViewControl dots but the rest is black.
LearnPageViewController *phoneContentController = [[LearnPageViewController alloc] init] ;
// [self presentModalViewController:phoneContentController animated:YES];
[self.navigationController pushViewController:phoneContentController animated:YES];
I managed to get it to work by adding a new window property #property (nonatomic, retain) IBOutlet UIWindow *window; but it doesn't sound right. There is a window property up on the AppDelegate and now another one 2 levels down.

Resources