简体   繁体   English

通过segue在视图控制器之间传递数据

[英]Passing data between view controllers through segue

I have a MapViewController with a prepareForSegue(_:sender:) method, which I intend to use to send data to LandmarkTableViewController , and is called when a button is pressed. 我有一个带有prepareForSegue(_:sender:)方法的MapViewController ,我打算用它将数据发送到LandmarkTableViewController ,并在按下按钮时调用。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destinationvc = segue.destinationViewController
        if let landmarkvc = destinationvc as? LandmarkTableViewController {
            if let identifier = segue.identifier {

                let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])

                let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])

                switch identifier {
                    case "showLibrary" : landmarkvc.passedLandmark = library // pass data to LandmarkTableViewController
                    case "showBank" : landmarkvc.passedLandmark = bank // pass data to LandmarkTableViewController
                    default : break
                }
            }
        }
    }

The LandmarkTableViewController is properly set up to display the String array properties , with one String on each row. LandmarkTableViewController已正确设置为显示String数组properties ,每行包含一个String。 So what I intend to do is pass the appropriate data for the table to properties according to which button was pressed, and let LandmarkTableViewController display the corresponding properties . 所以我打算做的是根据按下的按钮将表的相应数据传递给properties ,并让LandmarkTableViewController显示相应的properties

class LandmarkTableViewController: UITableViewController {
    var properties = [String]()
    var passedLandmark = Landmark(name: "temp", properties: ["temp"]) // initially set to default value

    override func viewDidLoad() {
        super.viewDidLoad()

        loadSampleProperties()
    }

    func loadSampleProperties() {
        self.properties = passedLandmark!.properties
    }
    // other methods....
}

class Landmark {
    var name: String
    var properties: [String]

    init?(name: String, properties: [String]) {
        self.name = name
        self.properties = properties

        // Initialization should fail if there is no name or if there is no property.
        if name.isEmpty || properties.isEmpty {
            return nil
        }
    }

However, when I run the code, only temp is displayed in the table view. 但是,当我运行代码时,表视图中只显示temp。 I've been stuck on this for a long time now, so any help is much appreciated! 我已经坚持了很久了,所以任何帮助都非常感谢!

Edit: loadData() inside of viewDidLoad() is changed to the correct loadSampleProperties(). 编辑: viewDidLoad()内的loadData()更改为正确的loadSampleProperties()。 I made an error while posting the code to the question. 在将问题发布到问题时我犯了一个错误。

I think this should solve your problem if not double check your identifiers and you can make sure to data passing with adding print(passedLandmark) to viewDidLoad() or breakpoint to make sure you getting the data 我认为这应该可以解决您的问题,如果不仔细检查您的标识符,您可以确保数据传递与添加print(passedLandmark)到viewDidLoad()或断点,以确保您获取数据

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)        {
    let destinationvc = segue.destinationViewController
    if let landmarkvc = destinationvc as? LandmarkTableViewController {
        if segue.identifier == "showLibrary" {

            let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
              landmarkvc.passedLandmark = library
        }
        if segue.identifier == "showBank" {
            let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
            landmarkvc.passedLandmark = bank
        }
    }
}

Hope this will helps 希望这会有所帮助

Code is missing from your quote, so I can't be sure, but I assume your loadData() method is the one that reloads the table view data with Landmark you've passed in prepareForSegue. 您的引用中缺少代码,因此我无法确定,但我认为您的loadData()方法是使用您在prepareForSegue中传递的Landmark重新加载表视图数据的方法。 If that is the case: 如果是这种情况:

viewDidLoad() is called before prepareForSegue, so that all the views and elements of the destinationViewController are loaded and ready to use. prepareForSegue 之前调用viewDidLoad(),以便加载并准备好使用destinationViewController的所有视图和元素。 Thus, in your case, the table view is loaded with your "temp" data and nothing makes it reload when you set the proper one. 因此,在您的情况下,表视图加载了您的“临时”数据,当您设置正确的数据时,没有任何东西可以重新加载。

You have two options: You could call loadData()/reloadData() in viewWillAppear for example, which is called after prepareForSegue(). 您有两个选择:例如,您可以在viewWillAppear中调用loadData()/ reloadData(),这在prepareForSegue()之后调用。 Bare in mind that viewWillAppear will possibly be called again in some other navigation. 请记住,viewWillAppear可能会在其他导航中再次被调用。 Otherwise, you could instantiate and present/push the new controller in your parent view controller, instead of using the segue. 否则,您可以在父视图控制器中实例化并显示/推送新控制器,而不是使用segue。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM