My UI structure:
UINavigationController
--UIViewController(first)
|
|Modal
|
V
UINavigationController (subclass)
--UIViewController (contains UIPageViewController)
For iOS6 new autorotation, I created a subclass of UINavigationController and added:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Only want keep the orientation is portrait.
And I initialize UIPageViewController like:
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
PageDataViewController *startingViewController = [self viewControllerAtIndex:start storyboard:self.storyboard];
NSArray *viewControllers = [NSArray arrayWithObject:startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
Then I perform the Modal segue,
if the first UIViewController is portrait, the pageViewController works,
but if the first UIViewController is landscape, I always got exception:
* Assertion failure in -[UIPageViewController willAnimateRotationToInterfaceOrientation:duration:],
/SourceCache/UIKit_Sim/UIKit-2372/UIPageViewController.m:945
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No view controllers'
** First throw call stack: (0x2a5e012 0x19bae7e 0x2a5de78 0x1450f35 0xd75974 0x9f12b4 0x9f13ea 0x9e4c32 0x9f1372 0x9f13ea 0x9e4c32
0x9f1372 0x9f1751 0x93a1a6 0xbee5f9 0x9ec4f3 0x9ec777 0x9ec7b7
0xd57fe2 0xd49ad9 0xd49b54 0x9b1899 0x9b1b3d 0x13b8e83 0x2a1d376
0x2a1ce06 0x2a04a82 0x2a03f44 0x2a03e1b 0x2c967e3 0x2c96668 0x90265c
0xea7d 0x2685 0x1) libc++abi.dylib: terminate called throwing an
exception
Actually There is another solution for it.
The rotation just calling the willAnimateRotationToInterfaceOrientation method to the rootViewController, you have to implement the method of it.
#interface UIPageViewController (Rotation)
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
#end
//Implementation file
#import "UIPageViewController+Rotation.h"
#implementation UIPageViewController(Rotation)
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {}
#end
In the documentation about "supportedInterfaceOrientations" it is written:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window
Did you try setting the rootViewController for the main windows to your current NavigationController classes?
Like in the AppDelegate:
self.window.rootViewController = nvc;
And when your navigationController is changing, set the rootViewController again...
I have "fixed" the problem, by setting view controllers to UIPageViewController in viewWillAppear: again, because when this method is called, spine location for page view controller is correct and based on this information i set one, or two pages (view controllers)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.pageViewController.spineLocation == UIPageViewControllerSpineLocationMid)
{
BookPageViewController *currentViewController = self.pageViewController.viewControllers[0];
NSArray *viewControllers = nil;
NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];
if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) {
UIViewController *nextViewController = [self.modelController pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
viewControllers = #[currentViewController, nextViewController];
} else {
UIViewController *previousViewController = [self.modelController pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
viewControllers = #[previousViewController, currentViewController];
}
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
if(self.pageViewController.spineLocation == UIPageViewControllerSpineLocationMin)
{
[self.pageViewController setViewControllers:[NSArray arrayWithObject:[self.pageViewController.viewControllers objectAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
}
and set UIPageViewController Delegate method as below
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
BookPageViewController *currentViewController = self.pageViewController.viewControllers[0];
if(UIInterfaceOrientationIsPortrait(orientation))
{
[self.pageViewController setViewControllers:[NSArray arrayWithObject:currentViewController] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
pageViewController.doubleSided = NO;
return UIPageViewControllerSpineLocationMin;
}
if(UIInterfaceOrientationIsLandscape(orientation))
{
NSArray *viewControllers = nil;
NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];
if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) {
UIViewController *nextViewController = [self.modelController pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
viewControllers = #[currentViewController, nextViewController];
} else {
UIViewController *previousViewController = [self.modelController pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
viewControllers = #[previousViewController, currentViewController];
}
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
return UIPageViewControllerSpineLocationMid;
}
return UIPageViewControllerSpineLocationMin;
}
I have "fixed" the problem, by setting view controllers to UIPageViewController in viewWillAppear: again, because when this method is called, spine location for page view controller is correct and based on this information i set one, or two pages (view controllers)
Related
In my iPad app. I'm working on UIPageViewController I want to set text at one page and image at another page. I have tried a lot and googled,but I did not find any solution. It's killing my time so if any one have worked on it please guide me and post sample code.
I have used the code which is there in below link
http://www.ioslearner.com/implementing-uipageviewcontroller-programatically-without-storyboarding/
here is the sample code. i used contentviewcontroller.
i want to use two view controllers with UIPageViewController
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
//Set the array with only 1 view controller
UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
//Important- Set the doubleSided property to NO.
self.pageViewController.doubleSided = NO;
//Return the spine location
return UIPageViewControllerSpineLocationMin;
}
else
{
NSArray *viewControllers = nil;
ContentViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)currentViewController labelContents]];
NSUInteger currentIndex2 = [self.imagesArray2 indexOfObject:[(ContentViewController *)currentViewController imageContents]];
if(currentIndex == 0 || currentIndex %2 == 0 || currentIndex2 == 0||currentIndex2 %2 == 0)
{
UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
}
else
{
UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
}
//Now, set the viewControllers property of UIPageViewController
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
return UIPageViewControllerSpineLocationMid;
}
}
If I understood well then add an imageView in ContentPageView.xib file and make an outlet of. Set ImageView and label hidden or visible on your required pages.
Following steps can be helpful
Keep UILabel and and UIImageView in ContentViewController
Add UIImage OR NSString in the modelArray.
When you make a new ContantViewController check if object in model is image or string using following.
if ([myObject class] == [UIImage class])
{
//HERE ASSIGN IT TO UIIMAGEVIEW.image
}
else
{
//HERE ASSIGN IT TO UILABEL.text
}
post if you need more help....
Sorry if this is not what you are looking for.....
I am trying to implement a SwipeGesture on my viewcontroller.
The issue I am facing currently is that I cannot determine what the currently displayed childview controller is.
The swipeGesture is added to the container view controller. And then must determine what is the currently displayed VC is in the parent-child relationship, then move to the right or left.
If you are talking about custom container view controller, it's the job of the container controller to keep track of this. So, you might have your own #property that keeps track of which one you're on, and adjust it as you transitionFromController as a result of a swipe. So you might have a numeric property that you increment as you go to the right and that you decrement as you go to the left.
In general (if you're just trying to keep track of which "from" controller you're passing to transitionFromViewController), there are two approaches. One approach is implied by listing 14-3 in Adding a child controller of the View Controller Programming Guide.
In this scenario, in viewDidLoad you perform addChildViewController for the first view controller with. You do not, in this scenario, load the other child view controllers via addChildViewController at this point, though, but rather you let your method that does the transitioning take care of that (as in Listing 14-3).
If you do it this way, you can just grab [self.childViewControllers lastObject], and that will be your "current" child controller. So, that might look like:
#interface ViewController ()
#property (nonatomic) NSInteger currentIndex;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIViewController *initialController = ... // set your initial controller
[self addChildViewController:initialController];
initialController.view.frame = self.containerView.bounds;
[self.containerView addSubview:initialController.view];
[initialController didMoveToParentViewController:self];
UISwipeGestureRecognizer *gesture;
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.containerView addGestureRecognizer:gesture];
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.containerView addGestureRecognizer:gesture];
}
- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
{
self.currentIndex++;
UIViewController *newController = ... // set your new controller
UIViewController *oldController = [self.childViewControllers lastObject];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
{
self.currentIndex--;
UIViewController *newController = ... // set your new controller
UIViewController *oldController = [self.childViewControllers lastObject];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
}
- (void) cycleFromViewController:(UIViewController*) oldController
toViewController:(UIViewController*) newController
direction:(UISwipeGestureRecognizerDirection)direction
{
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];
newController.view.frame = oldController.view.frame;
UIViewAnimationOptions options;
if (direction == UISwipeGestureRecognizerDirectionRight)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else if (direction == UISwipeGestureRecognizerDirectionLeft)
options = UIViewAnimationOptionTransitionFlipFromRight;
[self transitionFromViewController:oldController
toViewController:newController
duration:0.33
options:options
animations:^{
[oldController removeFromParentViewController];
}
completion:^(BOOL finished) {
[newController didMoveToParentViewController:self];
}];
}
The other model that I've seen some people do is to load all of the potential child controllers via addChildViewController in viewDidLoad. I don't personally like this approach, but I know people do it this way and it works fine.
But if you do it this way, you can no longer rely upon childViewControllers to know which controller is the current one. In that case, you have to define your own class property for the currentChildController. So it might look something like:
#interface ViewController ()
#property (nonatomic, strong) UIViewController *currentChildController;
#property (nonatomic) NSInteger currentIndex;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"ChildOne"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"ChildTwo"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"ChildThree"]];
self.currentChildController = self.childViewControllers[0];
self.currentChildController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.currentChildController.view];
for (UIViewController *controller in self.childViewControllers)
[controller didMoveToParentViewController:self];
UISwipeGestureRecognizer *gesture;
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.containerView addGestureRecognizer:gesture];
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.containerView addGestureRecognizer:gesture];
}
- (void) cycleFromViewController:(UIViewController*) oldController
toViewController:(UIViewController*) newController
direction:(UISwipeGestureRecognizerDirection) direction
{
self.currentChildController = newController;
newController.view.frame = oldController.view.frame;
UIViewAnimationOptions options;
if (direction == UISwipeGestureRecognizerDirectionRight)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else if (direction == UISwipeGestureRecognizerDirectionLeft)
options = UIViewAnimationOptionTransitionFlipFromRight;
[self transitionFromViewController:oldController
toViewController:newController
duration:0.33
options:options
animations:^{
}
completion:nil];
}
- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
{
self.currentIndex++;
UIViewController *oldController = self.currentChildController;
UIViewController *newController = self.childViewControllers[self.currentIndex];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
{
self.currentIndex--;
UIViewController *oldController = self.currentChildController;
UIViewController *newController = self.childViewControllers[self.currentIndex];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
}
Those are the two logical approaches.
By the way, the first approach doesn't preclude having your own class property to know which controller you're on, and sometimes that's useful (e.g. if the type of animation you employ is contingent upon whether you're going to the "right" or to the "left), but unless that's the case, you can get away with just looking at [self.childViewControllers lastObject].
In a project i'm working on, I have many View Controllers each with their own Navigation Controller added to a Parent View Controller. I know the initial entry child, but I do the following to find the currently displayed children of the child:
[myKnownViewController.childViewControllers objectAtIndex:0].navigationController.topViewController
I have used the UIPageViewController for loading around 5 images in a webview.
So whenever the below line of code
- (UIViewController *)pageViewController:(UIPageViewController *) pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
, is called, the return object is a class object(in my case class name is LoadPage). Everything works fine in my code, but I am facing some weird problem here and little uncomfortable.
So I planned to move to single class instead of two.
Now the return type for this method will be self.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
return self;
}
Am I right?
But When I try to initialize the UIPageViewController, the application is getting crashed at one particular line.
- (void) createViewControllers {
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options: nil];
pageViewController.delegate = self;
pageViewController.dataSource = self;
NSArray* nextScreens = [NSArray arrayWithObject:self];
[pageViewController setViewControllers: nextScreens direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:pageViewController];
[self.view addSubview:pageViewController.view];
[[self view] addSubview:[pageViewController view]]; //CRASH
[pageController didMoveToParentViewController:self];
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect,0, 0);
pageViewController.view.frame = pageViewRect;
self.view.gestureRecognizers = pageViewController.gestureRecognizers;
}
You cannot return self in pageViewController:viewControllerAfterViewController because that would produce a cycle in the view controller hierarchy.
You add the pageViewController as child view controller to self and then later the pageViewController adds self as child view controller (page) to itself. Thats not allowed!
on language switch within my App, I need to access the views of the MoreViewController in a TabBar and change their titles.
Could anyone pls tell me how to do that?
Your help is much appreciated.
Cols
Here are some snippets that might work for you. Note that all of the below is subject to break on each and every new iOS release at it is not meant to be done.
Customizing the More view title as itself as well as the title while it is being edited by the user.
- (void)customizeTitleViewWithNavigationItem:(UINavigationItem *)navigationItem
{
VASSERT(navigationItem != nil, #"invalid navigationItem supplied", navigationItem);
UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0f];
titleView.text = navigationItem.title;
[titleView sizeToFit];
navigationItem.titleView = titleView;
[titleView release];
}
This needs to be implemented within the UINavigationController's delegate;
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if (navigationController == tabBarController_.moreNavigationController)
{
if ([viewController isKindOfClass:NSClassFromString(#"UIMoreListController")])
{
[self customizeTitleViewWithNavigationItem:viewController.navigationItem];
}
else
{
NSLog(#"viewController (%#) does not seem to be a UIMoreListController", viewController);
}
}
else
{
NSLog(#"navigationController (%#) does not seem to be the moreNavigationController", navigationController);
}
}
This needs to be implemented within the UITabBarController's delegate;
- (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
//get the second view of the upcoming tabbar-controller
UIView *editView = [controller.view.subviews objectAtIndex:1];
//did we get what we expected, which is a UITabBarCustomizeView?
if (editView != nil && [editView isKindOfClass:NSClassFromString(#"UITabBarCustomizeView")])
{ //yes->get the navigation-view
UIView *navigationView = [editView.subviews objectAtIndex:0];
//is that a navigationBar?
if (navigationView != nil && [navigationView isKindOfClass:[UINavigationBar class]])
{ //yes->...
UINavigationBar *navigationBar = (UINavigationBar *)navigationView;
[self customizeTitleViewWithNavigationItem:navigationBar.topItem];
}
else
{
NSLog(#"the navigationView (%#) does not seem to be a navigationBar", navigationView);
}
}
else
{
NSLog(#"the editView (%#) does not seem to be a UITabBarCustomizeView", editView);
}
}
I was able to change the title of one of the tabBar items for my 6th view controller (the last one in the more list) with the following code:
NSArray *vcs = [(UITabBarController *)self.window.rootViewController viewControllers];
[[vcs.lastObject tabBarItem] setTitle:#"New Title"];
Is this what you want to do?
After Edit: To change these titles after they are set up the first time, you need to re-set the viewControllers property of the tabBar controller. In this code example, I connected a button in my 6th view controller to an action method that changes the titles of 3 of my controllers, 2 in the more list and one in the regular list.
-(IBAction)changeNames:(id)sender {
UITabBarController *tbc = (UITabBarController *)[[UIApplication sharedApplication] delegate].window.rootViewController;
NSArray *vcs = tbc.viewControllers;
[[vcs.lastObject tabBarItem] setTitle:#"New Title"];
[[[vcs objectAtIndex:4] tabBarItem] setTitle:#"New VC"];
[[[vcs objectAtIndex:3] tabBarItem] setTitle:#"New VC2"];
tbc.viewControllers = tbc.viewControllers;
}
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
ChatViewController *chatView;
if(contactView==nil)
{
chatView=[[ChatViewController alloc] initWithNibName:#"ChatViewController" bundle:nil];
}
[self.navigationController pushViewController:chatView animated:YES];
[messageDelegate newMessageReceived:m];
}
The above delegate method called for every incoming message.When it called, it goes to a new UIViewController.Here my problem is a view pushed multiple tinmes,so error will be occered.how can i fix this error in iphone
Add this snippet before pushing the view controller
BOOL viewControllerAlreadyPushed = NO;
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[ChatViewController class]]) {
viewControllerAlreadyPushed = YES;
}
}
if(!viewControllerAlreadyPushed) //if not pushed, push it
{
ChatViewController *chatView;
if(contactView==nil)
{
chatView=[[ChatViewController alloc] initWithNibName:#"ChatViewController" bundle:nil];
}
[self.navigationController pushViewController:chatView animated:YES];
[messageDelegate newMessageReceived:m];
}