Multiple View App shows Blank Screen in Simulator - iphone

I am developing a simple app that first shows a menu screen, and when a button is pressed, a game screen appears. I was able to develop the game screen without any issues, but when I changed the code to first display the menu, the simulator showed a blank screen.
I've read all the articles on connecting views with IB but I can't figure this out.
Any help would be appreciated.
This is my code:
// Pong_Multiple_ViewAppDelegate.h
// Pong Multiple View
//
// Created by Brett on 10-05-19.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#class MenuViewController;
#interface Pong_Multiple_ViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MenuViewController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet MenuViewController *navigationController;
#end
//
// Pong_Multiple_ViewAppDelegate.m
// Pong Multiple View
//
// Created by Brett on 10-05-19.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "Pong_Multiple_ViewAppDelegate.h"
#import "MenuViewController.h"
#implementation Pong_Multiple_ViewAppDelegate
#synthesize window;
#synthesize navigationController;
- (void)application:(UIApplication *)application{
// Override point for customization after application launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
#end
//
// MenuViewController.h
// Pong Multiple View
//
// Created by Brett on 10-05-19.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "GameViewController.h"
#interface MenuViewController : UIViewController {
GameViewController *gameViewController;
IBOutlet UIButton *gameButton;
}
#property(nonatomic, retain) GameViewController *gameViewController;
#property(nonatomic, retain) UIButton *gameButton;
-(IBAction)switchPage:(id)sender;
#end
//
// MenuViewController.m
// Pong Multiple View
//
// Created by Brett on 10-05-19.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MenuViewController.h"
#import "GameViewController.h"
#implementation MenuViewController
#synthesize gameViewController;
#synthesize gameButton;
-(IBAction)switchPage:(id)sender{
if (self.gameViewController==nil) {
GameViewController *gameView = [[GameViewController alloc]initWithNibName:#"GameView" bundle:[NSBundle mainBundle]];
self.gameViewController= gameView;
[gameView release];
}
[self.navigationController pushViewController:self.gameViewController animated:YES];
}
....
#end
My code also includes classes: GameViewController.h, GameViewController.m, and nib files: MenuView.xib, and GameView.xib
Thanks,
B

- (void)application:(UIApplication *)application{
The method name must be -applicationDidFinishLaunching: (not -application:), otherwise UIKit can't find it and will ignore the initialization code.

In your Pong_Multiple_ViewAppDelegate.m, you try to add a view using
[window addSubview:[navigationController view]];
Are you sure that navigationController is instantiated anywhere? Either in code or using a nib?

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.

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.

Loading a new view before the old one iphone sdk

Probably just a simple question, but ive been building a little puzzle game and no decided i could do with a menu for it. Ive created the menu no problem i just cant get it to show before the puzzle. Ive tried changing the code in the appdelegate but its not liking it.
slider appdelegate.h
#import <UIKit/UIKit.h>
#class SliderViewController;
#class MainMenu;
#interface SliderAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainMenu *viewController1;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet SliderViewController *viewController;
#property (nonatomic, retain) IBOutlet MainMenu *viewController1;
#end
Slider appdelegate.m
#import "SliderAppDelegate.h"
#import "SliderViewController.h"
#implementation SliderAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize viewController1;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController1.view];
[self.window makeKeyAndVisible];
return YES;
}
But doing that throws out an error:
error: request for member 'view' in
something not a structure or union
I know its probably a stupid question but could anyone help please.
You need to import that class and make alloc the object first see this
Slider appdelegate.m
#import "SliderAppDelegate.h"
#import "SliderViewController.h"
#import "MainMenu.h"//change here
#implementation SliderAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize viewController1;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[[MainMenu alloc] initWithNibName#"MainMenu" bundle:nil] autorelease]; // and here
[self.window addSubview:viewController1.view];
[self.window makeKeyAndVisible];
return YES;
}

EXC_BAD_ACCESS in tableview application

This is my first iPhone application and it's based on a top-level tableview. Selections of rows either go to another tableview or to a view. The application runs OK on the simulator but when ported to my iPhone it fails with a EXC_BAD_ACCESS error. This happens while my splash screen is being displayed. NSLog indicates that the program processes in appDelegate.m:
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
but then it just fails. The code never seems to reach the viewDidLoad in my RootViewController.
I'm sure that I've got the RootViewController and appDelegates mixed up somehow but cannot figure out exactly what's wrong. I've attached the beginning code of my RootViewController, appDelegate - any help appreciated.
RootViewController.h code....
#interface RootViewController : UITableViewController {
TyresViewController *tyresController;
EngineSpecViewController *engineSpecController;
CarbonTaxBandViewController *carbonTaxBandController;
TyreSpecificationsViewController *tyreSpecificationsController;
FuelConsumptionandEmissionsViewController *fuelConsumptionandEmissionsController;
CompanyCarTaxBandViewController *companyCarTaxBandController;
CarbonCalculatorViewController *carbonCalculatorController;
ReminderViewController *reminderController;
//NSString *selectedSpecification;
NSArray *listOfItems;
}
RootViewController.m code ......
#import "RootViewController.h"
#implementation RootViewController
#synthesize listOfItems;
//#synthesize selectedSpecification;
#synthesize carbonTaxBandController;
#synthesize engineSpecController;
#synthesize tyreSpecificationsController;
#synthesize tyresController;
#synthesize fuelConsumptionandEmissionsController;
#synthesize companyCarTaxBandController;
#synthesize carbonCalculatorController;
#synthesize reminderController;
appDelegate.h code.....
#interface MyCar3AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#end
appDelegate.m code .....
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
Just a thought, in your main Info.plist file there should be an entry for Main nib file base name. This refers to the nib that will be loaded when your app starts. The simulator is not case sensitive, but the device is. Check the case of the value of your main nib.

How to pass a variable from one view controller to another?

I have three view controllers, one root controller, one login view controller and one customers view controller. I want to pass the entered username and password in login view controller to the customers view controller. My files and code is displayed below, could you please guide me, how can access to variables set in the login view controller? Or how can I pass variables to customers view controller?
I have these class files:
/classes/MySoftwareAppDelegate.h
/classes/MySoftwareAppDelegate.m
/classes/ViewController.h
/classes/ViewController.m
/classes/LoginController.h
/classes/LoginController.m
/classes/CustomersController.h
/classes/CustomersController.m
I have these views:
/resources/MainWindow.xib
/resources/Login.xib
/resources/Customers.xib
In the AppDelegate, I have successfully inserted the sub view "Login" and it's displayed whenever the app starts.
In the login view, I enter my username and password and then click the "Login" button. When this button is clicked, an IBAction is triggered. In this IBAction, I want to change the current subview with the Customers.
Here's the code I have used:
MySoftwareAppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface MySoftwareAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#end
MySoftwareAppDelegate.m
#import "MySoftwareAppDelegate.h"
#import "ViewController.h"
#implementation MySoftwareAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#class LoginController;
#interface ViewController : UIViewController {
LoginController *loginController;
}
#property (nonatomic, retain) LoginController *loginController;
#end
ViewController.m
#import "ViewController.h"
#import "LoginController.h"
#implementation ViewController
#synthesize loginController;
- (void)viewDidLoad {
LoginController *tmpViewController = [[LoginController alloc] initWithNibName:#"Login" bundle:nil];
self.loginController = tmpViewController;
[self.view insertSubview:loginController.view atIndex:0];
[tmpViewController release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
if (self.loginController.view.superview == nil) {
self.loginController = nil;
}
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[loginController release];
[super dealloc];
}
#end
LoginController.h
#import <UIKit/UIKit.h>
#class CustomersController;
#interface LoginController : UIViewController {
UIButton *loginButton;
UITextField *usernameTextField;
UITextField *passwordTextField;
NSMutableString *available_credits;
NSString *current_xml_element;
CustomersController *customersController;
}
#property (nonatomic, retain) IBOutlet UIButton *loginButton;
#property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
#property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
#property (nonatomic, retain) NSMutableString *available_credits;
#property (nonatomic, retain) NSString *current_xml_element;
#property (nonatomic, retain) CustomersController *customersController;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)loginToAccount:(id)sender;
#end
LoginController.m
#import "LoginController.h"
#import "CustomersController.h"
#implementation LoginController
#synthesize loginButton;
#synthesize usernameTextField;
#synthesize passwordTextField;
#synthesize customersController;
- (void)viewDidLoad {
UIImage *buttonImageNormal = [UIImage imageNamed:#"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[loginButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[loginButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[usernameTextField release];
[passwordTextField release];
[super dealloc];
}
-(IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
-(IBAction)backgroundTap:(id)sender {
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
-(IBAction)loginToAccount:(id)sender {
// bla bla bla... Login check process is done here
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:#"Customers" bundle:nil];
self.customersController = tmpViewController;
[self presentModalViewController:tmpViewController animated:YES];
[self.view removeFromSuperview];
[tmpViewController release];
}
#end
As you can see above, in LoginController.m's loginToAccount method, I am checking the login info and then setting the new view controller for the "customers" sub-view.
Then I am removing the current "Login" subview from the super view but don't know how to add the new "Customers" sub view.
In MainWindow.xib, I have one view controller which is linked to ViewController class and it's the root contoller.
Any help is appreciated. Because I am new to Objective-C and iPhone programming, please do your best to explain considering a novice programmer :)
Thanks again.
Okay, let me answer my question. I just found the answer on StackOverFlow.com
In the view controller which is going to load the next view controller, just add these lines:
NextController *tmpViewController = [[NextController alloc] initWithNibName:#"NextView" bundle:nil];
tmpViewController.enteredUsername = usernameTextField.text;
tmpViewController.enteredPassword = passwordTextField.text;
I'd say that better way is to have separate class for storing globally needed data (and that would be compliant with MVC model).
For example you can store you login information in your MySoftwareAppDelegate, which is easily accessible with [[UIApplication sharedApplication] delegate] call from any part of your application.
It all depends on how serious the data you want to pass it. For a quick variable (maybe a settings change in a modal view controller) TamTam's solution makes the most sense. You alloc/init'ed it, you got the variable, why not access it properties? That same (modally presented) view controller might pass variables back via a delegate pattern.
If you're data needs to be system wide, you can use the singleton pattern. Using "[[UIApplication sharedApplication] delegate]" gets the application delegation (which is a singleton), and many people stuff their variables there for convenience. However, your app delegate wasn't designed for this, and so it's considered bad form. Create your own singleton if your apple isn't a quickie.
If you use a persistent data store like sql, plists or coredata, you can put your system wide data there.