简体   繁体   English

iOS SplitView /通用帮助需要xcode 4.2

[英]iOS SplitView / Universal help needed xcode 4.2

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. 我刚刚开始学习Objective-C和iOS开发,并且在尝试将iPhone应用程序迁移到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. 我一直在阅读Head First iPhone&iPad Development 2nd Edition,但是从xcode 4.2开始,第7章“迁移到iPad”已经过时了。 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. 将iOS应用程序目标从iPhone更改为Universal时,会自动创建MainWindow-iPad.xib。 But this isn't happening for me in xcode 4.2. 但这在xcode 4.2中对我而言并没有发生。 I have created a splitview controller programmatically in AppDelegate. 我已经在AppDelegate中以编程方式创建了splitview控制器。 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. splitview(表视图)的左侧效果不错,但是当我在左侧选择不同的行时,无法更改右侧(详细信息侧)。 Here is the code I have in the MasterViewController class. 这是我在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];
        }
    }
}

Here is the code I have in the DetailViewController class 这是我在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];
}

Please let me know if I need to clarify anything. 请让我知道是否需要澄清任何事情。

Thank you 谢谢

I'm reading "Head First iPhone and iPad Development" too. 我也在阅读“ Head First iPhone和iPad开发”。 With help from KevinM's code I have created a UISplitController programmatically without xib. 在KevinM的代码的帮助下,我以编程方式创建了没有xib的UISplitController。 Here is my solution. 这是我的解决方案。

Here is the code I have at the beginning of AppDelegate.m: 这是我在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: 这是我在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) 并在第345页(方法refreshView)和第346页(属性splitViewDetailView)上从书中添加代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM