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

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.

Related

Add new viewcontroller at launch of app IOS Objective C

I am new to IOS development . i got a open source project and loaded in xcode and it is running good.
the project is based on webview and loading url.
what i want to do is add a new screen at the launch of application which takes three inputs from textfields and on submit of button it should send the data to next screen which is a webview and currently it is being loaded first .
1) how can i add new view controller which launches first at launch of app where i can input textfields and click on button to move to second screen
2) get the data from first screen and give to the load url to load the url.
i have taken project files from here :
https://github.com/paulirish/iOS-WebView-App
viewcontroller option on right side doesnt have the option is initial view
i am using xcode 6.2
I think you are loading viewcontroller from code like we did in older versions. I hope this helps you.
In this code you can simply change MainViewController to your own view.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MainViewController* viewC =[[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:viewC];
self.window.rootViewController = navC;
[self.window makeKeyAndVisible];
You will have to create new UIViewController with your three textFields and then set this UIViewController as Initial View Controller from storyboard like this:
After doing this get data from your textfields like:
NSString *text1Data = self.yourtextField.text;//Repeat for all text fields.
After getting data make three variables in your webViewController and assign thse values to them.
Suppose WebVC is yoour WebViewController then you can pass data and go to web viw like this:
WebVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"your_WebVc_identifier"];
vc.text1DataFromFirstVC = text1Data; //repeat for all three variables
//go to web view controller
[self.navigationController pushViewController: vc animated:YES]
EDIT:For Non Storyboard use following code from your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *Vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = Vc;
[self.window makeKeyAndVisible];
return YES;
}

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;
}

After creating a project on Xcode, i deleted the viewController that comes by default and created a new one. But the project does not run

I created a project in Xcode and deleted the viewController file that comes by default in it. I added a new file(objective-c class) and added some controls in its viewDidLoad and run the project but it does not seem to work,the view it shows is blank. Is there any setting to be made?
When you say deleted the viewController file that comes by default in it., that means that you have chosen application type other than Empty Application. That means most probably you might have chosen a Single view Application, as the defaultViewcontroller file will be provided with SingleView application( means with Storyboards by defaults on Xcode 5+).
So even if you have deleted the application, you can just
add a NewViewController (subclassed from UIViewController),
name it (myViewController)
save it
Import it in appDelegate File. (#import "myViewController.h")
Create an instance of it.(myViewController *vc = [myViewController alloc]init])
Push it on navigationController
If you want to add buttons or anything else,
just create objects in myViewController
& instantiate them
add them to SubViews.
If you are using storyBoards, then
Add a new ViewController file & name it, something like myViewController
Add a new ViewControlelr object in the mainStoryboard file by drag & drop.
Select its inheriting class in the inspector on RHS, as myViewController
No need to modify anything in appDelegate file.
Make that controller in storyBoard as entryPoint. A default arrow will be preceding your controller.
hope that gives you the overview.
You have to set mainviewcontroller from didFinishLaunchingWithOptions method of appdelegate.m file.
as below
#import "Yourviewcontroller.h"
and in didFinishLaunchingWithOptions method:-
self.viewController = [[YourViewControoler alloc] initWithNibName:#"Your xib file" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
I hope this will help you.
Yes. You have modify the AppDelegate file.
Suppose you add the new controller that name is DashboardViewController.
DashboardViewController.h
#import <UIKit/UIKit.h>
#class DashboardViewController;
#property (nonatomic,strong) DashboardViewController *viewController;
***DashboardViewController.m***
#synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
self.window.rootViewController = self.viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

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.

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