IOS_21 group_controller inheritance Diagram
Finally:
Controller inheritance relationship diagram:
Note:
Click the button on the Dock on the left of the master controller,
For example, when you [Group Buying], [favorites], and [map,
The implemented functions have many similarities.
The details are as follows:
Click [Group Buying] to display group buying in the form of jiugongge,
When you click a Cell, the Group Buying details of the Cell are displayed.
Click [add to favorites] to display the archived group buying models in the form of jiugongge,
When you click a Cell, the Group Buying details of the Cell are displayed.
Click map to display the group buying model in the form of a pin on the MapView. The request is sent using the city name, longitude, latitude, and radius parameters)
Click a pin to display the Group Buying details of the Cell.
The main functions and methods of the above controllers are described as follows:
The extracted parent class is as follows:
Parent class ShowDealDetailController
Creates, displays, and hides the encapsulated group buying details controller]
Private member: Cover
//// ShowDealDetailController. h // handsome guy _ buy /// Created by beyond on 14-8-20. // Copyright (c) 2014 com. beyond. all rights reserved. // The base class, parent class, the Controller that displays the Order details controller. When you click XXX, You can inherit this controller when you need to display the Group Buying details, create and display order details controllers internally, and hide order details controllers # import
@ Class Deal; @ interface ShowDealDetailController: UIViewController // The data model (Data Source)-(void) showDetail :( Deal *) deal that the Controller that displays the Order details depends on; @ end
//// ShowDealDetailController. m // handsome _ buy /// Created by beyond on 14-8-20. // Copyright (c) 2014 com. beyond. all rights reserved. // The base class, parent class, the Controller that displays the Order details controller. When you click XXX, You can inherit this controller when you need to display the Group Buying details, the Order details controller is created and displayed internally, and the Order details controller # import "ShowDealDetailController is hidden. h "# import" Cover. h "// real details controller encapsulated by xib. when creating the controller, you must input the deal data source # import" DealDetailController. h "// self-encapsulated globally unified navigation controller # import" BeyondNavigationController. h "// truly use xib The height of the mounted detail controller is variable, but the width is usually fixed # define kDealDetailVCWidth 600 @ interface ShowDealDetailController () {// Cover * _ cover ;} @ end @ implementation ShowDealDetailController # pragma mark display details controller-(void) showDetail :( Deal *) deal {// 1. show cover if (_ cover = nil) {_ Cover = [cover coverWithTarget: self action: @ selector (hideDetail)];} // self is only a sub-(Root) controller, the navigation bar of the navigation controller is above self. navigationController obtains the view of the entire navigation controller (including the navigation bar and its Root (sub) controller )//???? The width and height of the view of the navigation controller (including the navigation bar )???? Because the self controller, each appearance, is first packaged by the navigation controller, because the top of the self is also the navigation bar _ cover. frame = self. navigationController. view. bounds; // transparent during creation, after which the animation becomes black _ cover. alpha = 0; [UIView animateWithDuration: kDefaultAnimDuration animations: ^ {[_ cover alphaReset] ;}]; // ??? Add to the top view of the navigation controller (cover the navigation bar and its root Controller) [self. navigationController. view addSubview: _ cover]; // 2. create and display the Group Buying details controller DealDetailController * detailVC = [[DealDetailController alloc] init]; // important ~~~ The left side of the navigation bar is disabled. After clicking it, call the method of the Controller to execute the animation to close the animation. The created DealDetailController cannot set this button in the DealDetailController, the reason is that it is not convenient to call the external method to hide the DealDetailController detailVC internally. navigationItem. leftBarButtonItem = [UIBarButtonItem itemWithIcon: @ "btn_nav_close.png" highlightedIcon: @ "break" target: self action: @ selector (hideDetail)]; // data source is required, for its internal child control generated from the xib to display the data detailVC. deal = deal; // use the navigation controller to package the real group buying details controller BeyondNavigati OnController * nav = [[BeyondNavigationController alloc] initWithRootViewController: detailVC]; // listen to the pan gesture and drag [nav. view addGestureRecognizer: [[UIPanGestureRecognizer alloc] initWithTarget. view. autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin ;//???? On the right side of the mask, it means that the animation is completely invisible, and then gradually moves to the left ??? // The real details controller in the xib Encapsulation has a variable height, but the width is generally fixed with the nav. view. frame = CGRectMake (_ cover. frame. size. width, 0, kDealDetailVCWidth, _ cover. frame. size. height); // when two controllers are in the parent-child relationship, their views are also in the parent-child relationship, because if only view is added, no controller is added, the Controller will be destroyed after this method is called, because it is a local variable [self. navigationController. view addSubview: nav. view]; [self. navigationController addChildViewController: nav]; // The animation moves slowly to the left and displays the details controller [UIView animateWithDuration: kDefaultAnimDuration animations: ^ {CGRect f = nav. view. frame; f. origin. x-= kDealDetailVCWidth; nav. view. frame = f ;}] ;}# pragma mark hide details controller-(void) hideDetail {// obtain the navigation controller that encapsulates the details controller, animation hides its view UIViewController * nav = [self. navigationController. childViewControllers lastObject]; [UIView animateWithDuration: 0.3 animations: ^ {// 1. hide cover _ cover. alpha = 0; // 2. hide controller CGRect f = nav. view. frame; f. origin. x + = kDealDetailVCWidth; nav. view. frame = f;} completion: ^ (BOOL finished) {[_ cover removeFromSuperview]; [nav. view removeFromSuperview]; [nav removeFromParentViewController];} # pragma mark-let the details controller have drawer effect, plus spring effect-(void) drag :( UIPanGestureRecognizer *) pan {// go left to negative CGFloat tx = [pan translationInView: pan. view]. x; // when the gesture ends, that is, if (pan. state = UIGestureRecognizerStateEnded) {CGFloat halfW = pan. view. frame. size. width * 0.5; if (tx> = halfW) {// more than half has been moved to the right [self hideDetail];} else {[UIView animateWithDuration: kDefaultAnimDuration animations: ^ {pan. view. transform = CGAffineTransformIdentity;}] ;}} else {// view if (tx <0) of the mobile controller {// drag tx * = 0.4 to the left;} pan. view. transform = CGAffineTransformMakeTranslation (tx, 0) ;}}@ end
Parent class BaseDealListController
Creates and maintains a CollectionView and requests the data source totalDealsArr from the subclass.
//// BaseDealListController. h // handsome guy _ buy /// Created by beyond on 14-8-21. // Copyright (c) 2014 com. beyond. all rights reserved. // The parent class located in the middle layer, inherited from ShowtDealDetailController, automatically displays the Group Buying details in an animation as long as XXX is clicked, and automatically has the click cover function, animation hides the Group Buying details controller function .... you are only responsible for creating and maintaining a CollectionView (jiugong Ge), and the data source (Group Buying object array) of jiugong Ge is provided by the subclass # import "ShowDealDetailController. h "@ interface BaseDealListController: ShowDealDetailController {UICollectionView * _ collectionView;} // the data source of the jiugong lattice (Group Buying object array) is provided by the subclass itself-(NSArray *) totalDeals; // All group buying data @ end
//// BaseDealListController. m // handsome _ buy /// Created by beyond on 14-8-21. // Copyright (c) 2014 com. beyond. all rights reserved. // The parent class located in the middle layer, inherited from ShowtDealDetailController, automatically displays the Group Buying details in an animation as long as XXX is clicked, and automatically has the click cover function, animation hides the Group Buying details controller function .... you are only responsible for creating and maintaining a CollectionView (jiugong Ge), and the data source (Group Buying object array) of jiugong Ge is provided by the subclass # import "BaseDealListController. h "# import" Deal. h "# import" DealCell. h "// width and height of each grid # define kItemW 250 # define kItemH 250 @ interface BaseDealListController ()
// Create and maintain a nine-cell grid by yourself. The data source (Group Buying object array) is provided by the subclass @ property (nonatomic, strong) UICollectionView * collectionView; @ end @ implementation BaseDealListController # pragma mark-lifecycle method-(void) viewDidLoad {[super viewDidLoad]; // 1. create your own collectionView [self addCollectionView]; // 2. register the xib file [self. collectionView registerNib: [UINib nibWithNibName: @ "DealCell" bundle: nil] forCellWithReuseIdentifier: @ "DealCell"]; // 3. set CollectionView always supports vertical scrolling and prepares (spring) self for pull-down refresh. collectionView. alwaysBounceVertical = YES; // 4. set the background color self of collectionView. collectionView. backgroundColor = kGlobalBg;} // 1. create your own collectionView-(void) addCollectionView {// create a stream layout. You must specify UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]; // set the width and height of each grid in the stream layout, that is, layout of each grid. itemSize = CGSizeMake (kItemW, kItemH); // layout. MinimumLineSpacing = 20; // create a collectionView for the specified stream layout and use the member variables to remember self. collectionView = [[UICollectionView alloc] initWithFrame: self. view. bounds collectionViewLayout: layout]; // auto scaling of height and width self. collectionView. autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; self. collectionView. delegate = self; self. collectionView. dataSource = self; [self. view addSubview: self. colle CtionView] ;}# pragma mark in viewWillAppear and viewDidAppear, you can obtain the most accurate width and height (width and height) of the view. // important ~~~ When the controller is created, the default width is 768 and the default height is 1024. no matter whether the screen is horizontal or vertical, the view can be obtained most accurately (that is, the actual view) only in the viewWillAppear and viewDidAppear methods) width and height (width and height)-(void) viewWillAppear :( BOOL) animated {// default calculation layout [self didRotateFromInterfaceOrientation: 0];} # pragma mark-parent class method // interval, called when the screen is about to rotate (Controller monitoring screen rotation)-(void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {// log (@ "the screen is about to rotate") ;}# call // intercept when The pragma mark screen is rotated and call it when the screen is rotated-(void) didRotateFromInterfaceOrientation :( UIInterfaceOrientation) fromInterfaceOrientation {// 1. retrieve the UICollectionViewFlowLayout * layout = (UICollectionViewFlowLayout *) self. collectionView. collectionViewLayout; // 2. calculation interval CGFloat v = 0; CGFloat h = 0; CGFloat height = self. view. frame. size. height-44; CGFloat width = self. view. frame. size. width; if (UIInterfaceOrientationIsLandscape (self. interfaceOrientation) {// horizontal screen spacing v = (height-2 * kItemH)/3; h = (width-3 * kItemW)/4 ;} else {// vertical screen spacing v = (height-3 * kItemH)/4; h = (width-2 * kItemW)/3;} // 3. adjust the distance between the grids in the animation [UIView animateWithDuration: 4.0 animations: ^ {// margin layout in the upper left and lower right directions. sectionInset = UIEdgeInsetsMake (h, h, v, h); // layout. minimumLineSpacing = h ;}] ;}# pragma mark-collectionView proxy method // The total number of items (I .e., the lattice Cube), and The subclass-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return self. totalDeals. count ;}# reloadData will be called when pragma mark refreshes data # pragma mark calls every time a cell re-enters the screen field of view, it will generate a unique grid, subclass-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {static NSString * ID = @ "DealCell"; DealCell * cell = [collectionView preview: ID forIndexPath: indexPath]; cell. deal = self. totalDeals [indexPath. row]; return cell;} // when you click a grid, showDealDetailVC-(void) collectionView :( UICollectionView *) collectionView didSelectItemAtIndexPath :( NSIndexPath *) indexPath {// call the method of the parent class again to display the dealDetail controller [self showDetail: self. totalDeals [indexPath. row] ;}# pragma mark-life cycle method-(void) dealloc {[[nsicationicationcenter defacenter center] removeObserver: self] ;}@ end
Subclass DealListController
You only need to inherit and provide the data source totalDealsArr to the parent class.
//// DealListController. h // handsome guy _ buy /// Created by beyond on 14-8-14. // Copyright (c) 2014 com. beyond. all rights reserved. // click the Controller corresponding to the Group Buying button on the dock. The navigation bar is displayed on the top, the searchBar is displayed on the right, and a large button (TopMenu) is displayed on the left of the navigation bar (which consists of three small buttons ).
) // This controller is inherited from BaseDealListController and automatically owns the nine squares. You only need to provide the data for it, while BaseDealListController inherits from ShowDealDetailVc and has the function of displaying idle feelings # import "BaseDealListController. h "@ interface DealListController: BaseDealListController @ end
//// DealListController. m // handsome _ buy /// Created by beyond on 14-8-14. // Copyright (c) 2014 com. beyond. all rights reserved. // click the Controller corresponding to the Group Buying button on the dock. The navigation bar is displayed on the top, the searchBar is displayed on the right, and a large button (TopMenu) is displayed on the left of the navigation bar (which consists of three small buttons ).
) # Import "DealListController. h "// There is a big button (top menu) # import" TopMenu on the left of the navigation bar. h "// encapsulated custom cell # import" DealCell. h "// reviews the class provided to encapsulate and send requests # import" DPAPI. h "// tool class # import" MetaDataTool ool. h "// encapsulate the requested tool class # import" DealRequestTool. h "// model class # import" City. h "# import" Deal. h "// secondary encapsulated image download tool class # import" ImgDownloadTool. h "# define kItemW 250 # define kItemH 250 @ interface DealListController ()
{// The dictionary array returned by the receiving server ---- the array of converted objects for the grid to display NSMutableArray * _ deals; // refresh MJRefreshHeaderView * _ header from the drop-down menu; // pull up to load the next page MJRefreshFooterView * _ footer; // Number of pages loaded each time int _ pageNo;} @ end @ implementation DealListController // inherits the BaseDealListController controller, which must be implemented, the purpose is to provide the data source-(NSArray *) totalDeals {return _ deals;}-(void) viewDidLoad {[super viewDidLoad]; _ deals = [NSMutableArray array] For collectionView; // 1. basic settings in the top navigation bar [s Elf setNavigationBar]; // 2. add the refresh control [self addRefresher]; // 0. listening to the notification of city changes, click the drop-down list to refresh kAddAllNotes (dataChange)} // 0. listening to notifications of city changes, click the drop-down refresh-(void) dataChange {[_ header beginRefreshing];} // 1. basic settings in the top navigation bar-(void) setNavigationBar {// 1. UISearchBar * s = [[UISearchBar alloc] init]; s. frame = CGRectMake (0, 0,210, 35); s. placeholder = @ "Enter product name, address, etc."; self. navigationItem. rightBarButtonItem = [UIBarButtonItem al Loc] initWithCustomView: s]; // 2. topMenu * top = [[TopMenu alloc] init]; // important: After you click the item in TopMenu, where will the created PopMenu be added ??? Is the view top of the controller. controllerView = self. view; self. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: top];} // 3. add refresh control-(void) addRefresher {_ header = [MJRefreshHeaderView header]; _ header. scrollView = _ collectionView; _ header. delegate = self; _ footer = [MJRefreshFooterView footer]; _ footer. scrollView = _ collectionView; _ footer. delegate = self ;}# proxy method of The pragma mark refrecontrol-(void) refreshViewBeginRefreshing :( MJRefreshBaseView *) refreshView {// mark it, whether to pull down or pull up BOOL isHeader = [refreshView isKindOfClass: [MJRefreshHeaderView class]; if (isHeader) {// pull down to refresh // we recommend that you first, clear the previously downloaded image memory cache [ImgDownloadTool clear]; // each pull-down operation loads the first page _ pageNo = 1;} else {// pull up to load more, load the next page _ pageNo ++;} // load the data on the page _ pageNo [[DealRequestTool sharedDealRequestTool] partition: _ pageNo success: ^ (NSArray * deals, int total_count) {if (isHeader) {// if the first page of data is loaded from the drop-down list, remove the old [_ deals removeAllObjects];} // 1. add a new returned object array [_ deals addObjectsFromArray: deals]; // 2. refresh the table [_ collectionView reloadData]; // 3. restore refresh status [refreshView endRefreshing]; // 4. based on the total number of items, you can easily determine whether to hide the pull-up control _ footer. hidden = _ deals. count> = total_count;} fail: ^ (NSError * error) {log (@ "request failed -- % @", error ); // you must also hide [refreshView endRefreshing] ;}] ;}@ end