我是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中做我在代码中看不到的事情?
我得到了拆分视图,但没有内容,尤其是演示附带的按钮没有导航栏。
我非常感谢提示或示例代码!
最佳答案
好吧,曼尼,我们走了。这是我的界面工作代码:
#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]。在该函数中,使用rect.view.bounds计算帧rect,以便再次调用viewDidLoad,因为视图尚不存在。也许可以在MGSplitViewController.m中实现一种解决方法,但是我太懒了。
为了使此功能在选项卡栏控制器上起作用,请确保您提交了MGSplitViewController的git页面上发布的大多数更改。祝好运。
关于ios - MGSplitViewController不是作为RootView,而是在UIViewController中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7039629/