MGSplitViewController不是作为RootView而是在UIViewController中 [英] MGSplitViewController not as RootView but within a UIViewController

查看:110
本文介绍了MGSplitViewController不是作为RootView而是在UIViewController中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS编程的新手(来自Java / C ++)。我正在尝试使用TabBarController设置一个应用程序,其中一个选项卡应该是SplitView。我已经完成了我的研究,我知道UISplitview无法运行,并且人们建议使用MGSplitViewController。我看过这个演示,但是我不知道如何使用它而没有它应用程序的根视图,并且找不到任何可以帮助
的示例代码所以这就是我对这些类做的事情从我在一个单独的UIViewController类中的演示开始,然后我添加到TabBarController:这是我的类:

I'm very new to iOS programming (Coming from Java / C++). I'm trying to set up an app with a TabBarController of which one tab should be a SplitView. I've done my research and I know that UISplitview will not work and everywhere people recommend using the MGSplitViewController. I've looked at the demo but I just can't figure out how to use it without it beeing the app's root view and can't find any sample code that could help So here is what I do with the classes from the demo in a separate UIViewController class that I afterwards add to the TabBarController: This is my class:

#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

这是我绝望的尝试设置它

And this is my desperate try to set it up

- (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中看不到代码中的演示吗?
我得到拆分视图,但没有内容,特别是没有带有演示附带按钮的导航栏。

I guess I'm doing something wrong with delegates? Or do I have something else mixed up? Is the demo doing things in the IB that I can't see in the code? I get the split view but no content and especially no navigation bar with the buttons the demo comes with.

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

I'd be very thankful for hints or sample code!

推荐答案

好的manny,我们走了。这是我的界面工作代码:

Ok manny, here we go. This is my working code for the interface:

#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的按钮class但是有一个空的xib条目。所以创建必须通过loadView。也许我可以在loadView中完成viewDidLoad的东西......但是我必须保护viewDidLoad不被调用两次。一旦从MGSplitViewController类实例化视图,就会在loadView中发生这种情况,因为initWithCoder正在调用 [self setup] 。在该函数中,使用self.view.bounds计算frame rect,以便再次调用viewDidLoad,因为视图尚不存在。也许有人可以在MGSplitViewController.m中实现一个解决方法,但是我太懒了。

My MainWindow.xib has a UITabBarController and the button for splitMain is configured for this class but with an empty xib entry. So creation has to go via loadView. Maybe I could have done the viewDidLoad stuff within loadView ... but so I had to protect viewDidLoad from being called twice. That happens in loadView as soon as the view is instantiated from MGSplitViewController class because the initWithCoder there is calling [self setup]. In that function the frame rect is calculated with self.view.bounds so that viewDidLoad is called again because the view doesn't exist yet. Maybe one could implement a workaround within MGSplitViewController.m but I was too lazy doing that.

为了让这个工作在标签栏控制器上,请确保你承诺大部分的在MGSplitViewController的git页面上发布的更改。祝你好运。

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.

这篇关于MGSplitViewController不是作为RootView而是在UIViewController中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆