starting ios project without storyboard - ios

Im having some troubles to start an iOS app using xibs instead of storyboard. The problem is that im getting a black screen and the first view controller is not being called (added break point to viewDidLoad method).
In the app delegate header i have declared this:
#property (strong, nonatomic) UIWindow window;
#property (strong, nonatomic) ViewController *viewController;
And in the didFinishLaunchingWithOptions method i have this implementation:
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
Looking over some forums i found that i should be allocing the window so i added this as the first line of the function
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
The problem is that, when i do this, the app crashes after returning from didFinishLaunchingWithOptions method (SIGABRT without any trace).
I also tried to make the navController a property and also instantiating a default UIViewController class initing the same xib
What am i doing wrong?
Thanks and regards

Hope this helps you:
Delete the view controller and storyboard file and new viewController.h,viewController.h.m ,viewController.xib file.
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
#synthesize viewCOntrollerobj;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewCOntrollerobj = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewCOntrollerobj];
//navController.navigationBarHidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

To change your project to use xibs instead of storyboards start by creating a xib for each view controller. You will need to change the File's Owner to class to the view controller you are creating the xib for. Then link the File's Owner view outlet to the view in the xib.
After that, select your app target and change the Main Interface drop down to be empty. Now you can delete the storyboard file.
Finally, initialize your window in the app delegate's application:didFinishLaunchingWithOptions: method and set your initial view controller as the root view controller of the window. Then call makeKeyAndVisible on your app delegate's window and you should be good to go.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [UIWindow new];
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
return YES;
}

In xCode 11:
Step1:
-> Delete the storyBoard file
Step2:
Under yourProject > General > deployment info > main interface -> delete reference to storyboard
Step 3:
In info.plist > Application Scene Manifest > Scene Configuration > Application Session Role > item 0 > storyboard name
-> Delete the line
Step 4:
In SceneDelegate.m
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.windowScene = (UIWindowScene *)scene;
self.window.rootViewController = [[UINavigationController alloc]
initWithRootViewController:ViewController.new];
[self.window makeKeyAndVisible];
}
make sure to import your viewController headfile like so: #import "ViewController.h"

OK, at last i got it.
What i had to do is just add again
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
After this, just delete the .h, .m and .xib and create them again.
For any reason its working fine now.

This is a kind of "out of the box" solution, so it is irrelevant to your code. Specially since I don't see anything wrong with your code.
I had the same problem and tried for a long time to fix the code, what worked for me was finding an example project (in github for example) that uses xibs.
Download it and then edit it to make your application it is guaranteed to work. It is a shame that Xcode wants to force us to use their storyboards with these kinds of problems.

I didnt try with XIB, but this thing is working fine here:::
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.viewController = [[ViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
and in ViewController's viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor greenColor]];
// Do any additional setup after loading the view, typically from a nib.
}
And the green color comes on the screen.
Initialize the window before setting the root view controller for the window.
That's the only problem that i see in you code, as the rootViewController is set to a nil window, and after that you are initializing.

Leave main interface empty
Add new ViewController.xib to the project and mark its file's owner class as "ViewController"
In AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSArray* xibContents = [[NSBundle mainBundle] loadNibNamed:#"ViewController"
owner:nil
options:nil];
ViewController* vc = [xibContents objectAtIndex:0];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
build and run

In Swift 3.0
//MARK: Initial View Controller
func initialViewController(){
self.window = UIWindow(frame: UIScreen.main.bounds)
let rootViewController = UIViewController(nibName: "HomeVC", bundle: nil)
let navigation : UINavigationController = UINavigationController(rootViewController: rootViewController)
navigation.isNavigationBarHidden = true
self.window?.rootViewController = navigation
self.window?.makeKeyAndVisible()
}
in appdelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
initialViewController()
return true
}

Xcode 11 with SceneDelegate
In SceneDelegate.m
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.windowScene = (UIWindowScene *)scene;
self.viewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
}
No need to do anything in AppDelegate.m

Related

Setting UINavigationController (from storyboard) as rootViewController in AppDelegate.m

I'm using parse and I'm trying to get the login screen to show if the user isn't the "current" user. I'm having issues with my NavigationController (from my storyboard) and using it as the rootViewController even though it's already set as initial View Controller in my storyboard. Using this line of code I select the NavigationController (from my storyboard) and initialize it in my app delegate.
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
I then decide whether or not to display the loginVC then finally I set the NavigationController as the rootViewController here:
self.window.rootViewController = navVC; [self.window makeKeyAndVisible];
Except someone I don't. I get this error when I try to build my app. "Application windows are expected to have a root view controller at the end of application launch" Anyone have any ideas what's going wrong?
Here's the code all together.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
// Initialize Parse.
[User registerSubclass];
[Question registerSubclass];
[Parse setApplicationId:#"HI"
clientKey:#"HI"];
// Determine whether or not to show the login screen
if (![PFUser currentUser]) {
LogInViewController *loginVC = [[LogInViewController alloc] init];
[navVC setViewControllers:#[loginVC] animated:YES];
} else {
QuestionsTableViewController *questionsVC = [[QuestionsTableViewController alloc] init];
[navVC setViewControllers:#[questionsVC] animated:YES];
}
self.window.rootViewController = navVC; [self.window makeKeyAndVisible];
return YES;
}
If you're starting with a storyboard you need to have it set as your Main Interface in the Deployment Info in your app's General settings. You should also remove the line:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
..as it's initialising a new window in the place of the one created from the previous step.
I used these two lines of code to fix the issue. I got answer from here:
Programmatically set the initial view controller using Storyboards
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navVC = [storyboard instantiateViewControllerWithIdentifier:#"QuestionsView"];
I removed this line:
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
There don't get the error anymore.

NavigationController presenting ViewController twice

I installed third party library using pods, which has it's own navigationController. I my existing project I am setting the rootViewController in the app delegate. I am pushViewController to the library ViewController (which is in it's own navigation stack). When I exit and then push to the library for a second time the ViewAlready seems to be there before transitioning the same view over the top.
In my appDelegate...
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
appbarViewController = [[AppBarViewController alloc] init];
[self loadNormalFlow];
[self.window makeKeyAndVisible];
}
- (void)loadNormalFlow
{
self.navigation = [[UINavigationController alloc]initWithRootViewController:appbarViewController];
self.window.rootViewController = self.navigation;
[self.navigation setNavigationBarHidden:YES];
}
-(void)displayLibraryView
{
ACSViewController *acs = [[ACSViewController alloc] init];
[self.navigation pushViewController:acs animated:nil];
}
Within library ViewController class to return back to original rootViewController i am using
[[UIApplication sharedApplication].delegate performSelector:#selector(loadNormalFlow)];
I seems like it is not properly dismissing the library ViewController, or it does a push twice? Any idea?

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

Xcode without Storyboard and ARC

i have downloaded new xcode-5 and just started using it.
We can create application directly including storyboard and ARC , it is not asking for option like earlier versions.
So, my question is how can we use xcode5 without ARC and storyboard. we have to manually remove storyboard file ? or is there any other option.
Create a project with an Empty application and Add any viewcontroller (i added TestViewController here)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
TestViewController *test = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
STEPS FOR REMOVE ARC
1) In build setting set Automatic Reference Counting to NO.
///////////////////////////////////////////////////////////////////////////END///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
If you have Already Created Application with storyboard and ARC then
STEPS FOR REMOVE STORY BOARD
1) Remove Main.storyboard file from your project.
2) Add new files with xib for your controller , if it is not added in compiled sources in build phases then add there manually.
3) Remove Main storyboard file base name from plist.
4) Change appdelegate didFinishLaunchingWithOptions file and add :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
[self.window makeKeyAndVisible];
just like :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
// Override point for customization after application launch.
TestViewController *test = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Now,in above example you have to manage memory management manually like ,
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[test release];
STEPS FOR REMOVE ARC
1) In build setting set Automatic Reference Counting to NO.
Instead of delete the storyboard file, you can Create a new project with Empty Application template. So that you can avoid the storyboard file creation.
Use following steps to omit storyboard:
Create a new project with Empty Application template.
Add a new viewController (Example: LoginViewController)
Change the didFinishLaunchingWithOptions in AppDelegate.m file as specified below.
Change To:
#import "LoginViewController.h"
- (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];
LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Remove ARC:
Go to Build Setting -> Objective-C Automatic Reference Counting -> NO
create new project
![Create new Project]
//remove Main storyboard file base name in Info
Add This Code In appdelegate
- (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];
LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Then automatic remove your storyboard.
Please Try this...
successfully Executed. thanks
ShortCut: I Prefer
Create the project without Storyboard and ARC in xcode4 and then open that project in xcode5 .
Xcode 4 had the "Use Storyboards" checkbox when you created a new project. It is possible to grab the old Xcode 4 application templates (XML files) and convert them to Xcode 5. That way, you get the old templates back that let you choose whether you want storyboards or not.
I wrote a script that does all that work for you: https://github.com/jfahrenkrug/Xcode4templates
After running the script, you will have an "Xcode 4" section in the "New Project" screen:
And then - Alas! - you get your beloved choices back:
You will need a copy of the Xcode 4 .app bundle from http://developer.apple.com/ios to use this script.
I have a Tip:
The First: I create my project by XCode 4.6 (Because this version is nearest to XCode 5).
Of course that with XCode 4.6, you can chose use or not using ARC, Storyboard.
The Second: After that I will open my Project with XCode 5.
=> I think that Xcode 5 will understand that project is use nonARC, and of course, do not have Storyboard.
I hope your project will work! :D

iOS Application launch black screen, UINavigationController, Nib, RootViewController

I've got the following app, whose RootViewController is named TopicsViewController.
When I run it, there aren't any errors or breaks but the screen is black. No table, populated or empty, just a black screen. Not sure which of the following is happening:
Is there something wrong with my application didFinishLaunchingWithOptions method in relation to a parser initlizing in it?
Is it something to do with my nib file for the TopicsViewController?
I can show more code from my TopicsViewController class if needed.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
///////////////////////////////////////////
***initializing code for parser which populates TopicsViewController (not shown to save space)*****
///////////////////////////////////////////
UIViewController *rootController =
[[TopicsViewController alloc]
initWithNibName:#"TopicsViewController" bundle:nil];
navController = [[UINavigationController alloc]
initWithRootViewController:rootController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Instead of:
[self.window addSubview:navController.view];
Write:
self.window.rootViewController = self.navController;

Resources