使用 Objective C 中的 Delegates 進行 Master 和 Detail View 互動
UISplitViewController
必須是應用程式的 rootViewController。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
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
建立一個物件,並將該 viewcontroller 設定為應用程式的 rootviewcontroller。
SplitViewController.h
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface ViewController : UISplitViewController
{
DetailViewController *detailVC;
MasterViewController *masterVC;
NSMutableArray *array;
}
@end
MasterViewController
始終位於裝置的左側,你可以在 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;
}
建立的主檢視和詳細資訊 ViewControllers 被新增到一個陣列中,該陣列在 UISplitViewController
中設定為 self.viewControllers
。self.preferredDisplayMode
是為 DisplayMode 顯示 master 和 DetailViewController
Apple 文件的模式。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:(UIViewController *)viewController
方法建立 DetailViewDelegate
委託,將 UIViewController
傳送到 DetailViewcontroller
。然後在 MasterViewController
中,mainTableView 是 leftside 中的 tableview。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
建立一些 UIViewControllers
並將其新增到陣列中。表檢視初始化然後在 didSelectRowAtIndexPath
方法我使用 detailDelegate
傳送 UIViewController
到 DetailViewController
,相應的 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
中的所有檢視並新增從 MasterViewController
傳遞的 UIViewController
新增應用程式的一些螢幕截圖
在啟動應用程式時,我們沒有得到 MasterViewController
,因為我們在螢幕上刷了 preferredDisplayMode
,我們得到了如下圖所示的 MasterViewController
但在橫向模式下我們得到了 MasterViewController
和 DetailViewController
論景觀組織