iphone - iOS SplitView/Universal 帮助需要 xcode 4.2

标签 iphone ios ipad uisplitviewcontroller xcode4.2

我刚刚开始学习 Objective-C 和 iOS 开发,在尝试将 iPhone 应用程序迁移到 iPad 时遇到了一些麻烦。

我一直在阅读 Head First iPhone & iPad Development 2nd Edition,但第 7 章“迁移到 iPad”在 xcode 4.2 中已过时。 该应用程序演示了如何使用带有表格 View 和详细信息 View 的 Split View。
当将 iOS 应用程序目标从 iPhone 更改为 Universal 时,他们会自动创建 MainWindow-iPad.xib。但这在 xcode 4.2 中并没有发生。我在 AppDelegate 中以编程方式创建了一个 splitview Controller 。这是代码:

- (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 的左侧(表格 View )表现良好,但当我在左侧选择不同的行时,无法更改右侧(详细信息侧)。这是我在 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];
}

如果我需要澄清任何事情,请告诉我。

谢谢

最佳答案

我也在读“Head First iPhone and 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)添加书中的代码

关于iphone - iOS SplitView/Universal 帮助需要 xcode 4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290868/

相关文章:

ios - 特殊弹出 View Controller

c# - MonoTouch 通用 iPhone/iPad 应用程序和代码复制

iphone - 调整 UITableViewCell 中的图像大小,使其不等于总高度

iphone - 提交应用程序 - 添加本地化,​​但组合框中未列出我的语言?

iphone - iOS6 中的 UISlider 自定义问题

ios - QuickLook ShouldOpenUrl 委托(delegate)方法未触发

iOS - 是否可以暂停执行,让主循环运行,然后恢复执行?

ios - iOS Swift Web Socket SRWebSocket设置响应超时

iphone - 如何防止 iOS 设备上的显示屏变暗和关闭?

iphone - 在 iOS 5 上以编程方式关闭 UIAlertView 不会调用 didDismiss 委托(delegate)方法