I have SingleView application. Now I want to add a mainwindow to my project.
How can I add the window into my project?
Thanks in advance.
First Add Your LoginViewController as self.window.rootViewController such like
Add this code in your Appdelegate.h file
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface AppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
#property (nonatomic, retain) UIWindow *window;
#end
Add this code in your Appdelegate.m file
(Here i also added UINavigationController too)
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
LoginViewController *loginViewController = [[LoginViewController alloc] init];
UINavigationController *loginNVController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
loginNVController.navigationBarHidden = YES;
self.window.rootViewController = loginNVController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
OR Add Directly
Check this link for adding window.xib .
Check this link
If you have choosen single view application. You can give the xib name as per the view. That is more general in sense. If you have selected empty application then go to you xcode project and add new file. which extends from UIViewController.
AppDelegate can not be referenced in any .xib files. the main method will instantiate AppDelegate object and AppDelegate object will then instance other view controllers.
For more you can read some apple documentation.
Thanks
Rinku
Related
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
This is the code, i'm trying to make the 'self.viewController' work, but it is giving me an error. What do i need to do to fix this. I am getting the error as stated above in the header of this thread.
#import "AppDelegate.h"
#import "ViewController.h"
#import "Name.h"
#interface AppDelegate ()
-(Name *)createNameWithNonsenseDataWithIndex:(int)index;
#end
#implementation AppDelegate
#synthesize tableData;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//Create dummy data
NSUInteger numberOfNames = 25;
self.tableData = [[NSMutableArray alloc] initWithCapacity:numberOfNames];
//Create a temporary array of tableData
for (NSUInteger i = 0; i < numberOfNames; i++) {
//Create a new name with nonsense data
Name *tempName = [self createNameWithNonsenseDataWithIndex:1];
//Add it to the temporary array
[self.tableData addObject:tempName];
}
self.viewController = [[ViewController alloc]
initWithNibName:#"viewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You need to declare a property to use self.viewController
#interface AppDelegate ()
-(Name *)createNameWithNonsenseDataWithIndex:(int)index;
#property (nonatomic, strong) ViewController *viewController;
#end
Also as Haroldo Gondim mentioned make sure the name of your nib is correct.
Since Xcode 4.4 you can skip #synthesize
Declaration/definition of variables locations in ObjectiveC?
There is no property 'viewController' in your 'AppDelegate' class.
In your AppDelegate.h under interface declaration you need to have:
#property (nonatomic, strong) ViewController* viewController;
And In your AppDelegate.m under implementation declaration you need to have:
#synthesize viewController;
In Nib name the name must be exactly the name of your .xib File.
Use V with upper case.
self.viewController = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
thank you for taking time to answer me !
I'm inexperienced in IOS development, after several searches on google, I turn to you.
My problem is that : I will wish to use UINavigationController, with new main view, to allow me to create a back button.
Here is my AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MainViewController *mvc = [[MainViewController alloc]init];
//self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//UINavigationController * navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = mvc;
[self.window makeKeyAndVisible];
return YES;
}
and my AppDelegate.h :
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
//#property (strong, nonatomic) ViewController *viewController;
#end
During development I changed the main view of the application as you can see above the two lines commented out.
I would like to use "UINavigationController" with this new main view, what does it change? and where?
Thank you and sorry if i'm not clear ! (Google Translate)
Set your MainViewController *mvc as the root of your navController and make it the root view controller of the window.
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController: mvc];
self.window.rootViewController = navController;
I'm very new to ios developing and many of the tutorials i watch follow the xib approach since they were recorded before XCode 5. How can i go with the xib approach in Single View Application? When i delete storyboard and select one of the xib's as the main interface it's not working. Thanks for any tip.
add new file in that select the cococatouch and select the Objective -C class and then go to the next click.
you show the below screen
at the bottom must select the with xib for user Interface.and next
AppDelegate.h:
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ViewController *viewObj;
UINavigationController *navObj;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewObj;
#property (strong, nonatomic) UINavigationController *navObj;
AppDelegate.m:
#synthesize viewObj,window,navObj;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
viewObj = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
navObj = [[UINavigationController alloc] initWithRootViewController:viewObj];
window.rootViewController=navObj;
[window makeKeyAndVisible];
return YES;
}
Select Empty application template while creating new project.
Select Cocoa touch from left pane -> objective-c class -> then give name of viewController and tick option of Xib user interface.
and then in apply below code in appDelegate file:
appDelegate.h File:
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#interface HomeAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic)HomeViewController *homeViewController;
#property (strong,nonatomic)UINavigationController *navigationController;
appDelegate.m File:
- (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];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
#end
you can do it by following these steps:-
Create a new project
Select Single View Application
Set ProjectName and other settings
Save Project at location
Select Project in Navigator Panel in Left
Remove Main.storyboard file from project
Add New .xib file in Project
Set Main Interface to your .xib file in "General Tab" on right panel.
Paste following code in didFinishLaunchingWithOptions method
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Courtesy:- MKR
https://stackoverflow.com/a/19633059/1865424
Just check it have you missed any of the step.
I have a file named MMAppDelegate.m:
#import "MMAppDelegate.h"
#implementation MMAppDelegate
#synthesize window;
#synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewController = [[MainViewController alloc] init];
[window addSubview:viewController.view];
// Override point for customization after application launch.
[window makeKeyAndVisible];
return YES;
}
//and more standard methods
MMAppDelegate.h:
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#interface MMAppDelegate : UIResponder <UIApplicationDelegate> {
UIWindow *window;
MainViewController *viewController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) MainViewController *viewController;
#end
MainViewController.m:
#import "MainViewController.h"
#implementation MainViewController
- (void)viewDidLoad {
NSLog(#"view did load main view controller");
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
}
#end
And yet when I run the program, I get a black screen, and the output:
2012-02-12 15:44:48.160 MoreMost[44076:f803] view did load main view controller
2012-02-12 15:44:48.163 MoreMost[44076:f803] Applications are expected to have a root view controller at the end of application launch
What is the problem?
Change this line:
[window addSubview:viewController.view];
to this:
window.rootViewController = viewController;
And you'll need to create the window, like this:
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
You don't need/want to add the view controller's view as a subview. You want to make your controller the root view controller and iOS will take care of the rest:
window.rootViewController = viewController;
That is, as of iOS 4. (Not sure the answer if you're going back earlier than that.)
You also need to create the window before you use it. Something like
window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
Not sure if you aren't or if you just didn't show it ...