📜 ⬆️ ⬇️

Alternative UISplitViewController (display MasterView in the portrait orientation of the device)

As noted in the article “Pop-ups. Work with UIPopoverController ” in the iPhone SDK 3.2 new interface elements were added: UIPopoverController and UISplitViewController. About the first of them already quite extensively described in Habré. I want to talk about UISplitViewController, or rather about some alternative.

“Why is this alternative needed?”, You ask, “what is the reason?”. It’s all a matter of the behavior of SplitView, here's an excerpt from the SDK: “It’s a view of the landscape orientation”. Those. in portrait orientation, the right side (MasterView) is simply hidden (see fig. 1)


Figure 1 - SplitView in landscape (a) and portrait (b) orientation
')
Looking at the main settings of the iPad, we will see a completely different picture: in the portrait and landscape orientation, the left side (MasterView) is displayed the same way (it does not hide)


Figure 2 - Basic iPad Settings

Well, let's build our SplitView, where the left side would not hide with blackjack and whores, although to hell with SplitView and blackjack .

Start by creating a new project in Xcode (View-based Application). I gave it the name AlternativeSplit (see Figure 3).


Figure 3- Creating a new project

Next, add two new MasterViewController (subclass of UITableViewController) and DetailViewController (subclass of UIViewController) classes to the project by selecting File-> New file ... -> Cocoa Touch Class-> UIViewController subclass from the menu.

Open AlternativeSplitViewController.xib in InterfaceBuilder and “draw something like this”:


Figure 4 - Editing AlternativeSplitViewController.xib

Set up Autosizing so that our view doesn't “float” when switching from one orientation to another. For the left side of the Master (NavigationBar and TableView), we specify the same parameters as in Figure 5.a, and for the right Detail (NavigationBar and View), as in fig. 5 B


Figure 5 - Autosizing options

Add a new IBOutlet variable to the AlternativeSplitViewControlle.h class by editing the file
AlternativeSplitViewController.h as follows

// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  1. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  2. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  3. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  4. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  5. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  6. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  7. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  8. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  9. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
  10. // // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .


The MasterViewController class will be responsible for the table containing the menu items. Let's rewrite it as follows (briefly: we fill the table below in the code and when the user selects a table cell (didSelectRowAtIndexPath), we send a DetailView message):

  1. //
  2. // MasterViewController.h
  3. // AlternativeSplit
  4. //
  5. #import <UIKit / UIKit.h>
  6. @ class DetailViewController;
  7. @ interface MasterViewController: UITableViewController {
  8. IBOutlet DetailViewController * detailViewController;
  9. }
  10. @end
* This source code was highlighted with Source Code Highlighter .


  1. //
  2. // MasterViewController.m
  3. // AlternativeSplit
  4. //
  5. #import "MasterViewController.h"
  6. #import "DetailViewController.h"
  7. @implementation MasterViewController
  8. #pragma mark -
  9. #pragma mark View lifecycle
  10. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
  11. // Override to allow orientations.
  12. return YES;
  13. }
  14. #pragma mark -
  15. #pragma mark Table view data source
  16. - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView {
  17. // Return the number of sections.
  18. return 1;
  19. }
  20. - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section {
  21. // Return the number of rows in the section.
  22. return 5;
  23. }
  24. // Customize the appearance of the table view cells.
  25. - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
  26. static NSString * CellIdentifier = @ "Cell" ;
  27. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
  28. if (cell == nil) {
  29. cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
  30. }
  31. // Configure the cell ...
  32. cell.textLabel.text = [NSString stringWithFormat: @ "Row% i" , indexPath.row];
  33. return cell;
  34. }
  35. #pragma mark -
  36. #pragma mark Table view delegate
  37. - ( void ) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
  38. [detailViewController setNewText: [NSString stringWithFormat: @ "Row% i" , indexPath.row]];
  39. }
  40. #pragma mark -
  41. #pragma mark Memory management
  42. - ( void ) didReceiveMemoryWarning {
  43. // Releases the view if it doesn't have a superview.
  44. [super didReceiveMemoryWarning];
  45. // Relinquish ownership any cached data, images, etc that are not use.
  46. }
  47. - ( void ) dealloc {
  48. [super dealloc];
  49. }
  50. @end
* This source code was highlighted with Source Code Highlighter .


DetailViewController will be responsible for the right side. Edit the DetailViewController class (add a new IBOutlet variable UILabel * lbl and a new method setNewText, which will change the text of our lbl)

  1. //
  2. // DetailViewController.h
  3. // AlternativeSplit
  4. //
  5. #import <UIKit / UIKit.h>
  6. @ interface DetailViewController: UIViewController {
  7. IBOutlet UILabel * lbl;
  8. }
  9. - ( void ) setNewText: (NSString *) text;
  10. @end
* This source code was highlighted with Source Code Highlighter .


  1. //
  2. // DetailViewController.m
  3. // AlternativeSplit
  4. //
  5. #import "DetailViewController.h"
  6. @implementation DetailViewController
  7. - ( void ) setNewText: (NSString *) text {
  8. lbl.text = text;
  9. }
  10. - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
  11. // Overriden to allow any orientation.
  12. return YES;
  13. }
  14. - ( void ) didReceiveMemoryWarning {
  15. // Releases the view if it doesn't have a superview.
  16. [super didReceiveMemoryWarning];
  17. // Release any cached data, images, etc that are not in use.
  18. }
  19. - ( void ) viewDidUnload {
  20. [super viewDidUnload];
  21. // Release any retained subviews of the main view.
  22. // eg self.myOutlet = nil;
  23. }
  24. - ( void ) dealloc {
  25. [super dealloc];
  26. }
  27. @end
* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"> Source Code Highlighter .


All we have to do is bind our IBOutlet variables in interfaceBuildere (see Figure 6). And it's in the hat.


Figure 6 - Binding IBOutlet Variables

Result:


Figure 7 - Alternate SplitView

Source code can be downloaded here or here .

Thanks for attention.
PS There is a certain continuation of this article: “With a flick of the wrist, the UITabBar pants turn, the UITabBar pants turn… into elegant UISplitView shorts ” (a kind of crutch when porting an iPhone application to an iPad). Should I write?

Source: https://habr.com/ru/post/91366/


All Articles