UIPageViewController in iOS6 - iphone

In iOS6 in the methods viewControllerAfterViewController and viewControllerBeforeViewController if I return nil (for block the page navigation when I am in the first or last page) the app crash with this exception:
'The number of view controllers provided (0) doesn't match the number required (1) for the requested transition'
In iOS5 all works good.

I had the same issue. I found that the cause was replacing the delegate on the UIPanGestureRecognizer of the UIPageViewController, a no-no really. The pan gesture recognizer was calling an undocumented method _gestureRecognizerShouldBegin: (note the leading underscore) that UIPageViewController implements and apparently relies upon to work properly (read: not-crash). I ended up implementing respondsToSelector: and forwardingTargetForSelector: in my class that uses the UIPageViewController to pass the undocumented delegate method on to the UIPageViewController without specifically naming it (and almost certainly earning me an app store review rejection).
-(BOOL)respondsToSelector:(SEL)aSelector {
if ([super respondsToSelector:aSelector])
return YES;
else if ([self.pageViewController respondsToSelector:aSelector])
return YES;
else
return NO;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
if ([super respondsToSelector:aSelector]) {
return nil;
} else if ([self.pageViewController respondsToSelector:aSelector]) {
return self.pageViewController;
}
return nil;
}
My longer term solution will be to rework the use of UIPageViewController such that I don't need to displace the gesture recognizer delegates.

Ah,was wondering why no one has pointed out this bug,which i took almost 2 nights to find out the solution.
OLD CODE(iOS 5.1) : when returning nil on the first and last page you will experience the app crash.It works fine in iOS 5.1,but in iOS 6 it wont.
- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerBeforeViewController:
(UIViewController *)viewController
{
for (UIGestureRecognizer *recognizer in pageController.gestureRecognizers) {
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
recognizer.enabled = NO;
}
}
NSUInteger index = [self indexOfViewController:
(MainViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
for (UIGestureRecognizer *recognizer in pageController.gestureRecognizers) {
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
recognizer.enabled = NO;
}
}
NSUInteger index = [self indexOfViewController:
(MainViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
}
SOLUTION(iOS 6) : After adding the gesture effect to the superview,just call the delegate called -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer. What i did is quiet simple, computing the speed of the user flipping the first page and last page (i mean using the gesture recognizer) , i denied the swiping.All you need to do is just paste the following code,and you are DONE!.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (pageNum==0) {
if ([(UIPanGestureRecognizer*)gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &&
[(UIPanGestureRecognizer*)gestureRecognizer velocityInView:gestureRecognizer.view].x > 0.0f) {
//NSLog(#"Swiping to left on 1st page is denied");
return NO;
}
if ([(UITapGestureRecognizer*)gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
[(UITapGestureRecognizer*)gestureRecognizer locationInView:gestureRecognizer.view].x < self.view.frame.size.width/2) {
//NSLog(#"tapping to left on 1st page is denied");
return NO;
}
}
else if(pageNum ==totalNoOfFiles-1)
{
if ([(UIPanGestureRecognizer*)gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &&
[(UIPanGestureRecognizer*)gestureRecognizer velocityInView:gestureRecognizer.view].x < 0.0f) {
//NSLog(#"Swiping to right on 1st page is denied");
return NO;
}
if ([(UITapGestureRecognizer*)gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
[(UITapGestureRecognizer*)gestureRecognizer locationInView:gestureRecognizer.view].x > self.view.frame.size.width/2) {
//NSLog(#"Tapping to right on 1st page is denied");
return NO;
}
}
return YES;
}
- (UIViewController *)pageViewController:(UIPageViewController*) pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
int index = [self indexOfViewController:(ChildViewController *)viewController];
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
int index = [self indexOfViewController:(ChildViewController *)viewController];
index++;
return [self viewControllerAtIndex:index];
}

This has been well discussed but I have one thing to add. Consider why you were setting the delegate of the gesture recognizers to self. In my case, it was because in some cases I wanted to prevent the gesture recognizers from recognizing, with the delegate's gestureRecognizerShouldBegin:.
But in iOS 6, where this issue arises, there is a whole new way of doing exactly that, by implementing gestureRecognizerShouldBegin: on a UIView. (This is a new UIView instance method in iOS 6.)
Thus I was able to accomplish exactly what I was accomplishing before, without altering the gesture recognizers' delegate.

I had the issue with UIPageViewController crashing with iOS6 with the same error ('The number of view controllers provided (0) doesn't match the number required (1) for the requested transition').
None of the above solutions worked for me but I eventually found that moving the following line from viewDidLoad to viewDidAppear fixed it.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

Totally same issue here.
What I did was a hotfix which just to returns clone of before/afterViewController instead of nil, i.e.
// viewController = before/afterViewController
NSUInteger index = [self indexOfViewController:viewController];
// NOTE: return nil crashes in iOS6
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
This means you can page-curl forever but I had no other choice...
Better solution is always welcome.

Related

Be notified when a UITableViewCell swipe delete is cancelled in iOS 7

I'm using willTransitionToState which notifies me when the right hand delete button is shown. However, this method is not called when the delete is cancelled by tapping outside the cell area. I've also tried tableView:didEndEditingRowAtIndexPath.
The answers found in this question don't work in iOS 7.
The following code works for iOS 7 (not for iOS 6). The iOS 6 solution is this.
- (void)layoutSubviews
{
[super layoutSubviews];
[self detectDeleteButtonState];
// it takes some time for delete button to disappear
[self performSelector:#selector(detectDeleteButtonState) withObject:self afterDelay:1.0];
}
- (void)detectDeleteButtonState
{
BOOL isDeleteButtonPresent = [self isDeleteButtonPresent:self.subviews];
if (isDeleteButtonPresent) {
NSLog(#"delete button is shown");
} else {
NSLog(#"delete button is gone");
}
}
-(BOOL)isDeleteButtonPresent:(NSArray*)subviews
{
for (UIView *subview in subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"])
{
return [subview isHidden] == NO;
}
if([subview.subviews count] > 0){
return [self isDeleteButtonPresent:subview.subviews];
}
}
return NO;
}

RTL languages uipageviewcontroller animation

I know this question is typique and it was asked many times in the forum, but I still cannot solve my problem, so please if any body can help be that would be GREAT :)
I am creating a book application in the arabic languages and I need to perform the transitions of the uipageviewcontroller from right to left. And thats all I have to say.
One more thing (if I hadn't explain very well my self) I have the exact need as this thread: How to change UIPageViewController direction of paging curl animation but I couldn't manage to make the solution they spoke about, so if someone can explain me or give me a link where I can have how to do it that would be more than enough :)
It can be done in this way
Swap the code of pageViewController's datasource from viewControllerBeforeViewController to viewControllerAfterViewController
Change UIPageViewControllerSpineLocationMin to UIPageViewControllerSpineLocationMax
To check that, start Page-Based Application template as Universal and change the following in ModelController.m
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
and change UIPageViewControllerSpineLocationMin to UIPageViewControllerSpineLocationMax and swipe the condition of (indexOfCurrentViewController % 2 == 0) in "RootViewController.m"
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (UIInterfaceOrientationIsPortrait(orientation) || ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)) {
UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
self.pageViewController.doubleSided = NO;
return UIPageViewControllerSpineLocationMax;
}
DataViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSArray *viewControllers = nil;
NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];
if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) {
UIViewController *previousViewController = [self.modelController pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
} else {
UIViewController *nextViewController = [self.modelController pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
}
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
return UIPageViewControllerSpineLocationMid;
}
Source: PageViewControllers Apple Doc
You should dive into core graphics and core animation.
Make 2 layers (a previous/next and current)
When doing a 'pangesture' (see the Event Handling Guide ) you need to see if it's a swipe left or swipe right (previous/next page)
Then perform a 3D Rotation on the layer(s)
here's a nice example about flipping pages
Madev, Do you not want to use the "PageBased Application" template?
Simply start a new xCode Project:
Under iOS > Application , look for Page-Based Application. Hit Okay, Choose you Options. All you have to do is supply your content (via the "DataSource").
Now that is the complicated part...But here's some tips
http://www.techotopia.com/index.php/Implementing_a_Page_based_iOS_5_iPhone_Application_using_UIPageViewController
http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application
You can set the semanticContentAttribute on your UIPageViewController's viewDidLoad like:
if <You app language is Arabic/RTL based> {
self.view.semanticContentAttribute = .forceRightToLeft
}else {
self.view.semanticContentAttribute = .forceLeftToRight
}
Keep coding......... :)

Displaying Game Center modal view in landscape

I am using cocos2d engine in landscape-only orientation with no autorotation.
I want to display standart GC achievements modal view.
It's showing up normally, from bottom of screen (while holding device in landscape), but it is dismissing to the right side of the screen (like portrait modal views). It's seems to be changing orientation for dismiss animation, but view is not rotating before this. It's just sliding to the right.
Also I get a warning in console:
Unbalanced calls to begin/end appearance transitions for <UIViewController: 0x41b9a0>.
That's how I showing this controller:
-(void)showAchievements:(id) sender {
utilityController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[[[CCDirector sharedDirector] openGLView] addSubview:utilityController.view];
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
[utilityController presentModalViewController: achievements animated: YES];
}
[achievements release];
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
[utilityController dismissModalViewControllerAnimated:YES];
[utilityController release];
}
In gameConfig.h I have following configuration:
#define GAME_AUTOROTATION kGameAutorotationNone
Tried to change this to kGameAutorotationCCDirector - same thing. kGameAutorotationUIViewController - uiviews are jumping all over the screen.
Please do not suggest rotating UIView with CGAffineTransformMakeRotation - it's just a hack...
For Kobold2D I solved this for all orientations with a category for the GKMatchmakerViewController. You can make the same category for the other Game Center view controllers.
This will force the GC views to use the current device orientation:
#implementation GKMatchmakerViewController (OrientationFix)
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// [RootViewController option removed for clarity]
// all other autorotation types use the current device orientation
ccDeviceOrientation orientation = [CCDirector sharedDirector].deviceOrientation;
switch (interfaceOrientation)
{
case UIInterfaceOrientationPortrait:
return (orientation == kCCDeviceOrientationPortrait);
break;
case UIInterfaceOrientationPortraitUpsideDown:
return (orientation == kCCDeviceOrientationPortraitUpsideDown);
break;
case UIInterfaceOrientationLandscapeLeft:
return (orientation == kCCDeviceOrientationLandscapeRight);
break;
case UIInterfaceOrientationLandscapeRight:
return (orientation == kCCDeviceOrientationLandscapeLeft);
break;
default:
break;
}
return NO;
}
#end
This is my first ever answer on Stackoverflow, so please forgive me if any formatting may not work.
Anyway, on to the code. This solution works for me, for the Achievements-Viewcontroller. Any other GK-Viewcontroller may work accordingly...
#implementation GKAchievementViewController (OrientationFix)
-(BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
#end
#interface GKLeaderboardViewController (OrientationFix)
#end
-(void)showAchievements
{
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
[self presentModalViewController: achievements animated: YES];
}
[achievements release];
}
- (void)achievementViewControllerDidFinish: (GKAchievementViewController *)viewController
{
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[delegate.viewController dismissModalViewControllerAnimated:YES];
}
Try to call
self.modalPresentationStyle = UIModalPresentationCurrentContext;
Before presenting the ViewController

LandscapeOrientation on start of didload method in objective c

I've made an iPad application,
It works fine when I load my application in portrait mode for first time, but when I load my application in landscape mode for the first time, it takes the coordinates of portrait mode only, because inside my didLoad method I give coordinates of portrait mode only.
Now need to give coordinates of landscape mode inside my didLoad method.
I tried this, inside my didLoad method
if (interfaceOrientation == UIInterfacePortraitmode ||
interfaceOrientation == UIInterfaceUpsideDown)
{
// do this....
}
else
{
// do this....
}
but I am unable to write the condition for the if/else inside my didLoad method.
What should I do?
You can do handling as below -
-(void) viewWillAppear: (BOOL) animated {
[super viewWillAppear: animated];
[self updateLayoutForNewOrientation: self.interfaceOrientation];
}
-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration {
[self updateLayoutForNewOrientation: interfaceOrientation];
}
and then finally custom method -
- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation {
if (UIInterfaceOrientationIsLandscape(orientation)) {
// Do some stuff
} else {
// Do some other stuff
}
}
-(void) viewWillAppear: (BOOL) animated {
[super viewWillAppear: animated];
[self adjustViewtForNewOrientation: self.interfaceOrientation];
}
-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration {
[self adjustViewtForNewOrientation: interfaceOrientation];
}
- (void) adjustViewtForNewOrientation: (UIInterfaceOrientation) orientation {
if (UIInterfaceOrientationIsLandscape(orientation)) {
// Do some stuff
} else {
// Do some other stuff
}
also call adjustViewtForNewOrientation in your ViewDidLaod() method,
I had similar issue with UIScrollView. I had it fixed by aligning the subviews as suggested here.
- (void)alignSubViews
{
// Position all the content views at their respective page positions
scrollView.contentSize = CGSizeMake(self.contentViews.count * scrollView.bounds.size.width,
scrollView.bounds.size.height);
NSUInteger i = 0;
for (UIView *v in self.contentViews) {
v.frame = CGRectMake(i * scrollView.bounds.size.width, 0,
scrollView.bounds.size.width, scrollView.bounds.size.height);
i++;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup subviews and then align the views.
[self alignSubViews];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
duration:(NSTimeInterval)duration
{
[self alignSubViews];
scrollView.contentOffset = CGPointMake(self.currentPage * scrollView.bounds.size.width, 0);
}

tabBarController:shouldSelectViewController method doesn't fire

I have read the Apple docs - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1 about creating TabBar programmatically. I want to detect the TabBar selection so I have used following delegate methods. I am not sure why but these methods don't get fired when I change the Tabs on my iPhone. Could anyone please provide some thought on what's going wrong here. It would be really helpful. Thanks.
- (BOOL)tabBarController:(UITabBarController *)tbController shouldSelectViewController:(UIViewController *)viewController
{
if (viewController == [tbController.viewControllers objectAtIndex:3] )
{
// Enable all but the last tab.
return NO;
}
return YES;
}
- (void)tabBarController:(UITabBarController *)tbController didSelectViewController:(UIViewController *)viewController {
if (viewController == [tbController.viewControllers objectAtIndex:self.appTabs.count] )
{
//do some action
}
}
Did you forget to set the delegate when you created the UITabBarController?
someTabBarController.delegate = self;