Reputation: 1807
I'm just starting to learn objective-C and iOS development and I've ran in to some trouble with trying to migrate a iPhone app to iPad.
I've been reading Head First iPhone & iPad Development 2nd Edition, but chapter 7 " migrating to iPad" is out of date as of xcode 4.2.
The app is a demonstration of how to use a splitview with a table view and detail view.
They have a MainWindow-iPad.xib auto create when changing the iOS Application Target from iPhone to Universal. But this isn't happening for me in xcode 4.2. I have created a splitview controller programmatically in AppDelegate. Here is the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
MasterViewController *firstVC = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
self.secondVC = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
UINavigationController *firstVCnav = [[[UINavigationController alloc] initWithRootViewController:firstVC] autorelease];
UINavigationController *secondVCnav = [[UINavigationController alloc] initWithRootViewController:self.secondVC];
UISplitViewController *splitVC = [[UISplitViewController alloc] init];
splitVC.viewControllers = [NSArray arrayWithObjects:firstVCnav, secondVCnav, nil];
self.window.rootViewController= splitVC;
[self.window makeKeyAndVisible];
return YES;
}else {
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;
}
}
The left side of the splitview (the table view) is perform good, but I can't get the Right side (the detail side) to change when I select different rows on the left side. Here is the code I have in the MasterViewController class.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
AppDelegate *splitVCdetails = [[AppDelegate alloc] init];
[splitVCdetails.secondVC drinkChanged:[self.drinks objectAtIndex:indexPath.row]];
}else {
if (!self.editing) {
if (!self.detailViewController) {
self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
}
self.detailViewController.drink = [self.drinks objectAtIndex:indexPath.row];
[self.navigationController pushViewController:self.detailViewController animated:YES];
}else {
AddDrinkViewController *editingDrinkVC = [[AddDrinkViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
editingDrinkVC.drink = [self.drinks objectAtIndex:indexPath.row];
editingDrinkVC.drinkArray = self.drinks;
UINavigationController *editingNavCon = [[UINavigationController alloc] initWithRootViewController:editingDrinkVC];
[self.navigationController presentModalViewController:editingNavCon animated:YES];
[editingDrinkVC release];
[editingNavCon release];
}
}
}
Here is the code I have in the DetailViewController class
-(void)refreshView {
//Set up our UI with the provided drink
self.drinkTextLabel.text = [self.drink objectForKey:NAME_KEY];
self.ingredientTextBox.text = [self.drink objectForKey:INGREDIENTS_KEY];
self.directionTextBox.text = [self.drink objectForKey:DIRECTIONS_KEY];
}
-(void)drinkChanged:(NSDictionary *)newDrink {
self.drink = newDrink;
[self refreshView];
}
Please let me know if I need to clarify anything.
Thank you
Upvotes: 1
Views: 2139
Reputation: 1065
I'm reading "Head First iPhone and iPad Development" too. With help from KevinM's code I have created a UISplitController programmatically without xib. Here is my solution.
Here is the code I have at the beginning of AppDelegate.m:
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "DetailViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize navigationController = _navigationController;
@synthesize splitViewController = splitViewController_;
- (void)dealloc
{
[_window release];
[splitViewController_ release];
[_navigationController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
DetailViewController *detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
masterViewController.splitViewDetailView = detailViewController;
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
}
else {
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;
}
Here is the code I have in the MasterViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.editing) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.splitViewDetailView drinkChanged:[self.drinks objectAtIndex:indexPath.row]];
}
else {
self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
self.detailViewController.drink = [self.drinks objectAtIndex:indexPath.row];
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
}
else {
AddDrinkViewController *editingDrinkVC = [[AddDrinkViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
editingDrinkVC.drink = [self.drinks objectAtIndex:indexPath.row];
editingDrinkVC.drinkArray = self.drinks;
UINavigationController *editingNavCon = [[UINavigationController alloc] initWithRootViewController:editingDrinkVC];
[self.navigationController presentModalViewController:editingNavCon animated:YES];
[editingDrinkVC release];
[editingNavCon release];
}
}
And add code from book on page 345 (method refreshView) and page 346 (property splitViewDetailView)
Upvotes: 1