2011-11-28 102 views
1

我剛剛開始學習Objective-C和iOS開發,並且嘗試將iPhone應用程序遷移到iPad時遇到了一些麻煩。iOS SplitView/Universal需要xcode 4.2

我一直在閱讀Head First iPhone & iPad Development 2nd Edition,但第7章「遷移到iPad」已過期,因爲xcode 4.2已過期。 該應用程序演示瞭如何在表視圖和詳細視圖中使用拆分視圖。
將iOS應用程序目標從iPhone更改爲Universal時,它們具有自動創建的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]; 
} 

請讓我知道如果我需要澄清什麼。

謝謝

回答

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

而且從書添加代碼(方法refreshView)和頁面346(屬性splitViewDetailView)