Ios 启动应用程序时启动保存状态(swift)

Ios 启动应用程序时启动保存状态(swift),ios,swift,nsuserdefaults,Ios,Swift,Nsuserdefaults,到目前为止,该应用程序所做的是,用户通过单击一个按钮浏览一系列字符串,当他们离开并从多任务处理中删除该应用程序时,它会保存他们在数组中的位置。但是,当你启动应用程序时,你会在第一个字符串处陷入困境,但你必须按下名为showFunFact()的按钮才能到达停止的位置。我希望目前的一个来在启动 代码如下: let TechnologyfactBook = TechFactBook() var TechfactIndex = 0 override func viewDidLoad() { s

到目前为止,该应用程序所做的是,用户通过单击一个按钮浏览一系列字符串,当他们离开并从多任务处理中删除该应用程序时,它会保存他们在数组中的位置。但是,当你启动应用程序时,你会在第一个
字符串处陷入困境,但你必须按下名为
showFunFact()
的按钮才能到达停止的位置。我希望目前的一个来在启动 代码如下:

let TechnologyfactBook = TechFactBook()
var TechfactIndex = 0

override func viewDidLoad() {
    super.viewDidLoad()  
}

@IBAction func showFunFact() {
    if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }
    if (TechfactIndex >= TechnologyfactBook.TechfactsArray.count) {
        self.TechfactIndex = 0
    }

    TechfactIndex = NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation")

    TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]
    TechfactIndex++
    NSUserDefaults.standardUserDefaults().setInteger(TechfactIndex, forKey: "ByteLocation")
    NSUserDefaults.standardUserDefaults().synchronize()
}

只需将加载最后一个位置的代码添加到viewDidLoad中。代码首先检查用户的最后一个位置是否大于起始位置(我假设为0)。一旦选中此项,它将把文本值分配给用户上次拥有的值

override func viewDidLoad() {
    super.viewDidLoad()

    //Checks if the value that is saved into the app is greater than the
    //starting position the app first starts at (assuming index 0?)
    if NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation") != 0 {
        TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]   
    } else {
        //The code that first displays the values if the user
        //just started the app 
    }   
}

只需将加载最后一个位置的代码添加到viewDidLoad中。代码首先检查用户的最后一个位置是否大于起始位置(我假设为0)。一旦选中此项,它将把文本值分配给用户上次拥有的值

override func viewDidLoad() {
    super.viewDidLoad()

    //Checks if the value that is saved into the app is greater than the
    //starting position the app first starts at (assuming index 0?)
    if NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation") != 0 {
        TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]   
    } else {
        //The code that first displays the values if the user
        //just started the app 
    }   
}

那么问题是什么?另外,你已经发布了一些类似于3-4次的变化。那么,它有什么问题呢?正确的一个没有出现吗?不,在启动时,只有第一个出现,而不是用户没有出现。问题是什么?另外,你已经发布了一些类似于3-4次的变化。那么,它有什么问题呢?正确的一个没有出现吗?不,在启动时,只有第一个出现,而不是用户在Ank you上留下的。我已经编辑过了。我发现它有点混乱(OP的一半代码),因为我以前使用过NSUserDefaults,所以我不知道其中的一些。谢谢。我已经编辑过了。我发现它有点混乱(OP的一半代码),因为我以前使用过NSUserDefaults,所以我不知道其中的一些。