我目前雨燕3 创建一个登录项目,我有一个包含如何访问同一实例内的另一个类的函数?
- 的容器,它包括一个页面视图控制器和多页的主视图控制器
- 定制的“菜单”底部它包括两个按钮用于导航页面视图控制器和自定义页面控制
用户将p上的视图不满是由页面视图控制器控制的页面和顶部的控制菜单构成,底部是控制菜单。当用户在控制菜单中点击按钮的代码将在主视图 - 控制执行:
@IBAction func buttonNext(_ sender: UIButton) {
print("buttonNext has been pressed!")
nextPageWithIndex(index: nextIndex)
}
,这里是它调用的函数:
func nextPageWithIndex(index: Int) {
let nextWalkthroughVC = pages[index]
pageContainer.setViewControllers([nextWalkthroughVC], direction: .forward, animated: true, completion: nil)
print("nextPageWithIndex has been executed with Index \(index)!")
if currentIndex as Int? == 2 {
currentIndex = 2
} else if currentIndex as Int? == 1 {
currentIndex = 2
} else if currentIndex as Int? == 0 {
currentIndex = 1
} else {
currentIndex = 0
print("currentIndex was in inpossible state!")
}
checkButtonNavigation()
assignIndexes()
if let index = currentIndex {
pageControl.currentPage = index
print("PageControl updated")
}
}
checkButtonNavigation()
检查是否在按钮底部应根据当前正在显示的页面显示。
我试图实现一个页面的按钮(在这种情况下page1)导航到下一页的用户。
我试图与varAccountRegisterMainViewController.nextPageWithIndex(index: 1)
上在第1页的ViewController类按压按钮的动作通过定义要执行的函数nextPageWithIndex(index: 1)
:
var varAccountRegisterMainViewController: accountRegisterMainViewController!
在AppDelegate中, 和:
varAccountRegisterMainViewController = accountRegisterMainViewController()
在page1 viewcontroller。
可悲的是,似乎没有工作。我碰到一个“致命的错误:索引超出范围”的观点:
let nextWalkthroughVC = pages[index]
在主视图 - 控制功能nextPageWithIndex(index: 1)
。我检查了数组“页面”的数量。它是0.但是,当我从主视图控制器执行该功能时,它应该有3个。 Here is the error as an image.
谢谢你的帮助!
编辑:
这里是页阵是如何设置:
在主视图 - 控制类的开头添加以下代码:
var pages = [UIViewController]()
在viewDidLoad()中代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage1ViewController")
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage2ViewController")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage3ViewController")
pages.append(page1)
pages.append(page2)
pages.append(page3)
ANOTHER编辑:
我把 “页面” 也进入externalNext()函数的声明。现在的问题是解决了,但还有另外一个......这就是函数现在的样子:
func externalNext(index: Int) {
var pageContainer: UIPageViewController!
var pages = [UIViewController]()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage1ViewController")
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage2ViewController")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "IDAccountRegisterPage3ViewController")
pages.append(page1)
pages.append(page2)
pages.append(page3)
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
view.addSubview(pageContainer.view)
pageContainer.view.addSubview(pageControlView)
print("Current array count of pages is \(pages.count)")
print("Function: nextPageWithIndex has been called with index \(index)")
let nextWalkthroughVC = pages[index]
pageContainer.setViewControllers([nextWalkthroughVC], direction: .forward, animated: true, completion: nil)
print("nextPageWithIndex has been executed with Index \(index)!")
pageControl.currentPage = index
print("PageControl updated")
print("Hello")
}
我从主视图控制器类的顶部复制的代码,只是它粘贴到函数。不过,我总是得到错误“致命错误:意外地发现零,同时解开可选值”。
有什么办法可以重新定义函数中的对象吗? 或者有没有优雅的方式?我的方式看起来很混乱...
感谢Phillip Mills我知道这与作为类的主视图控制器的另一个实例有关。我如何访问那里的函数,但在同一个实例中?主视图控制器始终可见。
谢谢!
“页面”数组在'accountRegisterMainViewController'中如何设置? –
我刚更新了这个问题。谢谢! – wyte