标签:
UISplitViewController也是iPad的特有类。用storyboard方式创建UISplitViewController暂且不谈。
(1)直接从代码创建的话,先在AppDelegate中设置window的根控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; WPMainSplitViewController *splitVc=[[WPMainSplitViewController alloc]init]; self.window.rootViewController=splitVc; [self.window makeKeyAndVisible]; return YES; }
- (void)viewDidLoad { [super viewDidLoad]; //设置两个装有tableView的导航控制器作为左右视图 LeftTableViewController *left=[[LeftTableViewController alloc]init]; UINavigationController *leftNav=[[UINavigationController alloc]initWithRootViewController:left]; RightTableViewController *right=[[RightTableViewController alloc]init]; UINavigationController *rightNav=[[UINavigationController alloc]initWithRootViewController:right]; self.viewControllers=[NSArray arrayWithObjects:leftNav,rightNav, nil];
(2)常用的是代理。分隔视图控制器有自带的代理,UISplitViewControllerDelegate
比如,在上面的代码中加入:
- (void)viewDidLoad { [super viewDidLoad]; ...... self.delegate=right; …… }
//显示或者隐藏一般都是针对左边的那个菜单式的视图 -(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ //按钮点击事件官方已经封装 self.navigationItem.leftBarButtonItem=nil; } -(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc{ barButtonItem.title=@"班级"; self.navigationItem.leftBarButtonItem=barButtonItem; }
- (void)viewDidLoad { [super viewDidLoad]; //当需要点击左边,右边做出响应时,可以利用代理(取出左右两边的控制器,并设置代理) LeftTableViewController *leftOne=[self.viewControllers firstObject]; RightTableViewController *rightOne=[self.viewControllers lastObject]; leftOne.delegate=rightOne; }
注意:一般项目中不是直接在split视图控制器中写出master和detail视图控制器,所以如果要取得左右2个视图的话,需要用如上得方法获取,即用firstObject和lastObject从self.viewControllers中获取。
【iOS开发-107】UISplitViewController:如何取得master和detail控制器,核心在于如何运用代理
标签:
原文地址:http://blog.csdn.net/weisubao/article/details/43021153