我正在寻找一种在UIPageViewController中动态显示内容的解决方案。
我的应用正在寻找蓝牙设备,我将它们显示为我的UIPageViewController的不同页面。用户可以使用所有这些设备执行一些操作。
我的目标是每次我的应用发现新设备时添加新页面(如果设备不再可用,则删除页面)。
我的AppDelegate在连接/断开设备时收到通知。
有可能吗? 有没有正确的方法呢?我正在考虑从我的AppDelegate调用UIPageViewController上的更新方法,并添加/删除包含所有视图的数组中的视图。谢谢你的帮助!
以下是我的DevicesPageViewController示例,作为我的UIPageViewController的DataSource AND Delegate(请注意,我只给出了我修改过的方法):
这是.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
这是.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