IB网点为零,即使它们在故事板中相连

  • 本文关键字:故事 IB ios swift xcode null
  • 更新时间 :
  • 英文 :


我是Xcode的新手,我已经创建了一个UIPageViewController和一个视图控制器作为它的数据容器,这是我的代码:

class RootPageControllerViewController: UIPageViewController,UIPageViewControllerDataSource {

var myControllers:[UIViewController] = []
let pages:[String] = ["first","second","third","fourth","fifth","six","seven"]
let story = UIStoryboard(name: "Main", bundle: nil)


override func viewDidLoad() {
super.viewDidLoad()
for text in pages{
let vc = self.story.instantiateViewController(identifier: "dataContainer") as! DataContainer
vc.setText(text)
myControllers.append(vc)
}
configureController()
}

func configureController(){
self.dataSource = self
if let first = myControllers.first{
self.setViewControllers([first], direction: .forward, animated: true,completion: nil)
}
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = myControllers.firstIndex(of: viewController),index>0 else { return nil }
let before = index-1
return myControllers[before]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard  let index = myControllers.firstIndex(of: viewController),index<(myControllers.count - 1) else {
return nil
}
let after = index + 1
return myControllers[after]
}

}
class DataContainer: UIViewController {

@IBOutlet var titleView: UILabel!


override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}

override func viewDidLoad() {
super.viewDidLoad()
}

func setText(_ mssg:String){
if titleView != nil{
titleView.text = mssg
}
else{
print("reference is null")
}
}
}

我的插座连接正确,我已经检查了很多次,但在运行时也变成了零。。。我怎样才能防止网点的取消引用

@IBOutlet连接在加载视图之前无效。

.instantiateViewController时,将获得对该控制器对象的引用,但视图尚未加载。

这里有一种解决方法:

for text in pages{
let vc = self.story.instantiateViewController(identifier: "dataContainer") as! DataContainer

// add this line
vc.loadViewIfNeeded()

vc.setText(text)
myControllers.append(vc)
}

相关内容

  • 没有找到相关文章

最新更新