Reputation: 571
I'm having an issue integrating a UIPageViewController into my existing app. I've got a simple UITableViewController with a list of items. Tapping one brings up a detail view. I'd just like to let the user swipe from detail view to detail view (pretty common). I followed this to start: http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/.
With a tableview, would I simply wire a cell to segue to the PageViewController and use my existing detail VC as the "content" or "data" view controller?
UPDATE 1: I've got the basics working, I can see my content on the detail view and swipe between items. Everything besides the data passing doesn't work though. The big issue is the nav controller. When I'm shown my detail view, I technically see a nav bar with a back button, but it doesn't have any of the buttons i add in viewdidload (using [self.navigationController.navigationBar addSubview:addButton]). The barbuttonitem I add doesn't show either (i read about passing navitem as property, but i still there's other issues). The content is up under the nav bar (fixable with frame adjustments, but it just makes me think something isn't right). After I swipe once, the custom buttons show (not the barbuttonitem), but pressing them gives me the unrecognized selector errors telling me the initial target of self (the detailVC) is gone. why would this get released? why do the buttons show not on the first load (viewdidload is called)? It just seems like I'm missing a piece connecting the TableVC to PageVC to DetailVC and having the nav controller and bar propagate through. It's just not thinking a nav controller is there it seems.
Current setup: Storyboarded with initial VC as nav controller. Everything worked. I've now taken a cell and wired to a new view controller (that adopts UIPageViewControllerDataSource). Then in its viewdidload i have:
// Create page view controller
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.summaryPageViewController.dataSource = self;
myContentViewController *startingViewController = [self viewControllerAtIndex:self.rowTapped];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
Now i have the following, which i see in apple's example
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
The segue from the table to the view controller keeps the nav (push segue), but I'm losing it after that it seems. I tried adding [self.navigationController pushViewController:self.summaryPageViewController animated:NO];
but that gives me "nested push animation can result in corrupted navigation bar" errors. Like I said, this accomplishes the basics of letting me swipe between views and have the different data show, but it's clear something's missing. I've googled all over the place, but can't find what i'm missing to make pageviewcontroller and nav controller play nice together. Thanks so much in advance for the help and let me know what else i can provide to help.
SORTA SOLUTION: Awarded the bounty for the only answer. It's not quite everything i needed, but mark was helpful and the main thing was to pass a navItem property and add the buttons so that. Be careful with the viewdidload and appear methods on your "content" view controller as they page view controller will not call things in the sequence you're used to as its loading the previous and next controller.
Upvotes: 3
Views: 1264
Reputation: 4264
Yep, sounds good. Have you tried it? Where's the problem?
Edit to answer your edit ;)
You can add UIBarButtonItems in viewDidLoad
of the ViewController pushed by the table view selection this way:
One Item:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemTapped:)];
self.navigationItem.rightBarButtonItem = barButton;
Two items:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemTapped:)];
UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonItemTapped:)];
self.navigationItem.rightBarButtonItems = @[barButton, barButton2];
Use leftBarButtonItem
or leftBarButtonItems
to place the buttons on the left side.
Implement this method (or whatever you called the selector above) to make the buttons work when tapped:
- (void)barButtonItemTapped:(id)sender
{
NSLog(@"Hello");
}
I tried this in a test project with storyboard, controller hierarchy is like this:
UINavigationController -> UITableViewController -> UIViewController (with UIPageViewController as childViewController) -> UIViewController as view controllers inside the UIPageViewController
Since iOS 7 it's normal that your content is displayed below the status bar. If you don't want that you need to adjust your view frames. There are plenty of questions regarding this here on SO.
Hope that helps.
Upvotes: 3