iOS SplitView / 通用帮助需要 xcode 4.2

Posted

技术标签:

【中文标题】iOS SplitView / 通用帮助需要 xcode 4.2【英文标题】:iOS SplitView / Universal help needed xcode 4.2 【发布时间】:2011-11-28 02:22:01 【问题描述】:

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

我一直在阅读 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;
    

拆分视图(表格视图)的左侧表现良好,但是当我在左侧选择不同的行时,我无法更改右侧(详细信息侧)。这是我在 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 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)添加书中的代码

【讨论】:

以上是关于iOS SplitView / 通用帮助需要 xcode 4.2的主要内容,如果未能解决你的问题,请参考以下文章

IOS8 SplitVC + TabBarController + NavigationController

如何在splitview中改变按钮的位置 uwp

Splitview 转场和展开

iOS6 中奇怪的 SplitView/NavigationController 行为

UWP Xaml将属性绑定到splitview中的其他页面

IOS SplitView TableViewController 查询