"Application windows are expected to have a root view controller" conditional appearance - iphone

I'm writing an app for iPhone using Xcode 4.5 and iOS6. I'm also creating a new UIWindow to be able to manage the area of the status bar (to display messages there, etc.)
I'm using storyboards and my appDelegate method looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
The message does not appear in the console when I put it in the method called viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
if (!window) {
window = [[SGStatusBar alloc] initWithFrame:CGRectZero];
window.frame = [[UIApplication sharedApplication] statusBarFrame];
window.alpha = 0.5f;
[self.view.window makeKeyAndVisible]; // has to be main window of app
window.hidden = NO;
}
}
The same method, put in the viewDidLoad gives a warning in the console:
2012-12-27 11:34:20.838 NewApp[433:c07] Application windows are expected to have a root view controller at the end of application launch
Is this because I've created a new UIWindow? Why the difference between those two methods is so big?
And, most importantly, how can I get rid of this warning while putting the code in the viewDidLoad method?
EDIT:
I have encountered the same problem here, but it's not the way I'd like to solve it (it's actually the way I'm solving it right now)
I've tried setting my current ViewController as my window's root view controller by doing this:
ViewController *vcB = [[UIViewController alloc] init];
window.rootViewController = vcB;
But I keep getting a warning that says:
Incompatible pointer types initializing 'ViewController *__strong' with an expression of type 'UIViewController *'

Set window.rootViewController property .

Add the following code to your delegate.h and delegate.m files.
AppDelegate.h
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) YourViewController *viewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[YourViewcontroller alloc] initWithNibName:#"YourViewcontroller" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Hope it works.

Related

Convert View Based application to NavigationBased

I am new to iPhone development.
I created simple ViewBased Application in which, i have 2 pages Licpage and PlanPage
i have set LicPage as my RootViewController in didFinishLaunchingWithOptions,
Now when i click on button in LicPage i will navigated to PlanPage but in my PlanPage
i am unable to see my NavigationBar with back button on it.
Note: I can't drag NavigationBar manually with back button on it. because when i will add 3rd page, it will also navigate to 2nd page(PlanPage). and when i will click on back button it will take me on firstpage(LicPage) not in third page.
Try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[LicAppViewController alloc] initWithNibName:#"LicAppViewController" bundle:nil] autorelease];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
You need to make NavigationController in appDelegate class (delete viewController rootViewController).
And use this in .h
#property (nonatomic, retain) UINavigationController *navigationController;
in .m
#synthesize navigationController
and make object of LicPage (objLicPage) and set as rootviewcontroller for navigation controller
self.navigationController.rootViewController = objLicPage;
[self.window addSubView:navigationController.view];
[self.window makeKeyAndVisible];

How to assign a viewcontroller as a self viewcontroller to the appdelegate class?

I have a view controller class LoginViewcontroller where i need to add that view cotroller as a self viewcontroller to the appdelegate class. I need something like when i try to access self.viewcontroller, my LoginViewController should respond. Any help on this?
Write down this code:
AppDelegate.h File
#import "LoginViewController.h"
Create Object of LoginViewController class
LoginViewController *loginVC;
Create Property of LoginViewController
#property (nonatomic, retain) LoginViewController *loginVC;
AppDelegate.m File
#synthesize loginVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.loginVC = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
self.window.rootViewController = self.loginVC;
[self.window makeKeyAndVisible];
return YES;
}
If what you're trying to do is setup the root view controller for your application, then:
self.rootViewController = loginViewController;
If that's not what you need, please provide some more info as to what you´re trying to accomplish.

Add AdWhirl view to a tabBarController

I am trying to add an Adwhirl view to the top of my current iOS application. The application is composed of five different views, which are all under the control of a single TabBarController. can anyone write a brief tutorial that shows the code required to achieve this? I have looked through and tried a lot of the solutions out there but none of them are making it work.
Below is my current attempt at the problem, I don't get errors but I don't see anything different on the screen. Thanks in advance.
#implementation idoubs2AppDelegate
#synthesize window;
#synthesize tabBarController;
#synthesize contactsViewController;
#synthesize messagesViewController;
#synthesize adwhirlview = adview;
static UIBackgroundTaskIdentifier sBackgroundTask = UIBackgroundTaskInvalid;
static dispatch_block_t sExpirationHandler = nil;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AdWhirlView *adView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
[self.tabBarController.view addSubview:adView];
adView.center = CGPointMake(160, 342);
[self.tabBarController.view bringSubviewToFront:adView];
}
- (NSString *)adWhirlApplicationKey {
// return your SDK key
return kSampleAppKey;
}
- (UIViewController *)viewControllerForPresentingModalView {
//return UIWindow.viewController;
return [(idoubs2AppDelegate *)[[UIApplication sharedApplication] delegate] tabBarController];
}
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView {
}
You probably shouldn't be adding subviews to a normal UITabBar, if you need to customize the layout of your tab bar you might want to take a look at some of the replacements here: http://cocoacontrols.com/
I think instead of adding subviews to a UITabBar, you may think of making something similar to a GADBannerView singleton that you could just then add to each o the ViewControllers that your UITabBarController contains.
So you can set up a singleton easily enough (there's another StackOverflow question here that talks about how to do this for AdMob ads and making it work with AdWhirl should be trivial).
Then just add some ViewControllers into your UITabBarController doing something like:
UIViewController *viewController1 = [[[FirstViewController alloc]
initWithNibName:#"FirstViewController"
bundle:nil]
autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc]
initWithNibName:#"SecondViewController"
bundle:nil]
autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
viewController1,
viewController2,
nil];

Cannot find Xcode 4 new project template for navigation controller based app. Any alternative?

I am working on iOS 5. I am not able to find the navigation based application template formerly found in Xcode.
So what can I use instead?
If you want to start from scratch, start with a Single View Based project, then go to the storyboard and select the viewController, go to Editor > Embed in > Navigation Controller.
You need to use Master-Detail Application template. Choose Device Family from dropdown list as iPhone. After creating a project your appDelegate will contain UINavigationController instance.
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navigationController;
#end
and
#implementation AppDelegate
#synthesize window = _window;
#synthesize navigationController = _navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Use storyboard and drag a view controller into it. Then go Editor>Embed In>Navigation Controller.
Start with the Empty Application project. Add a UINavigationController.
This article, Creating a Navigation based iOS 5 iPhone Application using TableViews, will guide you. And this tutorial, Navigation Controllers and View Controller Hierarchies.

Bad Access - iPhone Xcode 4

I'm a beginner with Xcode and Objective-C, i want to make a view controller in code without a nib file and shape it how i want. Currently with this very simple code I can't seem to even change the background color because of EXC_BAD_ACCESS.
I read on internet it is something with memory management but I can't seem to find the fix for this. Pieces of my code:
AppDelegate.h
#import <UIKit/UIKit.h>
#import "DefaultViewController.h"
#class DefaultViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UIViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIViewController *rootViewController;
#end
AppDelegate.m
#synthesize window = _window;
#synthesize rootViewController = _rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rvc = [[DefaultViewController alloc] init];
self.rootViewController = rvc;
[rvc release];
[self.window addSubview:self.rootViewController.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_window release];
[_rootViewController release];
[super dealloc];
}
The view controller I made via, right click -> new file and UIViewController subclass without xib! In the loadView I only try this:
self.view.backgroundColor = [UIColor redColor];
The problem could be that the rootViewController doesn't have an initialized view. Hard to tell, since you don't show the code of the DefaultViewController. It could also be another error in DefaultViewController.
FWIW, you have two obsolete ivars:
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UIViewController *rootViewController;
}
You can delete these, since you synthesize _window and _rootViewController and never use the above.
There are a few issues with your code. But first the key problem you are getting EXC_BAD_ACCESS is because you are calling self.view.backgroundColor inside the loadView. If you override loadView, you must construct your view hierarchy inside the method. By not creating a view hierarchy in that method you are calling backgroundColor on a view that does not exist. Instead completely remove the loadView method or comment it out and move self.view.backgroundColor into the viewDidLoad method. (Remember even an empty loadView method will be a problem, you need to remove it or comment it out)
Second.. change your code to
self.rootViewController = rvc; to self.window.rootViewController = rvc;
BTW, once you add a view controller you don't need to add the view of the rootViewController as a subview to the window again. Assigning a view controller to the rootViewController property installs the view controller's view as the content view of the window.
Third. When you are initializing DefaultViewController you do
UIViewController *rvc = [[DefaultViewController alloc] init];
dont do that, instead do
DefaultViewController *rvc = [[DefaultViewController alloc] init];
Change the code so it's
self.window.rootViewController = rvc;
[self.window makeKeyAndVisible];
return YES;
What's inside your DefaultViewController? If you are not using a nib file, did you implement -(void)loadView ?
You should call:
UIViewController *rvc = [[DefaultViewController alloc]
initWithNibName:#"yournib" bundle:nil];
to load the NIB view. Otherwise, your rvc.view will be nil.
Sorry, I overlooked.
The answer is: you should not call self.view.backgroundColor = [UIColor redColor]; within - (void)loadView since self.view is nil firstly at this stage. He has to show that he has correctly created at least self.view = [[UIView alloc] init] in loadView.