我正在做QR码扫描仪测验应用,用户必须从10个问题中获得10分。在用户获得第一个qn之后,分数将加1并且它将恢复到qr扫描仪页面,在那里他们必须扫描QR代码以获得下一个qn。问题是分数数据的传递。有没有办法没有segue?
这是我的qn1controller
import UIKit
class Quiz1Controller: UIViewController {
@IBOutlet var question: UILabel!
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var button4: UIButton!
@IBOutlet var LabelEnd: UILabel!
@IBOutlet var scorelabel: UILabel!
var score = Int()
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
RandomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RandomQuestions(){
var RandomNumber = arc4random() % 1
RandomNumber += 1
switch(RandomNumber){
case 1:
question.text = "Who is the current Deputy Chairman of People's Association?"
button1.setTitle("Lee Hsien Loong", for: .normal)
button2.setTitle("Chan Chun Sing", for: .normal)
button3.setTitle("Goh Chok Tong", for: .normal)
button4.setTitle("Goh Khen Swee", for: .normal)
CorrectAnswer = "2"
Hide()
break
default:
break
}
}
func Hide(){
LabelEnd.isHidden = true
}
func UnHide(){
LabelEnd.isHidden = false
}
@IBAction func Button1(_ sender: Any) {
UnHide()
if (CorrectAnswer == "1"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
@IBAction func Button2(_ sender: Any) {
UnHide()
if (CorrectAnswer == "2"){
score = score + 1
scorelabel.text = "Score:\(score)"
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
@IBAction func Button3(_ sender: Any) {
UnHide()
if (CorrectAnswer == "3"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
@IBAction func Button4(_ sender: Any) {
UnHide()
if (CorrectAnswer == "4"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
}
这是我的qn2控制器
import UIKit
class Quiz2Controller: UIViewController {
@IBOutlet var question: UILabel!
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var button4: UIButton!
@IBOutlet var LabelEnd: UILabel!
@IBOutlet var scorelabel: UILabel!
var score = Int()
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
RandomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RandomQuestions(){
var RandomNumber = arc4random() % 1
RandomNumber += 1
switch(RandomNumber){
case 1:
question.text = "Who is the founder of People's Association?"
button1.setTitle("Lee Hsien Loong", for: .normal)
button2.setTitle("Lee Kuan Yew", for: .normal)
button3.setTitle("Goh Chok Tong", for: .normal)
button4.setTitle("Goh Khen Swee", for: .normal)
CorrectAnswer = "1"
Hide()
break
default:
break
}
}
func Hide(){
LabelEnd.isHidden = true
}
func UnHide(){
LabelEnd.isHidden = false
}
@IBAction func Button1(_ sender: Any) {
UnHide()
if (CorrectAnswer == "1"){
score = score + 1
scorelabel.text = "Score:\(score)"
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
@IBAction func Button2(_ sender: Any) {
UnHide()
if (CorrectAnswer == "2"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
@IBAction func Button3(_ sender: Any) {
UnHide()
if (CorrectAnswer == "3"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
@IBAction func Button4(_ sender: Any) {
UnHide()
if (CorrectAnswer == "4"){
self.performSegue(withIdentifier: "correct", sender: self)
}
else {
LabelEnd.text = "Incorrect Answer! Try again"
}
}
}
故事板:
答案 0 :(得分:0)
删除Seague并按照以下方式执行操作。假设您要分享NSMutableDictionary
。
只需将变量作为要与viewController共享的类型。
Exa。你想与ShareViewController共享数据,然后采取像
这样的变量var Dict_data = NSMutableDictionary()
现在你要导航的地方,就像假设它是你的ShareViewController一样。记住别忘了将ShareViewController的标识符赋予StoryBoard。
let PV = self.storyboard?.instantiateViewController(withIdentifier: "ShareViewController") as! ShareViewController
PV.data = dict_share_date
self.navigationController?.pushViewController(PV, animated: true)
这里dict_share_data是你的NSMutableDictionry
。您可以选择任何类型,但请记住两种类型必须相同。或者你可以做铸造。
答案 1 :(得分:0)
您可以使用AppDelegate在多个Viewcontroller之间共享数据。您始终可以保存,更新和检索App的AppDelegate中定义的数据。 Here是我之前的使用它有效地使用Appdelegate的。如果你需要Swift版本,请告诉我。
在AppDelegate中将变量定义为
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.min.js"></script>
<div id="root"></div>
您还可以使用NSNotificationCenter在多个viewcontoller中共享数据。如果您有问题重新实施,请告诉我。
如果您需要在导航时将数据从一个viewcontroller传递到另一个viewcontroller,您还可以使用storyboard id实例化viewcontoller,如@Jeetendra在他的ans中解释的那样。