我正在使用UISegmentedControl和UIPageViewController实现Tab控件实现。 下面提到的代码在某些设备上运行良好,对应于单击的段控件的视图将按预期显示。
在某些iPhone设备上(我认为使用iOS 11),当我点击分段控件时,两个UIViewControllers根本不可见。当我调试View Hierarchy时,我根本看不到与该段控件对应的VC。
但是我注意到当点击相应的段控件时,会在每个VC中调用VC的所有委托方法,如ViewDidLoad,ViewWillAppear等。
我可能做错了什么?
Date Available SickLeave CasualLeave MorningPermission EveningPermission
2017-12-20 3 1 0 0 0
2017-12-21 2 1 1 0 0
当从UIViewController实现中执行时,下面提到的代码用于检查View是否可见失败。
import UIKit
class Solution2SegmentControl : UISegmentedControl
{
private var current:Int = UISegmentedControlNoSegment
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
current = self.selectedSegmentIndex
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if current == self.selectedSegmentIndex {
self.sendActions(for: .valueChanged)
}
}
}
class ATSReferralScreensContainer: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
weak var delegateObj:ATSParentViewController?
@IBOutlet var switchControl: UISegmentedControl!
var referAFriendVC :ATSReferalScreenVC?
var referralInformationVC : ATSReferralInfomationVC?
func setVCForIndex(index: Int) {
setViewControllers([index == 0 ? referAFriendVC! : referralInformationVC!], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
func segmentControlClicked(sender: UISegmentedControl)
{
setVCForIndex(index: sender.selectedSegmentIndex)
}
@IBAction func SegmentControlClick(_ sender: UISegmentedControl) {
setVCForIndex(index: sender.selectedSegmentIndex)
}
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self;
let mainStoryBoard = UIStoryboard(name: "MainStoryBoard_ATS", bundle: nil)
referAFriendVC = mainStoryBoard.instantiateViewController(withIdentifier: "ReferalScreenVC") as! ATSReferalScreenVC
referralInformationVC = mainStoryBoard.instantiateViewController(withIdentifier: "ReferralInformationVC") as! ATSReferralInfomationVC
referralInformationVC?.parentDelegateObj = delegateObj
setVCForIndex(index: 0)
}
override func viewWillAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func pageViewController(_ pageView:UIPageViewController, viewControllerAfter nextVC:UIViewController) -> UIViewController?
{
if pageView == referAFriendVC
{return referralInformationVC } else { return nil }
}
func pageViewController(_ pageView:UIPageViewController, viewControllerBefore previousVC:UIViewController) -> UIViewController?
{
if pageView == referralInformationVC
{ return referAFriendVC } else { return nil }
}
}