iphone - 如何访问同一实例中另一个类中的函数?

标签 iphone swift function

我目前正在 Swift 3 中创建一个登录项目。 我有一个主视图 Controller ,其中包含

  1. 一个容器,其中包含一个页面 View Controller 和多个页面
  2. 底部的自定义“菜单”,其中包括两个用于导航页面 View Controller 的按钮和一个自定义页面控件

This is my storyboard setup

用户将看到的 View 由顶部的页面 View Controller 控制的页面和底部的控制菜单组成。当用户单击控制菜单中的按钮时,此代码将在主视图 Controller 中执行:

@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)的按钮,该按钮将用户导航到下一页。

我尝试在 page1 viewcontroller 类中按下按钮的操作上使用 varAccountRegisterMainViewController.nextPageWithIndex(index: 1) 执行函数 nextPageWithIndex(index: 1)通过定义:

var varAccountRegisterMainViewController: accountRegisterMainViewController!

在AppDelegate中, 和:

varAccountRegisterMainViewController = accountRegisterMainViewController()

在 page1 View Controller 中。

遗憾的是,这似乎不起作用。我在以下位置收到“ fatal error :索引超出范围”:

let nextWalkthroughVC = pages[index]

在主视图 Controller 的函数nextPageWithIndex(index: 1)中。我检查了数组“pages”的数量。它是 0。但是,当我直接从主视图 Controller 执行该函数时,它应该具有 3。 Here is the error as an image.

感谢您的帮助!

<小时/>

编辑:

以下是页面数组的设置方式:

在主视图 Controller 类的开头添加以下代码:

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")
}

我从主视图 Controller 类的顶部复制了代码,然后将其粘贴到函数中。不过,我总是收到错误“ fatal error :在解包可选值时意外发现 nil”。

有什么方法可以重新定义函数中的对象吗? 或者有什么优雅的方法吗?我的方式看起来很困惑...

感谢 Phillip Mills,我知道这与主视图 Controller 类的另一个实例有关。如何在同一个实例中访问其中的函数?主视图 Controller 始终可见。

谢谢!

最佳答案

当您执行此操作时:accountRegisterMainViewController()您正在创建一个新对象,即该类的不同实例。由于新的未显示,因此永远不会调用 viewDidLoad 并且您的数组为空。

您需要安排对象之间的链接,以便从 Storyboard 加载的对象也是调用 nextPageWithIndex 的对象。

关于iphone - 如何访问同一实例中另一个类中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431584/

相关文章:

c - 如何计算多项式导数?

c - 如何处理错误 "control may reach end of non void function"?

c - 如何实现我的变位词和回文函数来检查用户输入的单词?

swift - 在 Swift 中,如何使用 UIImagePickerController 从 iCloud、Box 等中选择媒体文件

iphone - 在 iPhone 上运行的 PhoneGap 应用程序中的 SQL 数据库问题

iphone - 如何在 Monotouch 中对 UIImageView 进行运动模糊效果?

iphone - TableView 加载错误的单元格

iphone - 如何让 UIImageView 以编程方式更改其图像?

ios - 无法在属性初始值设定项中将自己指定为委托(delegate)

ios - 尝试添加 UIImage 作为 Storyboard中的背景(不重叠我的其他 ViewController 元素)