Last active
December 15, 2015 03:39
-
-
Save lamprosg/5195761 to your computer and use it in GitHub Desktop.
(iOS) SplitViews and PopOver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Split Views consist of 2 views (view controllers). A mater view, which usually has a list if options to press) | |
| //and a detailed view which usually displays information based on what the user selected in the master view | |
| //When the iPad is in portrait mode the Split View Controller hides the master panel | |
| //so that the detail panel is able to utilize the entire screen. | |
| //In this case the master view appears as a popover | |
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
| { | |
| self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
| // Override point for customization after application launch. | |
| //Load the master view controller with its nib file | |
| splitViewMasterViewController *masterViewController = [[splitViewMasterViewController alloc] | |
| initWithNibName:@"splitViewMasterViewController" | |
| bundle:nil]; | |
| //Ad a navigation controller to the master view controller | |
| UINavigationController *masterNavigationController = [[UINavigationController alloc] | |
| initWithRootViewController:masterViewController]; | |
| //Load the details view controller with its nib | |
| splitViewDetailViewController *detailViewController = [[splitViewDetailViewController alloc] | |
| initWithNibName:@"splitViewDetailViewController" | |
| bundle:nil]; | |
| //Ad a navigation bar here as well | |
| UINavigationController *detailNavigationController = [[UINavigationController alloc] | |
| initWithRootViewController:detailViewController]; | |
| //Set the master view's deitail view controller as the one we created for this purpose | |
| masterViewController.detailViewController = detailViewController; | |
| //Create the split view controller with the previous allocated views | |
| self.splitViewController = [[UISplitViewController alloc] init]; | |
| //Declare the detail view controller as the delegate of the split view controller | |
| self.splitViewController.delegate = detailViewController; | |
| self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, | |
| detailNavigationController, nil]; | |
| //Set the split view controller as the root view controller | |
| self.window.rootViewController = self.splitViewController; | |
| [self.window makeKeyAndVisible]; | |
| /*****************************************/ | |
| //If you made your split view controller with storyboard | |
| //Type this: | |
| UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; | |
| UINavigationController *detailNavigationController = (UINavigationController *)[splitViewController.viewControllers objectAtIndex:1]; | |
| splitViewController.delegate = (id)detailNavigationController.topViewController; | |
| /*****************************************/ | |
| return YES; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Master view will be a table view (subclass of tableviewcontroller) | |
| //Here we will isplay a list of websites | |
| #import <UIKit/UIKit.h> | |
| //Create a protocol with a delegate to be called when pressing a row | |
| @protocol WebSiteDelegate <NSObject> | |
| -(void) didSelectPage:(NSString*) urlString; | |
| @end | |
| @interface splitViewMasterViewController : UITableViewController | |
| @property (nonatomic, retain) NSArray *siteNames; | |
| @property (nonatomic, retain) NSArray *siteAddresses; | |
| @property (nonatomic,retain) id<WebSiteDelegate> delegate; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "splitViewMasterViewController.h" | |
| @implementation splitViewMasterViewController | |
| - (id)initWithNibName:(NSString *)nibNameOrNil | |
| bundle:(NSBundle *)nibBundleOrNil | |
| { | |
| self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
| if (self) { | |
| self.title = NSLocalizedString(@"Favorite Web Sites", @"Favorite Web Sites"); | |
| self.clearsSelectionOnViewWillAppear = NO; | |
| self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); | |
| } | |
| return self; | |
| } | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| // Do any additional setup after loading the view, typically from a nib. | |
| siteNames = [[NSArray alloc] initWithObjects:@"Yahoo", @"Google",@"Apple", @"eBookFrenzy", nil]; | |
| siteAddresses = [[NSArray alloc] initWithObjects:@"http://www.yahoo.com", | |
| @"http:/www.google.com", @"http://www.apple.com", | |
| @"http://www.ebookfrenzy.com", nil]; | |
| [self.tableView selectRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection:0] | |
| animated:NO | |
| scrollPosition:UITableViewScrollPositionMiddle]; | |
| } | |
| - (void)viewDidUnload | |
| { | |
| // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. | |
| self.siteNames = nil; | |
| self.siteAddresses = nil; | |
| } | |
| #pragma mark - TableView Delegate | |
| - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
| { | |
| return 1; | |
| } | |
| - (NSInteger)tableView:(UITableView *)tableView | |
| numberOfRowsInSection:(NSInteger)section | |
| { | |
| return [siteNames count]; | |
| } | |
| - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| static NSString *CellIdentifier = @"Cell"; | |
| UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
| if (cell == nil) { | |
| cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
| } | |
| // [self configureCell:cell atIndexPath:indexPath]; | |
| cell.textLabel.text = [siteNames objectAtIndex:indexPath.row]; | |
| return cell; | |
| } | |
| //Selecting a row in the master view controller | |
| - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| NSString *urlString = [siteAddresses objectAtIndex:indexPath.row]; | |
| if ( indexPath.row ==0 ) | |
| [self.delegate didSelectPage:urlString]; //Do that in the detailviewcontrolloer | |
| /*************************************************/ | |
| //In case you want to load a different controller | |
| if (indexPath.row == 1) | |
| { | |
| //In storyboard get the viewcontroller | |
| AnotherDetailedViewController *newDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"serviceOrderViewController"]; | |
| //Otherwise just initialize it | |
| [[[self detailViewController] navigationController] popViewControllerAnimated:NO]; | |
| [[[self detailViewController] navigationController] pushViewController:newDetailViewController animated:YES]; | |
| } | |
| /*************************************************/ | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| //Must conform the split view controller delegate | |
| @interface splitViewDetailViewController : UIViewController <UISplitViewControllerDelegate, WebSiteDelegate> | |
| @property (strong, nonatomic) IBOutlet UIWebView *webView; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "splitViewDetailViewController.h" | |
| #import "splitViewMasterViewController.h" | |
| @interface splitViewDetailViewController () | |
| @property (strong, nonatomic) UIPopoverController *masterPopoverController; | |
| @end | |
| @implementation splitViewDetailViewController | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| //If needed | |
| //Set masterview delegate pointing to this class | |
| UINavigationController *masterNavigationController = [self.splitViewController.viewControllers objectAtIndex:0]; | |
| splitViewMasterViewController *mastervc = (splitViewMasterViewController*) masterNavigationController.topViewController; | |
| mastervc.delegate=self; | |
| } | |
| //Popover implementation | |
| //UISplitViewControllerDelegate delegates, called when the device rotates to portrait orientation | |
| - (void)splitViewController:(UISplitViewController *)splitController | |
| willHideViewController:(UIViewController *)viewController | |
| withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController | |
| { | |
| barButtonItem.title = NSLocalizedString((@"Favorite Web Sites", @"Favorite Web Sites"); | |
| [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES]; | |
| self.masterPopoverController = popoverController; | |
| } | |
| //removes the button from the toolbar and de-assigns the popoverController reference | |
| - (void)splitViewController:(UISplitViewController *)splitController | |
| willShowViewController:(UIViewController *)viewController | |
| invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem | |
| { | |
| // Called when the view is shown again in the split view, invalidating the button and popover controller. | |
| [self.navigationItem setLeftBarButtonItem:nil animated:YES]; | |
| self.masterPopoverController = nil; | |
| } | |
| //What will happen in or detailedView | |
| //Our custom WebSite delegate | |
| -(void) didSelectPage: (NSString*) urlString | |
| { | |
| NSURL *url = [NSURL URLWithString:urlString]; | |
| NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
| splitViewDetailViewController *detailViewController = self.detailViewController; | |
| detailViewController.webView.scalesPageToFit = YES; | |
| //Here te URL opens in the detailViewController | |
| [self.webView loadRequest:request]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment