MGSplitViewController not as RootView but within a UIViewController

这一生的挚爱 提交于 2019-11-30 16:40:38

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

and here excerpts from implementation file:

- (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;
}

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!