簡體   English   中英

如何在swift中將數組轉換為另一個視圖控制器?

[英]How to segue array to another view controller in swift?

我試圖將一個填充了Strings的數組從FactsViewController轉移到FavoritesViewController,但出於某種原因,當我向數組中添加一些元素時,運行代碼,並轉移到FavoritesViewController,它說數組中沒有元素。 。

第一視圖控制器:

import UIKit

class FactsViewController: UIViewController {

@IBOutlet var factsLabel: UILabel!
@IBOutlet var nextButton: UIButton!
@IBOutlet var previousButton: UIButton!
@IBOutlet var favoriteButton: UIButton!
var factNumber: Int = 0
var i = 0
var favoriteFactsList: [String] = []
var factsList: [String] = ["Fact 1", "Fact 2", "Fact 3", "Fact 4"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    factsLabel?.text = factsList[factNumber]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// Segue array to Favorites ViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var destinationViewController: FavoritesViewController = segue.destinationViewController as FavoritesViewController
    var arrayToSegue: [String] = favoriteFactsList
    destinationViewController.favoriteList = arrayToSegue
}

// Next Button
@IBAction func nextButton(UIButton: AnyObject) {
    factNumber++

    if(factNumber >= factsList.count - 1) {
        factNumber = factsList.count - 1
    }

    factsLabel.text = factsList[factNumber]
}

// Previous Button
@IBAction func previousButton(UIButton: AnyObject) {
    factNumber--

    if(factNumber <= 0) {
        factNumber = 0
    }

    factsLabel.text = factsList[factNumber]
}

// Favorite Button
@IBAction func favoriteButton(UIButton: AnyObject) {
    favoriteFactsList.append("\(factsList[factNumber])")
    NSLog(String(favoriteFactsList.count))
}

// Present Favorites ViewController
@IBAction func favoritesViewController(UIButton: AnyObject) {
    let favoritesViewController = self.storyboard?.instantiateViewControllerWithIdentifier("favoritesStoryBoard") as FavoritesViewController
    self.presentViewController(favoritesViewController, animated: true, completion: nil)
}

}

第二視圖控制器:

import UIKit

class FavoritesViewController: FactsViewController {

@IBOutlet var favoriteFactsLabel: UILabel!
@IBOutlet var favoriteNextButton: UIButton!
@IBOutlet var favoritePreviousButton: UIButton!

var favoriteList: [String] = [String]()
var favoriteFactNumber: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    factsList = favoriteFactsList

    NSLog("\(favoriteFactsList.count)")

    if(favoriteList.count == 0) {
        favoriteFactsLabel.text = "Sorry, You Have Not Favorited Any Facts Yet"
        favoriteNextButton.hidden = true
        favoritePreviousButton.hidden = true
    }
}

@IBAction func favoriteNextButton(UIButton: AnyObject) {
    favoriteFactNumber++

    if(favoriteFactNumber >= favoriteList.count - 1) {
        favoriteFactNumber = favoriteList.count - 1
    }

    favoriteFactsLabel.text = favoriteList[favoriteFactNumber]
}

@IBAction func favoritePreviousButton(UIButton: AnyObject) {
    favoriteFactNumber--

    if(favoriteFactNumber <= 0) {
        favoriteFactNumber = 0
    }

    favoriteFactsLabel.text = favoriteList[favoriteFactNumber]
}

override func favoriteButton(UIButton: AnyObject) {
    favoriteList.append("\(factsList[factNumber])")
}

@IBAction func returnButton(UIButton: AnyObject) {
    let factsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("factsStoryBoard") as FactsViewController
    self.presentViewController(factsViewController, animated: true, completion: nil)
}

}

為什么我的事實陣列不segueFactsViewControllerFavoritesViewController

您的錯誤在於您用於呈現FavoritesViewController 您目前通過presentViewController()呈現ViewController,而不是segue,因此永遠不會調用該方法。 您可以使用segue標識符從FactsViewControllerFavoritesViewController一個segue。 你可以稱之為goToFavoritesView

// Present Favorites ViewController
@IBAction func favoritesViewController(UIButton: AnyObject) {
    self.performSegueWithIdentifier("goToFavoritesView", sender: self)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM