如何使用导航控制器?

huangapple go评论68阅读模式
英文:

How to use Navigation Controller?

问题

我有这个使用UIKit的应用程序。

我想要做的是,当在问题视图控制器中按下“开始动物问答”按钮时,会显示动物问题。如果按下“开始足球问答”,则在问题视图控制器中显示足球问题。

英文:

enter image description here
I have this app with UIKit.

What I want to do is that when the Start Animal Quiz button is pressed in the question view controller, animal questions are displayed. If Start Soccer Quiz is pressed, soccer questions are shown in the question view controller.

答案1

得分: -1

首先,在你的QuestionViewController中添加一个变量来跟踪测验的类型。你可以像下面这样使用枚举:

enum QuizType {
    case animals, football, undefined
}

var quizType: QuizType = .undefined

现在,你可以使用prepareForSegue:sender:方法来设置这个变量,然后在QuestionViewController中检测它,如下所示。

你需要在触发导航控制器的两个视图控制器中都实现这个方法。

在具有“开始动物测验”按钮的视图控制器中,你可以定义方法如下:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navigationVC = segue.destination as? UINavigationController,
       let questionVC = navigationVC.viewControllers.first as? QuestionViewController {
        questionVC.quizType = .animals
    }
}

而在具有“开始足球测验”按钮的视图控制器中,你可以定义方法如下:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navigationVC = segue.destination as? UINavigationController,
       let questionVC = navigationVC.viewControllers.first as? QuestionViewController {
        questionVC.quizType = .football
    }
}

然后,在你的QuestionViewController中,在viewDidLoad()方法内(或任何你需要的地方),你将能够检测测验的类型,然后根据需要设置问题,如下所示:

switch quizType {
    case .animals:
        print("这是一个动物测验,加载动物问题")
                
    case .football:
        print("这是一个足球测验,加载足球问题")
                
    default: 
        print("这不应该发生;显示错误消息或使用默认的测验类型")
}
英文:

First add a variable to your QuestionViewController that tracks the kind of quiz it is. You could use an enum for this like so

enum QuizType {
    case animals, football, undefined
}

var quizType: QuizType = .undefined

Now, you can use the prepareForSegue:sender: method to set this variable and then detect it in the QuestionViewController as shown below.

You would need to implement this method in both the view controllers that trigger the segue to the navigation controller.

In the view controller that has the ‘Start Animal Quiz’ button, you can define the method as below:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navigationVC = segue.destination as? UINavigationController,
       let questionVC = navigationVC.viewControllers.first as? QuestionViewController {
        questionVC.quizType = .animals
    }
}

And in the view controller that has the ‘Start Futbol Quiz’ button, you can define the method as below:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navigationVC = segue.destination as? UINavigationController,
       let questionVC = navigationVC.viewControllers.first as? QuestionViewController {
        questionVC.quizType = .football
    }
}

Then in your QuestionViewController, inside the viewDidLoad() method (or anywhere else you need), you'll be able to detect the kind of quiz it is and then set the questions accordingly like below:

switch quizType {
    case .animals:
        print("this is an animals quiz, load animal questions")
                
    case .football:
        print("this is an football quiz, load football questions")
                
    default: 
        print("this should not happen; show an error message or use a default quiz type")
}

huangapple
  • 本文由 发表于 2023年2月27日 07:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定