DidFinishLaunching problem in application of iphone - iphone

Hello
I have problem with didFinishLaunching methods. I am really getting confused about what was the problem and that's why I pasted all my code. The problem was the application didn't launch, it crashed, and it show me this message in console:
**[Demo1AppDelegate setMapViewController:]: unrecognized selector sent to instance 0x5649a30
2011-05-25 14:17:58.724 Demo1[10630:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Demo1AppDelegate setMapViewController:]: unrecognized selector sent to instance 0x5649a30'**
I am using this code
In Demo1appDelegate.h file
#import <UIKit/UIKit.h>
#import "MapViewController.h"
#interface Demo1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MapViewController *mapViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
And in
Demo1AppDelegate.m file
#import "Demo1AppDelegate.h"
#interface Demo1AppDelegate ()
#property (nonatomic, retain) MapViewController *mapViewController;
#end
#implementation Demo1AppDelegate
#synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MapViewController *viewController = [[MapViewController alloc] init];
self.mapViewController = viewController;
[viewController release];
[window addSubview:self.mapViewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[mapViewController release];
[window release];
[super dealloc];
}
#end

I think
self.mapViewController = viewController;
is the problem. You do not have #synthesize for mapViewController. So you cannot access through self
Or another option is to try this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
mapViewController = [[MapViewController alloc] init];
[window addSubview:mapViewController.view];
[window makeKeyAndVisible];
return YES;
}

You need to implement UIApplication
change
#interface Demo1AppDelegate : NSObject
to
#interface Demo1AppDelegate : NSObject < UIApplicationDelegate>
This will solve your problem

the problem is in this line self.mapViewController = viewController;
you forgot #synthesize mapViewController;

Synthesize your map view controller.

You have to #synthesize mapViewController; in Demo1AppDelegate.m
You should also add [mapViewController release]; in the dealloc method of Demo1AppDelegate.m (with mapViewController being an instance variable).

Add delegate as follows ... This might be the issue :
#interface Demo1AppDelegate : NSObject <UIApplicationDelegate>

Try adding
#class MapViewController
before #implementation Demo1AppDelegate in Demo1AppDelegate.h

Hey! Where you add the subview
[window addSubview:self.mapViewController.view]; [window makeKeyAndVisible];
Try without "self"
[window addSubview:mapViewController.view]; [window makeKeyAndVisible];
Just a flying guess.

Related

screen not starting on iphone application

I am starting an iPhone application, and I can't seem to get the main screen started. Here is my delegate.h file:
#import <UIKit/UIKit.h>
#class MainViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (retain, nonatomic) MainViewController *mainViewController;
#end
and here is my implementation file(at least the beginning):
#import "AppDelegate.h"
#import "MainViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize mainViewController = _mainViewController;
- (void)dealloc
{
[_window release];
[_mainViewController];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *aViewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
[self setMainViewController:aViewController];
[aViewController release];
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
When I try to run the app, just a black screen appears, and even after waiting a long time, the screen won't show the contents of my MainViewController.xib file...
Any ideas?
Main XIB of your application is MainWindow.xib. (It can be selected in project settings).
It contains app delegate, window and mainViewController (BTW, it should be better connected from XIB, not created manually in application:didFinishLaunching) connected to app delegate.
Check the connections.

How to properly create a root view controller?

After upgrading to xCode 4.2 I am getting the following warning...
Applications are expected to have a root view controller at the end of application launch
After reading as much as I could find on line about the RootViewController I am not sure whether I have created my root view controller properly. I created it a long time ago when I was first learning to program in xCode.
One question I have is it ok to name the root view controller something other than RootViewController. Every example I see now has it named RootViewController. I also see it synthesized in the app delegate like this...
#synthesize rootViewController = _rootViewController;
I do not understand what this is doing. Why not just...
#synthesize rootViewController;
In any event I changed the name of my root view controller to RootViewController and followed the example I found at cupsofcocoa.com. But even after the changes I am still getting the "...expected to have a root controller..." warning.
If someone has the time to take a look and let me know what I am missing, I have listed the the significant portions of my initialization code below.
Thanks,
John
//RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UIViewController {
}
#end
.
//RootViewController.m
#import "RootViewController.h"
#import "JetLoggerAppDelegate.h"
#implementation RootViewController
#end
.
//JetLoggerAppDelegate.h my app delegate
#import <UIKit/UIKit.h>
#class RootViewController;
#interface JetLoggerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
#end
.
//.m app delegate
#import "JetLoggerAppDelegate.h"
#import "RootViewController.h" //I don't think I need this here
#implementation JetLoggerAppDelegate
#synthesize window;
#synthesize rootViewController = _rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions count] == 0) {
_rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = self.rootViewController;
[window makeKeyAndVisible];
return YES;
}else{
[JLHelper showAlertWithTitle:#"" message:[NSString stringWithFormat:#"launchOptions: %#", launchOptions]];
}
return NO;
}
.
//main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"JetLoggerAppDelegate");
[pool release];
return retVal;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions count] == 0) {
_rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = self.rootViewController;
**[window addSubview:_rootViewController.view];**
[window makeKeyAndVisible];
return YES;
}else{
[JLHelper showAlertWithTitle:#"" message:[NSString stringWithFormat:#"launchOptions: %#", launchOptions]];
return NO;
}
return nil;
}
Put return NO inside else statement and on the end put return nil; Hope this help.
Applications are expected to have a root view controller
Replace in AppDelegate
[window addSubview:[someController view]];
to
[self.window setRootViewController:someController];

UINavigationController not showing up when running build

EDIT: Working, see code below.
Working on an application, where right now I have the AppDelegate class, and a custom UINavigationController class. The code i have right now is pretty simple, and I feel like I've setup the XIB correctly. The build succeeds with no errors. But when the app launches, the navigationcontroller isnt displayed. I do not see the nav bar nor the table view. All I see is a blank screen. Here's the bulk of my code:
//
// FTPPhotosAppDelegate.h
// FTPPhotos
//
// Created by Aaron McLeod on 11-05-30.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "RootViewController.h"
#interface FTPPhotosAppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *navigationController;
RootViewController *rootViewController;
}
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) RootViewController *rootViewController;
#end
//
// FTPPhotosAppDelegate.m
// FTPPhotos
//
// Created by Aaron McLeod on 11-05-30.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "FTPPhotosAppDelegate.h"
#implementation FTPPhotosAppDelegate
#synthesize window=_window;
#synthesize navigationController, rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init];
rootViewController = [[RootViewController alloc] init];
[self.navigationController pushViewController:rootViewController animated:NO];
[self.window addSubview:[self.navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
#interface RootViewController : UITableViewController<UIImagePickerControllerDelegate> {
NSMutableArray *photos;
UIImagePickerController *picker;
UIBarButtonItem *addPhotoButton;
}
#property (nonatomic, retain) NSMutableArray *photos;
#property (nonatomic, retain) UIImagePickerController *picker;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *addPhotoButton;
- (void) loadPhotos;
- (IBAction) addPhoto;
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
#end
#import "RootViewController.h"
#implementation RootViewController
#synthesize photos, picker, addPhotoButton;
- (void)viewDidLoad
{
self.photos = [[NSMutableArray alloc] initWithCapacity:50];
[self loadPhotos];
[super viewDidLoad];
}
Any ideas? Here's a screenshot of the XIB,
.
Let me know if you need more info.
Instead of a root View controller, make it a UINavigationController *navigationController.
then in the applicationDidFinishLaunching, say
navigationController = [[UINavigationController alloc]init];
FirstViewController *firstView = [FirstViewController alloc]init];
[self pushViewController:firstView animated:NO];
firstView.title = #"TITLE";
[self addSubview:navigationController.view];
EDIT: some example code i have in an app of mine
AppDelegate.h
#import <UIKit/UIKit.h>
#interface DragonAge2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TitleClass *titlePage = [[TitleClass alloc]init];
navigationController = [[UINavigationController alloc]init];
titlePage.title = #"Dragon Age 2";
[navigationController pushViewController:titlePage animated:NO];
[titlePage release];
// Override point for customization after application launch.
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
You need to check some points:
Is the code in application:didFinishLaunchingWithOptions: executed when you run the application? Add a NSLog(#"app launched") in this code or set a breakpoint in it to check it. If it is not called, your FTPPhotosAppDelegate object is probably not set as the delegate of your UIApplication singleton in the XIB (this connection should be done by default if you created your app using a standard template, but it's worth checking)
Is the navigationController IBOutlet of your AppDelegate properly connected to your actual UINavigationController in the XIB? Check (using an NSLog or a breakpoint) that it is not nil.
Same for the view IBOutlet of the UINavigationController object in your XIB: check that it is properly connected and not nil.

Go to another view (UIViewController) from within UIViewController

A newbie Objective C question. Im trying to programmaticly change view from another view. But I only succeed in activating the tab button in the tabbarcontroller.
In my ViewDidLoad I have a condition that if thats not met, load the second view instead.
Is there any kind soul that can help out a Objective-C beginner? I have googled and search stackoverflow for the answer but with no luck.
FirstViewController.h
#interface FirstViewController : UIViewController {
some variables
}
some #properties
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
- (void)viewDidLoad
{
if(condition is met) {
[self.tabBarController setSelectedIndex:1];
}
}
In my AppDelegate.h
#import <UIKit/UIKit.h>
#interface otpAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation otpAppDelegate
#synthesize window;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];
return YES;
}
Edit!
I found a solution to my problem.
int selectedindex = 1;
self.tabBarController.selectedIndex = selectedindex;
UIViewController *tempvc = [[self.tabBarController viewControllers] objectAtIndex:selectedindex];
[self.view removeFromSuperview];
[self.view addSubview:tempvc.view];
Did the trick.
Try setting the index for the tabBarController instance of the app delegate like yourAppDelegate.tabBarController
If you are using tabBarController as IBOutlet , there is no need to allocate it in your app delegate. Please remove these code from your appDelegate method (application: didFinishLaunchingWithOptions)..
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];

Programmatically created ViewController and awakeFromNiB?

I am creating a viewController programmatically (hopefully the right way) My problem is that I previously created the controller in IB and have code I want to call in awakeFromNib. As I currently have things viewDidLoad works fine but awakeFromNib does not. Is there anyway to get awakeFromNib to call or an alternative method that I can use in its place?
#class MyViewController;
#interface TEST_ControllerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *viewController;
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#end
.
#implementation TEST_ControllerAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[MyViewController alloc] init];
[window addSubview:[viewController view]];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
EDIT_001:
I have pretty much come to the conclusion that using viewDidLoad is going to be my best option, particularly as I want to initialise IBOutlet instance variables.
In order to be called, the awakeFromNib method must be parameter-less (no sender):
- (void)awakeFromNib {
// Do whatever is needed...
}
Take care of the casing, as no error or warning will be logged if you mistype the method.