我刚刚开始学习Objective-C和iOS开发,并且在尝试将iPhone应用程序迁移到iPad时遇到了一些麻烦。
我一直在阅读Head First iPhone& iPad开发第2版,但第7章“迁移到iPad”已经过时了xcode 4.2。 该应用程序演示了如何使用带有表视图和详细视图的splitview 将iOS应用程序目标从iPhone更改为通用时,它们具有MainWindow-iPad.xib自动创建功能。但是xcode 4.2中没有发生这种情况。我在AppDelegate中以编程方式创建了一个splitview控制器。这是代码:
- (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;
}
}
splitview的左侧(表格视图)表现良好,但是当我在左侧选择不同的行时,我无法改变右侧(细节侧)。这是我在MasterViewController类中的代码。
- (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];
}
}
}
这是我在DetailViewController类中的代码
-(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];
}
如果我需要澄清任何事情,请告诉我。
谢谢
答案 0 :(得分:1)
我也在阅读“Head First iPhone和iPad Development”。在KevinM的代码的帮助下 我已经以编程方式创建了一个没有xib的UISplitController。这是我的解决方案。
以下是我在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;
}
以下是我在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];
}
}
并在第345页上添加代码(方法refreshView)和第346页(属性splitViewDetailView)