问题描述
我正在开发一个应用程序,我需要在视图控制器之间移动很多时间,主要是循环,但现在我必须存储一些数据(简单变量和数组).
I am working on an app where i needed to move between view controllers a lot of time mostly making a loop but now i have to store some data(simple variable's and array's).
我目前将它们存储在应用程序委托中,但我不知道这是否是个好主意.我在网上查看过,但我真的不知道什么是最适合我的解决方案.
这是我如何设置它:应用委托:
I am currently storing them in the app delegate but i don't know if this is a great idea. i have looked online but i couldn't really tell what is the best solution for me.
This is how i have it set up:Appdelegate:
var aantalSpelers: Int!
var namenSpelers = [String]()
var youself = KaartenSpeler()
var player2 = KaartenSpeler()
var player3 = KaartenSpeler()
var player4 = KaartenSpeler()
var player5 = KaartenSpeler()
var player6 = KaartenSpeler()
var Vragen = [[0,0,0,0,0,5]]
var VragenOnbekend = [[6,0,0,0,0,0]]
var VragenInformatie = [[["Spelletjeskamer",""],["Keuken",""],["Garage",""],["Binnenplaats",""],["Zitkamer",""],["Slaapkamer",""],["Studeerkamer",""],["Eetkamer",""],["Badkamer",""]], [["De Wit",""],["Pimpel",""],["Blaauw van Draet",""],["Roodhart",""],["Groenewoud",""],["Van Geelen",""]], [["Loden pijp",""],["Pistool",""],["Engelse sleutel",""],["Dolk",""],["Touw",""],["Kandelaar",""]]]
var EersteKeerMainScreen = true
在 VC 中:
func Appdelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
let sections = ["Locaties","Personages","Wapens"]
var aantalSpelers: Int!
var namenSpelers = [String]()
var eersteKaarten = [[Int(),Int()]]
var youself: KaartenSpeler!
var player2: KaartenSpeler!
var player3: KaartenSpeler!
var player4: KaartenSpeler!
var player5: KaartenSpeler!
var player6: KaartenSpeler!
//vraag is: [numberVrager,numerGevraagde,numerLocatie,numerPersonage,numerWapen,0=lietgeenkaartzien - 1=locatie, - 2=personage - 3=wapen - 4=onbekend]
var Vragen = [[]]
var VragenOnbekend = [[]]
var VragenInformatie = []
var EersteKeerMainScreen = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Get information back from appdelegate
aantalSpelers = Appdelegate().aantalSpelers
namenSpelers = Appdelegate().namenSpelers
youself = Appdelegate().youself
player2 = Appdelegate().player2
player3 = Appdelegate().player3
player4 = Appdelegate().player4
player5 = Appdelegate().player5
player6 = Appdelegate().player6
Vragen = Appdelegate().Vragen
VragenOnbekend = Appdelegate().VragenOnbekend
VragenInformatie = Appdelegate().VragenInformatie
EersteKeerMainScreen = Appdelegate().EersteKeerMainScreen
这真的是一个可行的选择吗?
And is this actually a viable option?
附言抱歉我的英语不好
推荐答案
在这种情况下我将使用的方法(可能不是最好的)是通过使用 self.performSegueWithIdentifier("segueTitle",发件人).
The approach I would use (may not be the best) in this case is to pass the variables with you using segues through the use of self.performSegueWithIdentifier("segueTitle", sender).
然后你可以在 prepareForSegue 中实际处理你从这个 VC 移动到另一个 VC 的对象.
Then you can in a prepareForSegue actually handle the objects you moving from this VC to another VC.
例如
/*Global Variable is numbersArray*/
var numbersArray: [String] = []
每当您完成为视图控制器编写逻辑时,请使用:
and whenever you finish writing your logic for the view controller, use:
self.performSegueWithIdentifier("segueTitle", sender: self)
现在要处理要传递给下一个 VC 的内容,请添加以下内容:
Now to handle what you want to pass to the next VC, add this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "segueTitle") {
// pass data to next view
let destinationVC = segue.destinationViewController as! YourNextViewController
destinationVC.numbersArray = self.numbersArray;
}
}
要记住的是,您在下一个 VC 中必须具有相同的全局变量,因为您可以看到我们在上面的示例中将 current 分配给 next.还要确保 segue 是通过 Storyboard 创建和连接的,并将 segueTitle 替换为给它的标题.
Things to keep in mind is that you must have the same global variable in next VC as you can see we are assigning current to next in the above example. Also make sure that segue is created and connected via Storyboard and replace segueTitle with title given to it.
更新:例如转班
class TranferClass {
var numbersArray:[String] = []
}
然后实例化这个类并发送给下一个VC.例如:
then instantiate this class and send it to next VC.Eg.:
在 VC 1 中:
/*in global scope*/
var transferObject = TransferClass()
在同一个VC中,设置数组例如:
in the same VC, set the array for example:
self.transferObject.numbersArray[0] = "Hello"
然后触发转场:
self.performSegueWithIdentifier(...)
然后处理传球:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "segueTitle") {
// pass data to next view
let destinationVC = segue.destinationViewController as! YourNextViewController
destinationVC.transferObject = self.transferObject;
}
}
这篇关于最佳临时存储措施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!