Adding toolbar to UIView using XIB - ios

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.

Related

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!

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.

How to remove storyboard from existing iPhone project

I am new to iPhone programming and now facing problem with storyboard. I want to remove storyboard from application and call view controller from appDelegate programmatically. How can I accomplish this?
Here is my code in appDelegate :
FirstViewController *firstView = [[FirstViewController alloc] init];
self.window.rootViewController = signInView;
return YES;
Still its showing black screen. Please help me. Thanks.
remove Main storyboard file base name. It's .plist.
The reason it's showing a black screen is because there is nothing configured in your FirstViewController class. Try setting firstView.view.backgroundColor = [UIColor greenColor]; right before the return YES' and you'll see that the FirstViewController is in fact loading; it just doesn't have any configuration besides what you've done in the init method of your FirstViewController class.
Honestly, configuring ViewControllers outside of the storyboard is not fun for beginners. I don't know why you want to do it, but your alternatives are using .nibs or adding everything manually. I encourage you not to delete your storyboard, but if you must, your code is fine. Just delete the storyboard file, or better yet, just don't use it until you decide to come back to it because it's a better idea.
Did you initialize the window and made it key?
Here is an implementation of one of my apps:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[DDHDemoViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
Maybe you have to remove the Main Interface in your project settings.
Here are the steps how I am doing.
Create a Empty project or If you have already created no worries, just remove StoryBoard entry from plist as #trick suggested.
delete MainStoryBorad file from your project
Create New UIViewController with XIB file named "MyViewController"
In your AppDelegate.h add #property for New Controller "MyViewController"
In your AppDelegate.m update didFinishLaunchingWithOptions method this way.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
MyViewController *viewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I think it is better to use storyboards than xib if your application is not that much complicated with large number of UI View Controllers.
If you want to remove storyboard from project and use nib to use with the development do the steps with this link:
http://www.wastedpotential.com/create-an-ios-app-without-storyboards-in-xcode-5/
Please find the below link and Check with things..
1. Info.plist or General Info -> Removing main Interface
2. Check with .xib connections -> Custom class is added, view connection in .xib
https://github.com/sunilhts/RemoveDefaultStoryBoard
Delete MainStoryBorad file from your project.
Delete MainStroryBoard Key from info.plist file.
Clear MainInterface option from Project setttings.
Create New UIViewController with XIB file named "MyViewController"
In your AppDelegate.h add #property for New Controller "MyViewController"
In your AppDelegate.m update didFinishLaunchingWithOptions method this way.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
MyViewController *viewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}

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.

Converting a simple app to have a tab bar

I have a question that is similar to this SO question, but slightly different (or my skills don't allow me to follow the directions with confidence). I have an existing game app that has one view controller and one nib and works fine. I want to convert it to have a tab bar controller. I want the original, existing view controller to be on the first tab, and I wrote a new view controller and a new nib for the second tab, which will be dedicated to game settings. At this stage, the app builds and runs fine with the new nib and view controllers in the project (but with no further edits -- no attempt to add the tab bar controller etc). The modified app should simply have two views each accessible from one of the two tabs.
Sorry for the long bkgnd. I'm following the accepted answer to the above-referenced question. The first 4 steps I have done or can do. The 5th step is to Delete the old version of your Main View Controller from the NIB file and also remove the IBOutlet property from the Application Delegate. I don't think I have such an IBOutlet in my app (which is different from the OP's app). Should I delete the object view controller shown in this list? Or am I on the wrong track here?
Additional Info
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set up view controller & load a clean view
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[P3ViewController alloc] initWithNibName:#"P3ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
NSLog(#"P3ViewController now active");
[self.window makeKeyAndVisible];
return YES;
}
This should get you in the right direction...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tbc = [[UITabBarController alloc] init];
YourNewViewController *ynvc = [[YourNewViewController alloc] initWithNibName:#"YourNewViewController" bundle:nil];
YourCurrentViewController *ycvc = [[YourCurrentViewController alloc] initWithNibName:#"YourCurrentViewController" bundle:nil];
[tbc setViewControllers:[NSArray arrayWithObjects:ynvc, ycvc, nil]];
self.window.rootViewController = tbc;
[self.window makeKeyAndVisible];
return YES;
}

Resources