Simple use of iOS_27SplitViewController
Finally:
Master controller BeyondViewController
Inherited from UISplitViewController <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> /// BeyondViewController. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // master controller, inherited from UISplitViewController. The master controller on the left is FoodTypeListCtrl, and the Slave controller on the right is FoodListCtrl # import @ Interface BeyondViewController: UISplitViewController @ end
//// BeyondViewController. m // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // master controller, inherited from UISplitViewController. The master controller on the left is FoodTypeListCtrl, and the Slave controller on the right is FoodListCtrl # import "BeyondViewController. h "// cuisine list controller # import" FoodTypeListController. h "// course list controller # import" FoodListController. h "// The protocol defined by the cuisine list controller. The purpose is to tell the agent (that is, the food list Controller) When you click a line in the cuisine list) which of the following dishes is displayed? # import "FoodTypeListCtrlDelegate. h "@ interface BeyondViewController ()
@ End @ implementation BeyondViewController // important ~~~ The main controller acts as the intermediary. First, you get FoodTypeListController and become its proxy. Then, you can get the corresponding cuisine object when a row of the cuisine list is clicked. // then, in the proxy method of FoodTypeListController, get FoodListController, and pass the cuisine to FoodListController-(void) viewDidLoad {[super viewDidLoad]; // 1. first obtain the Master controller, that is, the navigation controller, and then obtain FoodTypeListController from the navigation controller, and become its proxy UINavigationController * foodTypeListNav = [self. childViewControllers firstObject]; FoodTypeListController * foodTypeListCtrl = [foodTypeListNav. childViewControllers firstObject]; foodTypeListCtrl. delegate = self; // Let foodListCtrl act as the Master controller proxy, only listen to the appearance and hiding of the Master controller, the corresponding prompt text is displayed on the left button of foodListCtrl (click the text to expand the hidden Master controller) UINavigationController * foodListNav = [self. childViewControllers lastObject]; FoodListController
* FoodListCtrl = [foodListNav. childViewControllers firstObject]; self. delegate = foodListCtrl;} // 2. then, in the FoodTypeListController proxy method, get FoodListController and pass the cuisine to FoodListController-(void) foodTypeListController :( FoodTypeListController *) foodTypesVc didSelectedFoodType :( FoodType *) type {UINavigationController * foodListNavi = [self. childViewControllers lastObject]; FoodListController * foodListCtrl = [foodListNavi. childViewControllers firstObject]; foodListCtrl. foodType = type; [foodListNavi popToRootViewControllerAnimated: YES];} @ end
Master controller of SplitViewCtrl,
Inherited from Table Controller
FoodTypeListController [cuisine list] and its agency
//// FoodTypeListController. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // Master controller of SplitViewCtrl, inherited from Table controller [cuisine list] # import
@ Protocol FoodTypeListCtrlDelegate; @ interface FoodTypeListController: UITableViewController // Member: proxy. When a row is clicked, the proxy is notified of the selected cuisine, the Controller on the right displays the corresponding menu name list @ property (weak, nonatomic) id
Delegate; @ end
//// FoodTypeListController. m // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "FoodTypeListController. h "# import" FoodType. h "# import" FoodTypeListCtrlDelegate. h "@ interface FoodTypeListController () // Member: array, which stores all the FoodType @ property (strong, nonatomic) NSArray * foodTypesArr of the objects loaded from plist to be converted from the dictionary one by one; @ end @ implementation FoodTypeListController // load only when getter accesses it. if (_ foodTypesArr = nil) {// classic, convert the dictionary array in the Plist file corresponding to the parameter to the object array of the class _ foodTypesArr = [FoodType objArrFromPlistName: @ "food_types.plist"];} return _ foodTypesArr;}-(void) viewDidLoad {[super viewDidLoad]; self. title = @ ""; // The default value is 0th. Call your own method and pass the data model to it [self. tableView selectRowAtIndexPath: kIndexPathZero animated: YES scrollPosition: UITableViewScrollPositionTop]; // display the selected status of row 0th. The default method of overwriting is viewWillAppear [self tableView: self. tableView didSelectRowAtIndexPath: kIndexPathZero];} // cancel some default system events and display the selected status-(void) viewWillAppear :( BOOL) for the first row) animated {// do nothing ...} # pragma mark-data source method // number of rows-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. foodTypesArr. count;} // cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * ID = @ "FoodType "; UITableViewCell * cell = [tableView metadata: ID]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: ID];} // extract the FoodType * type = self from the model array. foodTypesArr [indexPath. row]; // set unique content cell. textLabel. text = type. name; // return cell;} # pragma mark-proxy method-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// if the proxy (that is, the Slave controller on the right) needs to be sent to inform the current cuisine if ([self. delegate respondsToSelector: @ selector (foodTypeListController: didSelectedFoodType :)]) {FoodType * type = self. foodTypesArr [indexPath. row]; [self. delegate foodTypeListController: self didSelectedFoodType: type] ;}}@ end
Defined Protocol
//// FoodTypeListCtrlDelegate. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
@ Class FoodType; @ protocol FoodTypeListCtrlDelegate
@ Optional // when a line is clicked, the agent is notified of which type of cuisine is clicked. Thus, the Controller on the right displays the corresponding menu name list-(void) foodTypeListController :( FoodTypeListController *) ctrl didSelectedFoodType :( FoodType *) foodType; @ end
FoodListController
The Slave controller of SplitViewCtrl, inherited from the table Controller
Shows all the dishes under a certain dish [list of dishes]
//// FoodListController. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // The Controller of SplitViewCtrl inherits from the [list of dishes] of a certain cuisine under the table controller. // when you click a row in the left-side Navigation Pane controller, this controller will show all the dishes under this cuisine # import
@ Class FoodType; @ interface FoodListController: UITableViewController // data source. Based on the imported cuisine, use its idstr to piece together a new plist name, load, convert to an array of Food Model Objects @ property (strong, nonatomic) FoodType * foodType; @ end
//// FoodListController. m // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "FoodListController. h "# import" FoodDetailController. h "# import" Food. h "# import" FoodCell. h "# import" FoodType. h "// becomes the proxy of UISplitViewControllerDelegate. The purpose is to listen to its display and hide methods, so as to control the display and hide of leftBarButtonItem @ interface FoodListController ()
@ Property (strong, nonatomic) NSArray * foodsArr; @ end @ implementation FoodListController-(void) viewDidLoad {[super viewDidLoad];} // important ~~~ Intercept the setterFoodType method, set the title, load data from the corresponding Plist, convert it to an object array, and refresh the table-(void) setFoodType :( FoodType *) foodType {_ foodType = foodType; NSString * filename = [NSString stringWithFormat: @ "type _ % @ _ foods. plist ", foodType. idstr]; // classic, convert the dictionary array in the Plist file corresponding to the parameter into the object array self of this class in one sentence. foodsArr = [Food objArrFromPlistName: filename]; self. title = foodType. name; if (self. isViewLoaded) {// by default, the tableView is rolled to row 0th [self. tableView scrollToRowAtIndexPath: kIndexPathZero atScrollPosition: UITableViewScrollPositionTop animated: YES]; // refresh the table [self. tableView reloadData]; }}# proxy method of pragma mark-SplitViewCtrl // displays the Master controller-(void) splitViewController :( UISplitViewController *) svc willShowViewController :( UIViewController *) aViewController invalidatingBarButtonItem :( UIBarButtonItem *) barButtonItem {self. navigationItem. principal = nil;} // The Master controller will be hidden soon-(void) splitViewController :( UISplitViewController *) svc willHideViewController :( UIViewController *) aViewController principal :( principal *) barButtonItem forpovercontroller :( UIPopoverController *) pc {barButtonItem. title = @ "cuisine"; self. navigationItem. leftBarButtonItem = barButtonItem;} # pragma mark-data source method // number of rows-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. foodsArr. count;} // The FoodCell is highly encapsulated, and the Controller knows very little-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {FoodCell * cell = [FoodCell witcellhtableview: tableView]; cell. food = self. foodsArr [indexPath. row]; return cell ;}# pragma mark-proxy method // cell row Height-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 100 ;} // select a row-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// click the corresponding model Food * food = self. foodsArr [indexPath. row]; FoodDetailController * detailVc = [[FoodDetailController alloc] init]; // transmits the data model and intercepts detailVc. food = food; [self. navigationController pushViewController: detailVc animated: YES];} @ end
WebView displays the details of a dish.
//// FoodDetailController. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // click a line in the menu bar to display the details of the dish. Use webView to display the details # import
@ Class Food; @ interface FoodDetailController: UIViewController // data source, which dish to display @ property (strong, nonatomic) Food * food; @ end
//// FoodDetailController. m // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // click a line to display the details of the dish. Use webView to display # import "FoodDetailController. h "# import" Food. h "@ interface FoodDetailController () @ property (weak, nonatomic) UIWebView * webView; @ end @ implementation FoodDetailController // make Weibo view the Controller's view-(void) loadView {UIWebView * webView = [[UIWebView alloc] init]; // bounds indicates the whole area of the screen, and applicationFrame indicates the area displayed by the app, excluding the status bar webView. frame = [UIScreen mainScreen]. applicationFrame; // automatically scales the webView Based on the width and height of the screen. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self. view = webView; // member variable. Remember to avoid strong self conversion. webView = webView;}-(void) viewDidLoad {[super viewDidLoad]; // navigation bar title self. title = self. food. name; // weight Point ~~~ Concatenate a local url. Note: If you create a real folder in the sandbox, add the folder name NSString * fileName = [NSString stringWithFormat: @ "Html/food/example @.html", self. food. idstr]; NSURLRequest * request = [NSURLRequest requestWithURL: [[NSBundle mainBundle] URLForResource: fileName withExtension: nil]; [self. webView loadRequest: request];} @ end
Model
//// Food. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // Model: A dish Food corresponds to a dictionary in the plist file # import
@ Interface Food: NSObject // It must be exactly the same as the dictionary key in the plist file // id @ property (copy, nonatomic) NSString * idstr; // name of the dish @ property (copy, nonatomic) NSString * name; // small icon url @ property (copy, nonatomic) NSString * imageUrl; // webpage url @ property (copy, nonatomic) NSString * url; // estimated duration of this course @ property (copy, nonatomic) NSString * time; // how difficult it is to make @ property (copy, nonatomic) NSString * diff; @ end
//// FoodType. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // Model: A dish FoodType corresponds to a dictionary in the plist file # import
@ Interface FoodType: NSObject // ID @ property (copy, nonatomic) NSString * idstr; // cuisine name: for example, Sichuan cuisine home cooking @ property (copy, nonatomic) NSString * name; @ end
Encapsulated Cell View
//// FoodCell. h // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // custom cell. A cell displays the data in a food model. The purpose of passing in tableView to instantiate the cell is to encapsulate the data thoroughly so that the controller can do the least and knows the least # import
@ Class Food; @ interface FoodCell: UITableViewCell // data source model, which is provided to internal child controls for display. Internally, the setter method @ property (strong, nonatomic) Food * food is intercepted; // The purpose of passing in tableView to instantiate the cell is to encapsulate the most thoroughly and let the Controller do the least, knowing the least + (instancetype) cellWithTableView :( UITableView *) tableView; @ end
//// FoodCell. m // 27_SplitViewCtroller // Created by beyond on 14-8-31. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "FoodCell. h "# import" Food. h "# import" UIImageView + WebCache. h "@ interface FoodCell () @ property (weak, nonatomic) IBOutlet UIImageView * iconView; @ property (weak, nonatomic) IBOutlet UILabel * nameLabel; @ property (weak, nonatomic) IBOutlet UILabel * descLabel; @ end @ implementation FoodCell // The purpose of passing in tableView to instantiate the cell is to encapsulate the most thoroughly and let the Controller do the least and know the least + (instancetype) cellWithTableView :( UITableView *) tableView {// cellID must be exactly the same as static NSString * cellID = @ "FoodCell"; FoodCell * cell = [tableView dequeueReusableCellWithIdentifier: cellID]; if (cell = nil) {// create a cell from xib = [[[NSBundle mainBundle] loadNibNamed: cellID owner: nil options: nil] lastObject]; // The right side is the arrow cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;} return cell;} // data source model, which is provided to internal child controls. The setter method-(void) setFood :( Food *) is intercepted internally *) food {_ food = food; // small icon [self. iconView setImageWithURL: [NSURL URLWithString: food. imageUrl] placeholderImage: [UIImage imageNamed: @ "timeline_image_placeholder"]; // Dish name self. nameLabel. text = food. name; // subtitle self. descLabel. text = [NSString stringWithFormat: @ "difficulty: % @ Duration: % @", food. diff, food. time] ;}@ end
XIB