Blank screen on device; simulator working fine - iphone

I am writing an iOS app with a navigation controller. When I open it up on the Simulator, it runs fine. When I run it on the device, a blank screen is displayed below the status bar. Plus I can't figure out where my RootViewController is made to be the default view (which I suspect is the root of my problem).
#class RootViewController;
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet RootViewController *viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
// ...
return YES;
}
RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Main Menu";
}
No viewWillAppear, viewDidAppear, etc.
Displays a table of 0 elements.
- (UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tv.backgroundColor = [UIColor whiteColor];
UITableViewCell *cell;
if (indexPath.row == 0)
cell = newsCell;
else if (indexPath.row == 1)
cell = configureCell;
else if (indexPath.row == 2)
cell = aboutCell;
return cell;
}
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
return 0;
}
#pragma mark UITableViewDelegate Methods
- (CGFloat)tableView:(UITableView *)tv
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 84;
}
- (void) tableView:(UITableView *)tv
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (0 == indexPath.row)
{
}
else if (1 == indexPath.row)
{
}
else if (2 == indexPath.row)
{
}
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[tableView release];
[newsCell release];
[configureCell release];
[aboutCell release];
}
RootViewController.h
#interface RootViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
UITableView *tableView;
IBOutlet UIView *displaySplashScreen;
IBOutlet UITableViewCell *newsCell, *configureCell, *aboutCell;
}
#property (nonatomic, retain) IBOutlet UITableView *tableView;

There are a few issues here. First, your AppDelegate header should read:
#class RootViewController;
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
Then, in the implementation, add the root to the navigation controller and the navController to the window like this:
#import "RootViewController.h"
#implementation MyAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
rootViewController = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
return YES;
}

PengOne's answer is correct, except I'd make one small change. Do this:
#import "RootViewController.h"
#implementation MyAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
rootViewcontroller = [[RootViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
It's a better way to show a nav controller. As PengOne also said, it has to be a problem with your Interface Builder file. Unhook everything then hook it up again to see if the problem persists. If it does, check to make sure everything is named correctly. Good luck!

Related

UITableView is not Getting displayed in iPhone

I am using Xcode 3.2.3.
Now i am doing simple UITable view.For that i referred this link.
I have done all the steps mentioned there.
But when my application starts it shows only white screen. No any tableview.
please provide me solution.
Thanks in advance.
MyCode is
AppDelegate.h
#import <UIKit/UIKit.h>
#class TableBasicsViewController;
#interface TableBasicsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
TableBasicsViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet TableBasicsViewController *viewController;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "TableBasicsViewController.h"
#implementation TableBasicsAppDelegate
#synthesize window;
#synthesize viewController;
#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.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
TableBasicViewController.h
#import <UIKit/UIKit.h>
#interface TableBasicsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
}
#property (nonatomic, retain) NSArray *games;
#end
TableBasicViewController.m
#import "TableBasicsViewController.h"
#implementation TableBasicsViewController
#synthesize games;
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.games = [NSArray arrayWithObjects: #"Super Mario Bros.",
#"The Legend of Zelda", #"Blades of Steel",
#"Teenage Mutant Ninja Turtles", #"Excitebike",
#"Dr. Mario", #"Duck Hunt", #"Tetris", #"Ice Climber",
#"River City Ransom", #"Ninja Gaiden", #"Super Mario Bros. 3",
#"Mega Man 2", #"Kid Icarus", #"Metroid", #"Metal Gear",
#"Super Mario Bros. 2", #"Zelda II: The Adventure of Link",
nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return [self.games count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Default";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [self.games objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected: '%#'", [self.games objectAtIndex:indexPath.row]);
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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 {
self.games = nil;
[super dealloc];
}
#end
Please check this
TableBasicViewController.h
#interface TableBasicsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UITableView *tableView;
}
#property (nonatomic, retain) NSArray *games;
#end
and link this in your xib too
Write This Code in Your AppDelegate.m File
- (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.
viewController = [[TableBasicsViewController alloc]initWithNibName:#"TableBasicsViewController"];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}

Passing object using story board iOS5

I am trying to pass an object to a static grouped table view that I have created in story board.
Here is the code that I am using in my first view to push the second view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row Selected");
CustomerDetailTableViewController *detailView = [[self storyboard] instantiateViewControllerWithIdentifier:#"DetailsView"];
detailView.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
NSLog(#"%#",detailView.customer.firstName);
[self.navigationController pushViewController:detailView animated:YES];
}
The NSlog for the firstName is correct but when the detail view is pushed the cells in the detailView are null. I'm probably just missing something dumb but a fresh set of eyes would be much appreciated.
Here is the code for the detailView controller:
CustomerDetailTableViewController.h
#class Customer;
#import <UIKit/UIKit.h>
#interface CustomerDetailTableViewController : UITableViewController{
Customer *customer;
UILabel *fullName;
UILabel *address;
UILabel *homePhone;
UILabel *cellPhone;
UILabel *email;
}
#property (nonatomic, strong) IBOutlet UILabel *fullName;
#property (nonatomic, strong) IBOutlet UILabel *address;
#property (nonatomic, strong) IBOutlet UILabel *homePhone;
#property (nonatomic, strong) IBOutlet UILabel *cellPhone;
#property (nonatomic, strong) IBOutlet UILabel *email;
#property (nonatomic, strong) Customer *customer;
#end
CustomerDetailTableViewController.m
#import "CustomerDetailTableViewController.h"
#import "Customer.h"
#implementation CustomerDetailTableViewController
#synthesize fullName, address, homePhone, cellPhone, email, customer;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
fullName = [NSString stringWithFormat:#"%# %#", customer.firstName, customer.lastName];
address = [NSString stringWithFormat: #"%#/n%#, %# %#", customer.address, customer.city, customer.state, customer.zipCode];
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
So, you have created the two views (the first TableView and the CustomerDetailTableViewController) with storyboards?
In that case, you have to click on the connection line between the two views into the storyboard and set the "Identifier" field, into the section "Storyboard Segue", to something like "setCustomer". Here's a screenshot:
Here a little screenshot:
After that, you can comment the method tableView:didSelectRowAtIndexPath: on the first TableView, and replace with this method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"setCustomer"]) {
CustomerDetailTableViewController *customerDetailVC = (CustomerDetailTableViewController *)[segue destinationViewController];
customerDetailVC.customer = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForSelectedRow]];
}
}
Remember to include
#include "CustomerDetailTableViewController.h"
at the top of implementation file.
I hope this help!

connect a UINavigation Controller to table view within view based app

I have an view based app that has a main menu. In the main menu the user can select an option that presents a table that is populated from an SQLite database (BrowseView). That is all working fine, however from the table I want the user to be able to select a row of data that will present a detailed view and I cannot get the detailed view to work. Nothing happens.
I think the problem is that I havn't connected the detailed view to the UINavigation controller but I'm unsure on how to do this as I have tried everything that I can think of (which is probably not much as I'm new to programming).
Any help on this would be greatly appreicated. Here is my code.
In the appDelegate.h
#class ClubFindViewController;
#interface ClubFindAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ClubFindViewController *viewController;
UINavigationController *navigationController;
NSMutableArray *clubArray;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ClubFindViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) NSMutableArray *clubArray;
-(void) copyDatadaseIfNeeded;
-(NSString *) getDBPath;
#end
AppDelegate.m
#implementation ClubFindAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize navigationController;
#synthesize clubArray;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self copyDatadaseIfNeeded];
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
self.clubArray = tempArray;
[tempArray release];
[Clubs getInitialDataToDisplay:[self getDBPath]];
self.window.rootViewController = self.navigationController;
[self.window addSubview:navigationController.view];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
DetailView.h
#class Clubs;
#interface DetailViewController : UITableViewController {
IBOutlet UITableView *tableView;
Clubs *clubObj;
}
#property (nonatomic, retain) Clubs *clubObj;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#end
DetailView.m
#import "DetailViewController.h"
#import "Clubs.h"
#import "BrowseViewController.h"
#implementation DetailViewController
#synthesize clubObj;
#synthesize tableView;
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = clubObj.clubName;
[self.tableView reloadData];
}
-(NSString *)tableView:(UITableView *)tblView titleForHeaderInSection: (NSInteger)section {
NSString *sectionName = nil;
switch (section) {
case 0:
sectionName = [NSString stringWithFormat:#"Club"];
break;
case 1:
sectionName = [NSString stringWithFormat:#"Address"];
break;
}
return sectionName;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text = clubObj.clubName;
break;
case 1:
cell.textLabel.text = clubObj.ClubAddress;
break;
}
return cell;
}
Updated code.
When you create your app and want to use UINavigationController you must in your AppDelegate tell that UINavigationController is your rootViewController.
self.window.rootViewController = self.navigationController;
When the Detail view will be pushed it will already (know) that it is inside the UINavigationController and so you don't need to create additional UINavigationController.
So delete UINavigationController *navigationController; from both .h and .m file.

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];

My UITabBarController's didSelectViewController method is not getting called?

Here is my code stub for my app-delegate.m -- it never gets called.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%s", __FUNCTION__);
}
It is defined in this app-delegate.h
#interface OrioleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
Did you make a connection between your UITabBarController and your application delegate?
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
...
tabBarController.delegate = self;
...
}
If your ViewController is a UITabBarController, you need to set self as it's delegate because you can't change the delegate of the UITabBar directly.
For example, in the ViewDidLoad of your UITabBarController :
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
I added the following tabBarController.delegate = self; and all is well. I hope this is helpful to others.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}