使用 Objective C 中的代理在主视图和详细信息视图之间进行交互
UISplitViewController
需要应用程序窗口的根视图控制器
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
self.window.clipsToBounds = YES;
SplitViewController *spView = [[SplitViewController alloc]init];
self.window.rootViewController = spView;
[self.window makeKeyAndVisible];
return YES;
}
只需为 UISplitVIewController
创建一个对象,并将其设置为应用程序的 rootViewController。
SplitViewController.h
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface ViewController : UISplitViewController
{
DetailViewController *detailVC;
MasterViewController *masterVC;
NSMutableArray *array;
}
@end
MasterViewController
是设置在设备左侧的 UIViewController
,你可以使用 maximumPrimaryColumnWidth
在 UISplitViewController
中设置宽度,DetailViewController
在右侧
SplitViewController.m
#import "ViewController.h"
#define ANIMATION_LENGTH 0.3
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
masterVC = [[MasterViewController alloc]init];
detailVC = [[DetailViewController alloc]init];
[masterVC setDetailDelegate:(id)detailVC];
NSArray *vcArray = [NSArray arrayWithObjects:masterVC, detailVC, nil];
self.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
self.viewControllers = vcArray;
self.delegate = (id)self;
self.presentsWithGesture = YES;
}
主人和细节 UIViewController
被添加到 NSArray
,设置为 self.viewControllers
。self.preferredDisplayMode
是用于显示 MasterViewController
和 DetailViewController
的模式。self.presentsWithGesture
启用滑动手势以显示 MasterViewController
MasterViewController.h
#import <UIKit/UIKit.h>
@protocol DetailViewDelegate <NSObject>
@required
- (void)sendSelectedNavController:(UIViewController *)viewController;
@end
@interface MasterViewController : UIViewController
{
UITableView *mainTableView;
NSMutableArray *viewControllerArray;
}
@property (nonatomic, retain) id<DetailViewDelegate> detailDelegate;
@end
使用 sendSelectedNavController
方法创建 DetailViewDelegate
代表,将 UIViewControllers
发送到 DetailViewController
。然后在 MasterViewController
中创建了一个 UITableView
。ViewControllerArray
包含需要在 DetailViewController
中显示的所有 UIViewControllers
MasterViewController.m
#import "MasterViewController.h"
@implementation MasterViewController
@synthesize detailDelegate;
-(void)viewDidLoad
{
[super viewDidLoad];
UIViewController *dashBoardVC = [[UIViewController alloc]init];
[dashBoardVC.view setBackgroundColor:[UIColor redColor]];
UIViewController *inventVC = [[UIViewController alloc]init];
[inventVC.view setBackgroundColor:[UIColor whiteColor]];
UIViewController *alarmVC = [[UIViewController alloc]init];
[alarmVC.view setBackgroundColor: [UIColor purpleColor]];
UIViewController *scanDeviceVC = [[UIViewController alloc]init];
[scanDeviceVC.view setBackgroundColor:[UIColor cyanColor]];
UIViewController *serverDetailVC = [[UIViewController alloc]init];
[serverDetailVC.view setBackgroundColor: [UIColor whiteColor]];
viewControllerArray = [[NSMutableArray alloc]initWithObjects:dashBoardVC,inventVC,alarmVC,scanDeviceVC,serverDetailVC,nil];
mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50,self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
[mainTableView setDelegate:(id)self];
[mainTableView setDataSource:(id)self];
[mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[mainTableView setScrollsToTop:NO];
[self.view addSubview:mainTableView];
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [viewControllerArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = [NSString stringWithFormat:@"Cell%li%ld",(long)indexPath.section,(long)indexPath.row];
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
[cell.contentView setBackgroundColor:[UIColor redColor]];
cell.textLabel.text =[NSString stringWithFormat:@"My VC at index %ld",(long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[detailDelegate sendSelectedNavController:[viewControllerArray objectAtIndex:indexPath.row]];
}
@end
创造了一些 UIViewController
并将它添加到 NSMutableArray
。UITableView
初始化然后在 didselectrowatindexpath
方法我使用 detailDelegate
委托发送 UIViewController
到 DetailViewController
,NSMutableArray
中相应的 UIViewController
作为参数
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController<UICollectionViewDelegate>
{
UIViewController *tempNav;
}
@end
DetailViewController.m
#import "DetailViewController.h"
@implementation DetailViewController
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
-(void)sendSelectedNavController:(UIViewController *)navController
{
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
tempNav = navController;
[self.view addSubview:tempNav.view];
}
@end
sendSelectedNavController
在这里声明删除 DetailViewController
中的所有 UIView
并添加从 MasterViewController
传递的 UIViewController
。