0

我对 iOS 编程非常陌生(来自 Java / C++)。我正在尝试使用 TabBarController 设置一个应用程序,其中一个选项卡应该是 SplitView。我已经完成了我的研究,并且我知道 UISplitview 将无法正常工作,而且人们在任何地方都推荐使用 MGSplitViewController。我查看了演示,但我无法弄清楚如何在没有应用程序根视图的情况下使用它,并且找不到任何可以帮助的示例代码所以这就是我对演示中的类所做的事情一个单独的 UIViewController 类,我后来添加到 TabBarController: 这是我的类:

#import <UIKit/UIKit.h>
#import "MGSplitCornersView.h"
#import "RootViewController.h"
#import "DetailViewController.h"



@interface ChannelViewController : UIViewController {
    MGSplitViewController *splitViewController;
    RootViewController *rootViewController;
    DetailViewController *detailViewController;

}

@property (nonatomic, retain) MGSplitViewController *splitViewController;
@property (nonatomic, retain) RootViewController *rootViewController;
@property (nonatomic, retain) DetailViewController *detailViewController;


@end

这是我拼命尝试的设置

- (id)initWithTabBar
{
    self = [super init];

    //this is the label on the tab button itself
    self.title = @"SplitView";

    //use whatever image you want and add it to your project
    //self.tabBarItem.image = [UIImage imageNamed:@"name_gray.png"];

    // set the long name shown in the navigation bar at the top
    self.navigationItem.title=@"Nav Title";

    self.splitViewController = [[MGSplitViewController alloc] init];
    self.rootViewController = [[RootViewController alloc] init];
    self.detailViewController = [[DetailViewController alloc] init];

    [self.splitViewController setDetailViewController:detailViewController];
    [self.splitViewController setMasterViewController:rootViewController];

    [self.view addSubview:splitViewController.view];

    [self.rootViewController performSelector:@selector(selectFirstRow) withObject:nil afterDelay:0];
    [self.detailViewController performSelector:@selector(configureView) withObject:nil afterDelay:0];

    if (NO) { // whether to allow dragging the divider to move the split.
    splitViewController.splitWidth = 15.0; // make it wide enough to actually drag!
    splitViewController.allowsDraggingDivider = YES;
    }

    return self;
}

我想我对代表做错了什么?还是我有其他混淆?演示是否在 IB 中做我在代码中看不到的事情?我得到了拆分视图,但没有内容,尤其是没有带有演示附带按钮的导航栏。

我会非常感谢提示或示例代码!

4

1 回答 1

3

好的,曼尼,我们开始吧。这是我的界面工作代码:

#import <UIKit/UIKit.h>
#import "MGSplitViewController.h"
#import "ecbView.h"
#import "ecbCalc.h"

@interface splitMain : MGSplitViewController <UIPopoverControllerDelegate,
                                              MGSplitViewControllerDelegate>
{
    IBOutlet UIPopoverController*       popoverController;
    IBOutlet UINavigationController*    naviController;
    IBOutlet ecbCalc*                   viewCalcLeft;
    IBOutlet ecbView*                   euroRatesRight;
             UIBarButtonItem*           savedButtonItem;
             BOOL                       keepMasterInPortraitMode;
             BOOL                       memoryWasDropped;
             BOOL                       viewLoaded;
}

@property (nonatomic, retain) UIPopoverController* popoverController;
@property (nonatomic, retain) UINavigationController* naviController;
@property (nonatomic, retain) ecbCalc* viewCalcLeft;
@property (nonatomic, retain) ecbView* euroRatesRight;
@property (nonatomic, retain) UIBarButtonItem* savedButtonItem;
@property (nonatomic, readonly) BOOL keepMasterInPortraitMode;
@property (nonatomic, readonly) BOOL memoryWasDropped;
@property (nonatomic, readonly) BOOL viewLoaded;

- (void)dismissPopoverController: (BOOL)animated;
- (void)settingsChanged;

@end

这里是实现文件的摘录:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        // my initialization...
    }

    return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    CGRect  rectFrame = CGRectMake(0.0, 20.0, 768.0, 1004.0 - 48.0);    // being above a tab bar!

    viewLoaded     = NO;
    self.view      = [[UIView alloc] initWithFrame:rectFrame];
    viewCalcLeft   = [[ecbCalc alloc] initWithNibName:@"ecbCalc" bundle:nil];
    euroRatesRight = [[ecbView alloc] initWithNibName:@"ecbView-iPad" bundle:nil];
    naviController = [[UINavigationController alloc] initWithRootViewController:self.viewCalcLeft];
    naviController.navigationBar.barStyle = UIBarStyleBlack;
    naviController.title = nil;
    viewCalcLeft.title   = NSLocalizedString(@"BtnTitleCalc",  @"");
    viewCalcLeft.view.hidden = NO;

    NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];

    if ([prefs objectForKey:@"iPadAlwaysSplitTableView"] != nil)
        self.keepMasterInPortraitMode = [prefs boolForKey:@"iPadAlwaysSplitTableView"];
    else
        self.keepMasterInPortraitMode = YES;

    NSArray*    theViewControllers = [NSArray arrayWithObjects:self.naviController, self.euroRatesRight, nil];

    [self setViewControllers:theViewControllers];
    [self setDelegate:self];
    [self setShowsMasterInPortrait:keepMasterInPortraitMode];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    // protection because this one is called twice
    if (viewLoaded)
        return;

    [super viewDidLoad];

    if (memoryWasDropped)
    {
        if (!self.keepMasterInPortraitMode && UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
        {
            // recreate popover controller
            self.popoverController = [[UIPopoverController alloc] initWithContentViewController:self.viewCalcLeft];
        }
    }

    viewLoaded = YES;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    memoryWasDropped = YES;

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self dismissPopoverController:NO];

    self.popoverController = nil;
    self.naviController    = nil;
    self.viewCalcLeft      = nil;
    self.euroRatesRight    = nil;
    viewLoaded = NO;
}

我的 MainWindow.xib 有一个 UITabBarController 并且 splitMain 的按钮是为这个类配置的,但有一个空的 xib 条目。所以创建必须通过 loadView。也许我可以在 loadView 中完成 viewDidLoad 的东西......但是我必须保护 viewDidLoad 不被调用两次。一旦从 MGSplitViewController 类实例化视图,loadView 就会发生这种情况,因为那里的 initWithCoder 正在调用[self setup]。在该函数中,使用 self.view.bounds 计算框架矩形,以便再次调用 viewDidLoad,因为视图尚不存在。也许可以在 MGSplitViewController.m 中实现一种解决方法,但我太懒了。

To get this working on a tab bar controller please make sure you commit most of the changes that are published on the MGSplitViewController's git page. Good luck.

于 2011-08-30T16:34:51.373 回答