Iphone 如何访问同一实例中另一个类中的函数?
我目前正在Swift 3中创建一个登录项目。 我有一个主视图控制器,它包含Iphone 如何访问同一实例中另一个类中的函数?,iphone,swift,function,Iphone,Swift,Function,我目前正在Swift 3中创建一个登录项目。 我有一个主视图控制器,它包含 一个容器,包括一个页面视图控制器和多个页面 底部的自定义“菜单”,包括两个用于导航页面视图控制器的按钮和一个自定义页面控件 用户将看到的视图由页面视图控制器(位于顶部)和控制菜单(位于底部)控制的页面组成。当用户单击控制菜单中的按钮时,此代码将在主viewcontroller中执行: @IBAction func buttonNext(_ sender: UIButton) { print("buttonNex
@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)
执行函数nextPageWithIndex(index:1)
,方法是在page1 viewcontroller类中通过定义以下项按下按钮:
var varAccountRegisterMainViewController: accountRegisterMainViewController!
在AppDelegate中,
以及:
在page1 viewcontroller中
遗憾的是,这似乎不起作用。我在以下位置得到“致命错误:索引超出范围”:
let nextWalkthroughVC = pages[index]
在主viewcontroller中的函数nextPageWithIndex(索引:1)
中。我检查了数组“pages”的计数。是0。但是,当我从主viewcontroller执行函数时,它应该有3个
谢谢你的帮助
编辑: 以下是页面数组的设置方式: 在主viewcontroller类的开头添加了以下代码:
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)
另一次编辑: 我将“pages”的声明也放入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")
}
我从主viewcontroller类的顶部复制了代码,并将其粘贴到函数中。不过,我总是会遇到错误“致命错误:在展开可选值时意外发现nil”
有没有办法重新定义函数中的对象?
还是有什么优雅的方式?我的路看起来很乱
感谢Phillip Mills,我知道这与成为类主viewcontroller的另一个实例有关。如何访问同一实例中的函数?主viewcontroller始终可见
谢谢大家! 执行此操作时:
accountRegisterMainViewController()
您正在创建一个新对象,即该类的另一个实例。由于没有显示新的数组,因此永远不会调用viewDidLoad
,并且数组为空
您需要安排对象之间的链接,以便从故事板加载的链接也是调用
nextPageWithIndex
的链接。如何在accountRegisterMainViewController
中设置页面
数组?我刚刚更新了问题。谢谢哇!我不知道它会创建该类的新实例。在其他情况下,这对我来说也是个问题。在我的情况下,你会怎么做?链接是什么意思?我指的是页面控制器中的一个变量,它指向来自故事板的主控制器。通过查看加载到窗口根视图控制器中的内容,您应该可以在应用程序代理中执行此操作。请提供一些代码,好吗?我是编程新手。你会救我的!
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")
}