MGSplitViewController不是作为RootView而是在UIViewController中

时间:2011-08-12 11:45:43

标签: ios uitabbarcontroller uisplitview

我是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;
}

我想我对代表做错了什么?或者我还有别的东西混在一起? 演示中的演示是否在代码中看不到? 我得到了拆分视图但没有内容,特别是没有带有演示附带按钮的导航栏。

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

1 个答案:

答案 0 :(得分: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计算frame rect,以便再次调用viewDidLoad,因为视图尚不存在。也许有人可以在MGSplitViewController.m中实现一个解决方法,但我太懒了。

要在标签栏控制器上运行,请确保提交在MGSplitViewController的git页面上发布的大多数更改。祝你好运。