Set storyboard as starting file instead of xib - ios

Hi i have a xib based project then i have added a storyboard "main.storyboard". But it still running the same xib as initial screen stead of storyboard.
2013-12-18 16:44:14.376 AePubReader[1882:60b] NSMainNibFile and UIMainStoryboardFile are both set. NSMainNibFile ignored.
2013-12-18 16:44:14.396 AePubReader[1882:60b] There is no app delegate set. An app delegate class must be specified to use a main storyboard file.
the following images are make sure that i have given storyboard as starting screen.
my old app delegate for xib file ,now i replace xib uiviewcontroller class from here and put my new storyboard viewcontroller class name
.h file
#import <UIKit/UIKit.h>
#class viewViewController;
#interface AePubReaderAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
viewViewController *detailViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet viewViewController *detailViewController;
#end
.m file
#import "AePubReaderAppDelegate.h"
#import "ViewController.h"
#implementation AePubReaderAppDelegate
#synthesize window, detailViewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// // Override point for customization after app launch.
//
// self.window.rootViewController = self.detailViewController;
// [self.window makeKeyAndVisible];
//
// // [detailViewController loadEpub:#"ker"];
return YES;
}

You should open the Info.plist file and delete the "Main nib file base name" key (or keys).
Also make sure that "Main storyboard file name base" key contains the name of your storyboard without extension.
Then fix code in you main.m file. Replace UIApplicationMain call with following line:
UIApplicationMain(argc, argv, nil, NSStringFromClass([AePubReaderAppDelegate class]));

You missed a bit. Try:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"main.storyboard" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];

Under your application didFinishLaunchingWithOptions: in the appdelegate
insert this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Your Soryboard name" bundle:nil];
// Instantiate the initial view controller object from the storyboard
initialViewController = [storyboard instantiateInitialViewController];

Related

Xcode 7 - Application windows are expected to have a root view controller at the end of application launch [duplicate]

Getting the above error when my app launches. The following code is from my AppDelegate .h File
#import <UIKit/UIKit.h>
#interface TableViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
The following is from my AppDelegate implementation file .m applicationdidfinishlaunchingwithoptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
add this in your app delegate:
self.window.rootViewController = navigationController;
If you started with an empty template and added a storyboard, you need to do a couple of things:
In project settings ->General, select your storyboard as the main interface
You also need to delete the following lines from YourAppDelegate.m
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
One of the ways, I overcame this was by the following steps:
1) Start Xcode and choose Empty Application
2) Now Goto the File --> New Application under UI
3) Give it a name --> myView
4) Now create a custom Class --> myView
5) Goto the .xib file now and click on the File Owner and under the Class Attribute enter --> myView as the custom Class
6) Now add a button and create an action
7) It should work
Select your target project.Then set your mainstoryboard name from the dropdownlist in summary section.
Maybe it does sound stupid, but I got this error because I linked the root view controller with one button's event. This issue was gone when I removed this connection.
Swift 2 solution that worked for me :
Insert the code below in AppDelegate -> didFinishLaunchingWithOptions
self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("YourRootViewController") as? YourRootViewControllerClass

Declaring UIViewController variable property on another ViewController with Swift

I am a newbie to Swift. I have started a new project with Swift. I am trying to add SecondViewcontroller view as a subview to the FirstViewController. What i am looking is to declare the SecondViewController property for the FirstViewController. Can anyone please suggest the Swift version of the below code
FirstViewController.h
#interface FirstViewController : UIViewController {
IBOutlet UIView *listContainerView;
}
#property (nonatomic, strong) SecondViewController *secondVC;
#end
FirstViewController.m
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.secondVC = (SecondViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"SecondViewController"];
if (![self.secondVC.view isDescendantOfView:self.view]) {
[self addChildViewController:self.secondVC];
self.secondVC.view.frame = CGRectMake(0, 0, listContainerView.frame.size.width, listContainerView.frame.size.height);
[listContainerView addSubview:self.secondVC.view];
[self.secondVC didMoveToParentViewController:self];
}
}
#end
What you should do is to make a subview, instead of making a new UIViewController. In only very special cases would you ever put one UIViewController inside another. Basically every 'screen' should have one UIViewController, but you shouldn't put one inside another.

How to transition between any two ViewControllers in Cocoa Touch?

I'm fairly new iOS development. I have multiple viewControllers for my app, for example -
A login screen
A main / my profile screen
A leader board / standings screen
etc..
Each of these screens has its own viewController.
What's the best way to transition between any two arbitrary viewControllers with an animation?
My current approach is -
Keep a reference to each viewController in AppDelegate.m
Keep changing the window's root controller as needed
This is both cumbersome and seems pretty inefficient, plus I'm not quite sure how to incorprate animated transitions here.
I see some examples with UINavigationController, but it seems like that operates as a "stack" of views that you can go into and then back out of. I'm not looking to keep a history here, just switch from any view to another.
Any good ways to accomplish this?
Thanks!
Try this Code.
AppDelegate.h
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic) UINavigationController *navigationController;
#property (strong,nonatomic) LoginViewController *loginVC;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "LoginViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.loginVC = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
self.loginVC.title = #"Login Page";
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.loginVC];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
}
LoginViewController.h
#import <UIKit/UIKit.h>
#import "MyProfileViewController.h"
#interface LoginViewController : UIViewController
#property (strong,nonatomic)MyProfileViewController *myProfileVC;
#end
Login button action in LoginViewController.m file
- (IBAction)pushMyProfileView:(id)sender
{
self.myProfileVC = [[MyProfileViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:self.myProfileVC animated:YES];
}
Basically you can achieve that by using:
Apple containers (UINavigationController, UITabbarController, UISplitController)
Containment API
Using "normal" view controller and modal presentation
Containment API is probably what you are looking for, you create a UIViewController that is responsible to manage the presentation of its child view controllers. Here is Apple Documentation, there are plenty of examples inside.
As a beginner I believe the easiest solution for you it to use a Storyboard and create a segue between your View Controller.
Inside your ViewController you should override prepareForSegue: method to pass data to the segue.destinationViewController

Xib doesn't load as expected

It has been a while since I created a project from scratch. Back then in XCode 4 / 5 the developer was given the choice between storyboard or Xib to get the project started. In Xcode 6 though every template is forced to get started as StoryBoard.
I have removed the story board. Then I have opened info.plist in Supporting files and set Main storyboard file base name : Main to Main storyboard file base name: <blank>.
Then I created a basic testViewController with Xib (same name).
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController.viewControllers = [NSArray arrayWithObject:firstViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And this still shows me a black view controller. I checked the xib file, the File owner is set to the view controller and the view delegate is also set.
What am I missing please?
change these two things:
#property (nonatomic, retain) IBOutlet UIWindow *window;
testViewController *firstViewController = [[testViewController alloc] initWithNibName:nil bundle:nil];
To this, and add this code, with the real name or you xib file:
#property (nonatomic, retain) UIWindow *window; // Are you using ARC?
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// I think your xib files is testViewcontroller.xib, if not change it.
testViewController *firstViewController = [[testViewController alloc] initWithNibName:testViewController.xib bundle:nil];
// change this also
self.navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;

Call NavigationController (xib) from ViewController (storyboard)

I'm normally just use storyboard for my iOS, but needed to integrate an SDK which does not use storyboard, rather xib. My initial view controller is from storyboard, and when a button is pushed (IBAction), I would like it to go to the xib view controller, but not sure how programmatically to do so. Here is my AppDelegate:
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:...{
// set to storyboard on launch
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"iPhoneStoryboard"
bundle: nil];
UIViewController* viewController = [mainStoryboard instantiateInitialViewController];
[self.window setRootViewController:viewController];
[window makeKeyAndVisible];
return YES;
}
Here is my code .h:
//mainVC.h
#import <UIKit/UIKit.h>
#interface MainVC : UIViewController{
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIButton *buttonScan;
-(IBAction) ActionScan;
#end
And the .m:
//mainVC.m
#interface MainVC ()
#end
#implementation MainVC
#synthesize window;
#synthesize navigationController;
#synthesize buttonScan;
- (IBAction)ActionScan{
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}
create instance of your xib viewController with initWithNibName
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
& then push this controller instance on navigation controller
[self.navigationController pushViewController:desController animated:YES]
In the view controller in storyboard, code below:
alloc & init destination view controller:
UIViewController *desController = [[SomeViewController alloc]init];
show the destination view controller:
[self.navigationController pushViewController:desController animated:YES]
or
[self.navigationController presentViewController:desController animated:YES completion:nil
Usually you don't set the window's rootViewController directly. Instead you should use
- (IBAction)ActionScan{
[self presentViewController:self.navigationController animated:YES completion:nil];
}
to show your next view controller. The first and accepted answer to this question gives a more detailed explanation of how this works.
Changing root view controller of a iOS Window
Setting window.rootViewController and calling [window makeKeyAndVisible] usually happens in your app delegate, but since you're using storyboards, it is done for you under the hood.
Also, unless you're using multiple windows, you can access your UIWindow from anywhere using your app delegate singleton object.
MyAppDelegateClass *appDelegate = (MyAppDelegateClass*)[[UIApplication sharedApplication] delegate];
UIWindow *mainWindow = appDelegate.window;

Resources