简体   繁体   English

如何在swift中将数组转换为另一个视图控制器?

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

I am trying to segue an array filled with Strings from the FactsViewController to the FavoritesViewController, but for some reason when I add some elements to the array, run the code, and transfer to the FavoritesViewController it says there are no elements in the array... 我试图将一个填充了Strings的数组从FactsViewController转移到FavoritesViewController,但出于某种原因,当我向数组中添加一些元素时,运行代码,并转移到FavoritesViewController,它说数组中没有元素。 。

First View Controller: 第一视图控制器:

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)
}

}

Second View Controller: 第二视图控制器:

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)
}

}

Why will my facts array not segue from the FactsViewController to the FavoritesViewController ? 为什么我的事实阵列不segueFactsViewControllerFavoritesViewController

Your error lies in the method you're using to present FavoritesViewController . 您的错误在于您用于呈现FavoritesViewController You are currently presenting the ViewController via presentViewController() , not a segue, so the method will never be called. 您目前通过presentViewController()呈现ViewController,而不是segue,因此永远不会调用该方法。 You can make a segue from FactsViewController to FavoritesViewController , with a segue identifier. 您可以使用segue标识符从FactsViewControllerFavoritesViewController一个segue。 You could possibly call it goToFavoritesView . 你可以称之为goToFavoritesView

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IOS如何将一个数组转移到另一个视图控制器 - IOS how to segue an array to another view controller 如何使用segue快速将json数组发送到其他视图控制器 - how to send json array to different view controller in swift using segue 快速选择功能中的另一个View Controller - Swift segue to another View Controller in function 快速嵌套在另一个视图控制器内部的视图控制器上执行segue - Performing a segue on a view controller that is nested inside of another view controller in swift 如何在同一视图控制器上选择文本字段(Swift 3) - how to segue a textfield on the same view controller (swift 3) 搜索结果中的Swift搜索结果控制器转到另一个视图控制器 - Swift Search Result Controller in search results segue to another view controller 如何在选择时“取消” AVAudioSession到另一个视图控制器? - How To “Cancel” AVAudioSession on segue to another view controller? 使用IBAction将数据传递到另一个视图控制器而无需快速 - passing data to another view controller with IBAction without segue swift 在Swift中尝试使用警报操作来隔离到另一个视图控制器 - Trying to segue to another view controller using an alert action, in Swift 如何在不快速使用segue的情况下导航并将数据传递到另一个视图控制器 - How to navigate and pass data to another view controller without use of segue in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM