How does one ensure that the viewWillAppear: and viewDidAppear: (and other view transition methods) are called on a UIPageViewController's child ViewControllers?
In my case, the view transition methods are adequately called on all except for the very firstViewController that the UIPageViewController opens to. On that firstViewController, the viewDidAppear: and viewWillAppear: methods are actually called BEFORE the UIPageViewController's view transition methods. And then as one starts scrolling, the viewWillAppear: and viewDidAppear: methods are called as one would expect for the new view controllers.
Here is my structure: I have a BossViewController that contains an OrganizerViewController (a couple different organizers, actually, but that is irrelevant). The OrganizerViewController contains a UIPageViewController, which contains a series of CustomViewControllers. My code adequately calls the child view transition methods as per apple (https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html) for the interactions between the BossViewController and the OrganizerViewController (fully functional, perfectly so). The problem lies somewhere between the OrganizerViewController, it's child UIPageViewController, and it's first child CustomPageViewController.
I have tried calling beginAppearanceTransition: and endAppearanceTransition: within the UIPageViewController's viewWillAppear: and viewDidAppear: - but that resulted in an 'unbalanced calls' error log to console.
Here is the UIPageViewController set up code, all of which is in the OrganizerViewController:
- (void)configurePVC
{
NSDictionary *key = #{UIPageViewControllerOptionInterPageSpacingKey : #([UIScreen mainScreen].bounds.size.width * 0.5)};
_pvc = [[AVSCustomPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:key];
self.pvc.delegate = self;
self.pvc.dataSource = self;
_pvcArray = [[NSMutableArray alloc] init];
CustomViewController *daily = (CustomViewController *)[self viewControllerAtIndex:6];
self.index = 6;
[self.pvcArray addObject:daily];
[self.pvc setViewControllers:self.pvcArray
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
[self addChildViewController:self.pvc];
[self.dailyContainerView addSubview:self.pvc.view];
self.pvc.view.frame = self.dailyContainerView.bounds;
[self.pvc didMoveToParentViewController:self];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [(CustomViewController *)viewController index];
if (index == 0) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [(CustomViewController *)viewController index];
if (index == 6) {
return nil;
}
index++;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
CustomViewController *viewController = [[CustomViewController alloc] init];
viewController.index = index;
return viewController;
}
My App's structure likes this:
UISplitViewController:
the master:NavigationController1->UITableViewController
the detail:NavigationController2->UIWebViewController
I want to show the barButtonItem when the view goes on portrait mode on iPad
and I know how to realize it in iOS7 by willHideViewController:
-(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc{
barButtonItem.title = #"Course";
self.navigationItem.leftBarButtonItem = barButtonItem;
}
-(void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{
if (barButtonItem == self.navigationItem.leftBarButtonItem) {
self.navigationItem.leftBarButtonItem = nil;
}
}
However, this method is deprecated in iOS 8, and i tried to use:
-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{
if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
self.navigationItem.leftBarButtonItem = svc.displayModeButtonItem;
}else{
self.navigationItem.leftBarButtonItem = nil;
}
}
This method only works when the display mode changes but not when the app first starts with a portrait orientation.
So how to show barButtonItem when loading the app first time with a portrait orientation.
You can add the bar button when your view controller shows up:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden)
{
UIBarButtonItem *barButtonItem = self.splitViewController.displayModeButtonItem;
barButtonItem.title = #"Show master";
self.navigationItem.leftBarButtonItem = barButtonItem;
}
}
This will only add the button when the master is currently hidden.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden){
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Course" style:self.splitViewController.displayModeButtonItem.style target:self.splitViewController.displayModeButtonItem.target action:self.splitViewController.displayModeButtonItem.action];
}
}
-(void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode{
if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Course" style:svc.displayModeButtonItem.style target:svc.displayModeButtonItem.target action:svc.displayModeButtonItem.action];
}else{
self.navigationItem.leftBarButtonItem = nil;
}
}
I want to have a persistent button in the bottom right corner of my app. During all view transitions, the button should remain static. I'm having trouble deciding what view to add the button to. I know the button ought to be stored in the AppDelegate, but I don't know what other view it would be sense to add it to except the window. One downside of adding it to the window is that when there's an app running in the background (ie Phone), the added status bar padding will push down the window. In general, adding it to the window seems to be a hacky solution -- any thoughts?
Yes, adding it to the UIWindow would be extremely hacky and finicky.
Storyboards
If you're using Storyboards and iOS 5.0 onwards, you should be able to use container views and do something like this:
Here's another picture showing the, rather simplistic, structure of the first View Controller:
The view controller on the left has a container, and then a view which holds the button on top of it. The container indicates that the navigation controller (directly to the right) should appear within itself, that relationship is shown by the =([])=> arrow (formally known as an embed segue). Finally the navigation controller defines its root view controller to the one on the right.
In summary, the first view controller pancakes-in the container view with the button on top, so everything that happens inside has to have the button on top.
Using childViewControllers
aka. The "I hate Storyboards and puppies" mode
Using a similar structure to the Storyboard version, you could create the base view controller with its button, and then, add the view that will become then new "root" of the application, underneath.
To make it clear, let's call the one view controller that holds the button the FakeRootViewController, and the view controller that will be, for all practical purposes, the root of the application: RootViewController. All subsequent view controllers won't even know that there's the FakeRootViewController above everyone else.
FakeRootViewController.m
// The "real" root
#import "RootViewController.h"
// Call once after the view has been set up (either through nib or coded).
- (void)setupRootViewController
{
// Instantiate what will become the new root
RootViewController *root = [[RootViewController alloc] <#initWith...#>];
// Create the Navigation Controller
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
// Add its view beneath all ours (including the button we made)
[self addChildViewController:nav];
[self.view insertSubview:nav.view atIndex:0];
[nav didMoveToParentViewController:self];
}
AppDelegate.m
#import "FakeRootViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FakeRootViewController *fakeRoot = [[FakeRootViewController alloc] <#initWith...#>];
self.window.rootViewController = fakeRoot;
[self.window makeKeyAndVisible];
return YES;
}
That way, you can have all the benefits of inserting the button on the window, without all the guilt and "Should I really be a programmer?" that it causes.
Potentially you could have 1 main "root" view controller, and all you other view controllers could be child view controllers, with their views as child views. Then they would have their content, and the button would be in the "root" view controller. But this seems just as sketchy and hacky as putting it in the window, and probably less convenient.
I use this button:
#interface UIPopUpButton : UIImageView <UIPopoverControllerDelegate, UIActionSheetDelegate>
{
UIPopoverController* popoverController;
Class popoverClass;
}
- (id) initWithPoint: (CGPoint) point;
- (void) touchesBegan: (NSSet*) touches
withEvent: (UIEvent*) event;
+ (id) buttonAtPoint: (CGPoint) point;
+ (id) buttonAtOriginalPoint;
+ (void) unhighlight;
+ (void) bringButtonToFront;
#property (nonatomic, retain) UIPopoverController* popoverController;
#property (nonatomic, assign) Class popoverClass;
#end
#import "UIPopUpButton.h"
#implementation UIPopUpButton
static UIPopUpButton* button = nil;
static CGPoint originalPoint;
#synthesize popoverClass;
#synthesize popoverController;
+ (id) buttonAtPoint: (CGPoint) point
{
if (button == nil)
{
button = [[UIPopUpButton alloc] initWithPoint: point];
originalPoint = point;
button.popoverClass = [UIPopoverController class];
}
else
{
button.frame = CGRectMake(point.x, point.y, button.frame.size.width, button.frame.size.height);
}
return button;
}
+ (id) buttonAtOriginalPoint
{
return [self buttonAtPoint: originalPoint];
}
+ (void) unhighlight
{
button.highlighted = NO;
}
+ (void) bringButtonToFront
{
[[UIApplication sharedApplication].keyWindow addSubview: [self buttonAtOriginalPoint]];
}
- (id) initWithPoint: (CGPoint) point
{
UIImage* image1 = [UIImage imageNamed: #"topbutton.png"];
UIImage* image2 = [UIImage imageNamed: #"topbutton.png"];
if ((self = [super initWithImage: image1
highlightedImage: image2]))
{
self.userInteractionEnabled = YES;
self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
self.multipleTouchEnabled = NO;
}
return self;
}
- (BOOL) isAppCurrStatus
{
return ([DevToolsClientController sharedInstance].statusOfRootViewController == FrontEndApplication);
}
- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
UITouch* touch = [touches anyObject];
if(touch.view == self)
{
if (self.popoverController == nil)
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle: #"Please choice operation:"
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: nil];
[actionSheet addButtonWithTitle: #"Cancel"];
actionSheet.cancelButtonIndex = 0;
[actionSheet addButtonWithTitle: #"Button 1"];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet setTag: 0];
[actionSheet setDelegate: self];
[actionSheet showInView: [self superview]];
[actionSheet release];
[actions release];
}
else
{
PopoverMenuController* contentViewController = [[PopoverMenuController alloc] init];
self.popoverController = [[UIPopoverController alloc] initWithContentViewController: contentViewController];
popoverController.delegate = self;
[popoverController presentPopoverFromRect: CGRectMake(10.0f, 10.0f, 5.0f, 5.0f)
inView: self
permittedArrowDirections: UIPopoverArrowDirectionAny
animated: YES];
contentViewController.popoverController = self.popoverController;
[contentViewController reloadData];
}
}
else
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
[super touchesBegan: touches withEvent: event];
}
#pragma mark UIActionSheetDelegate implementation
-(void) actionSheet: (UIActionSheet*) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex
{
NSNumber* indexAction = [[NSNumber alloc] initWithInt: buttonIndex - 1];
}
- (void) runAction: (NSNumber*) indexAction
{
[DevToolsPopoverMenuController runAction: [indexAction integerValue]];
}
#pragma mark -
#pragma mark UIPopoverControllerDelegate implementation
- (void) popoverControllerDidDismissPopover: (UIPopoverController*) thePopoverController
{
if (self.popoverController != nil)
{
self.popoverController = nil;
}
}
- (BOOL) popoverControllerShouldDismissPopover: (UIPopoverController*) thePopoverController
{
//The popover is automatically dismissed if you click outside it, unless you return NO here
return YES;
}
#end
call:
[UIPopUpButton bringButtonToFront];
My button is always on top.
Try subclassing the UIViewController class and make your own one with the button
Create a singleton object that holds the button so all view controllers can reference it and add it to their subview or add it to the window directly.
SomeClass.h
#property (nonatomic) UIButton *yourButton;
+(SomeClass*)sharedSomeClass;
SomeClass.m
#synthesize yourButton = _yourButton;
-(id)init
{
self = [super init];
if(self)
{
_yourButton = [UIButton new];
//Other settings you want for your button
}
return self;
}
+(SomeClass)sharedSomeClass
{
static SomeClass *sharedSomeClass;
if (!sharedSomeClass)
sharedSomeClass = [[super allocWithZone:nil]init];
return sharedSomeClass;
}
+(void)allocWithZone:(NSZone*)zone
{
return [self sharedSomeClass];
}
If you like you can access the window directly like this:
UIWindow *mainwindow = [[[UIApplication sharedApplication]delegate]window];
import SomeClass.h into your view controllers, and access the button from anywhere
#import "SomeClass.h"
SomeClass *someClass = [SomeClass sharedSomeclass];
UIButton *localButton = someClass.yourButton;
I have a UIViewController on an iPad that I am displaying modally.
However after dismissing it, the view controller that displayed it does not change in orientation and acts as if the underlying view controller did not change, unless the device is rotated back and forth.
I'm using these methods in the modal view controller:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
[self updateUI];
return YES;
}
- (BOOL)shouldAutorotate {
[self updateUI];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
-(void) updateUI{
}
The underlying UINavigationController code (the one that doesn't change itself properly once the modal view controller is dismissed):
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
[self adjustUIForOrientation:interfaceOrientation];
return YES;
}
- (BOOL)shouldAutorotate {
[self adjustUIForOrientation:[self preferredInterfaceOrientationForPresentation]];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
- (void) adjustUIForOrientation: (UIInterfaceOrientation)interfaceOrientation {
if(UIDeviceOrientationIsPortrait(interfaceOrientation)) {
[_landscapeTemplate hide];
[_portraitTemplate show];
_activeTemplate = _portraitTemplate;
} else {
[_portraitTemplate hide];
[_landscapeTemplate show];
_activeTemplate = _landscapeTemplate;
}
}
-(IBAction) signInButtonTouched: (id) sender{
if (![Session shared].loggedInUser){
UserProfileLoginViewController *userProfileLoginVC = [[[UserProfileLoginViewController alloc] initWithNibName:#"UserProfileLoginViewController" bundle:nil] autorelease];
userProfileLoginVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
//self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:userProfileLoginVC animated:YES completion:nil];
// [self pushViewController:userProfileLoginVC animated:YES];
[userProfileLoginVC.emailTextField becomeFirstResponder];
}
else [[Session shared] explicitEnd];
}
Is there a way to make this work? Basically it appears that the view controller that presented the modal view controller is not getting the rotation calls forwarded to itself.
I'm previewing a file using UIDocumentInteractionController, but it is occupying all the area of the iPad. I would like it to appear on the detail view of the split view controller. I've checked that the UIView of my view controller doesn't occupy all the screen. Any ideas? I checked that the documentInteractionControllerRectForPreview: is called.
-(void)requestExecutedWithSuccess:(Model *)model {
UIDocumentInteractionController *docInteractionCont = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:model.filePath]];
self.docInteractionController = docInteractionCont;
self.docInteractionController.delegate = self;
BOOL attachmentShown = [self.docInteractionController presentPreviewAnimated:YES];
...
}
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return [self navigationController];
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}