I'm looking for a solution to display content dynamically in an UIPageViewController.
My app is looking around for Bluetooth devices, and i show them as different pages of my UIPageViewController. User can do a few actions with all of these devices.
My aim is to add a new page each time my app is discovering a new device (or remove a page if a device isn't available any more).
It's my AppDelegate that is receiving notifications when a device is connected/disconnected.
Is it possible ? Is there a right way to do it ? I was thinking about calling an update method on my UIPageViewController, from my AppDelegate, and add/remove views from an Array, containing all the views. Thanks for your help !
Here is a sample of my DevicesPageViewController, as a DataSource AND Delegate of my UIPageViewController (note that i'm giving only methods that i have modified) :
Here is the .h
@interface DevicesPageViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
// Devices items, used for loading the data in
@property (nonatomic, strong) NSMutableArray *devices;
// All children view controllers : all the pages of my UIPageController
@property (nonatomic, strong) NSMutableArray *childrenViewControllers;
@property (nonatomic, strong) UIPageViewController *pageViewController;
- (void) buildDataModelFromDevices;
- (void) updateView;
@end
Here is the .m :
@implementation DevicesPageViewController
- (id) init {
self = [super init];
// Init the pageViewController - This is the UIPageViewController
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"iphoneSTORYBOARD" bundle:nil];
self.pageViewController = [sb instantiateViewControllerWithIdentifier:@"pageViewController"];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
// Move the pageViewController where it belong
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
// AT_appDelegate is a macro, getting the AppDelegate
if (AT_appDelegate.userDevices.count > 0) {
[self updateView];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
#pragma mark - PageController DataSource
/**
* View controller to give before
*/
- (UIViewController*) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = ((DeviceContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) { return nil; }
index--;
return [self.childrenViewControllers objectAtIndex:index];
}
/**
* View controller to give after
*/
- (UIViewController*) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = ((DeviceContentViewController*) viewController).pageIndex;
if ((index == [self.childrenViewControllers count]) || (index == NSNotFound)) { return nil; }
index++;
return [self.childrenViewControllers objectAtIndex:index];
}
/**
* Build data model and init all children view controllers
*/
- (void) buildDataModelFromDevices {
int index = 0;
// For each device to load, create a viewController
for (Device *d in self.devices) {
// Instantiate corresponding view controller
DeviceContentViewController *deviceVC = [[UIStoryboard storyboardWithName:@"iphoneSTORYBOARD" bundle:nil] instantiateViewControllerWithIdentifier:@"deviceContentVC"];
// Load the device in it
deviceVC.device = d;
deviceVC.pageIndex = index;
[deviceVC loadData];
index++;
[self.childrenViewControllers addObject:deviceVC];
}
}
- (void) updateView {
NSLog(@"[ DevicesPageViewController ] -- Updating view from devices...");
// Build the data
self.devices = [[NSMutableArray alloc] initWithArray:AT_appDelegate.userDevices];
[self buildDataModelFromDevices];
if (self.childrenViewControllers.count > 0) {
// Get the first view controller
NSArray *tmp_viewControllers = @[[self.childrenViewControllers objectAtIndex:0]];
[self.pageViewController setViewControllers:tmp_viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:true completion:nil];
}
}
@end