我有一个UIPageViewController PageVC
,其中包含我在故事板中创建的6个视图控制器。我的PageVC
课程是用Swift编写的。
class PageVC: UIPageViewController, UIPageViewControllerDelegate {
lazy var VCArr: [UIViewController] = {
return [self.VCInstance(name: "OB1"),
self.VCInstance(name: "OB2"),
self.VCInstance(name: "OB3"),
self.VCInstance(name: "OB4"),
self.VCInstance(name: "OB5"),
self.VCInstance(name: "OB6")]
}()
private func VCInstance(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}
override public func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
if let OB1 = VCArr.first {
setViewControllers([OB1], direction: .forward, animated: true, completion: nil)
}
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return VCArr.last
}
guard VCArr.count > previousIndex else {
return nil
}
return VCArr[previousIndex]
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCArr.count else {
return VCArr.first
}
guard VCArr.count > nextIndex else {
return nil
}
return VCArr[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return VCArr.count
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let OB1 = viewControllers?.first,
let OB1Index = VCArr.index(of: OB1) else {
return 0
}
return OB1Index
}
public func nextPageWithIndex(index: Int)
{
let nextVC = VCArr[index]
setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)
}
}
在每个包含的视图控制器中,我有按钮前进到下一个PageVC
索引。大多数包含的视图控制器都有用Objective-C编写的类,所以我调用了下一页的方法:
-(void)pageNext {
PageVC *vc = (PageVC *) self.parentViewController;
[vc nextPageWithIndexWithIndex:2];
}
上述转换效果很好,OB3
完全加载。在OB3
我有一个IBOutlet
到一个按钮,它显示我的通知权限对话框,我的下一页方法在完成处理程序中调用,如下所示。
- (IBAction)enableBtn:(id)sender {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
[self pageNext];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"notification"];
[[NSUserDefaults standardUserDefaults] synchronize];
FIRUser *user = [[FIRAuth auth] currentUser];
NSString *topic = [@"/topics/ios_user_" stringByAppendingString:user.uid];
[[FIRMessaging messaging] subscribeToTopic:topic];
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
点击&#34;允许&#34;页面顺利过渡在通知权限对话框中,OB4
上的某些元素在转换发生后几乎没有加载10秒。例如,按钮出现但文本内部不可见,UILabels
也不可见。如果我等待大约10秒钟,他们会出现。我已尝试重新排列PageVC
索引中的页面,但无论我使用哪个页面,该页面始终显示为部分加载。没有正确加载的元素附加动画。我认为它与权限对话框中的完成处理程序有关。如何在转换时完全加载UIPageViewController
加载中的下一页?
答案 0 :(得分:2)
听起来您正在从非主线程更新UI。尝试将[self pageNext]
放入主队列中的调度:
dispatch_async(dispatch_get_main_queue(), ^{ //update UI in main thread.
[self pageNext]
});