简体   繁体   中英

Implementing protocol to pass data back to a view controller from a pushed view controller

I am new to iOS development and have been stuck on this problem for a couple of hours. I thought I did it correctly in setting up the structure. Can someone please check it? When I call self.delegate!.updateData(daysSet) in the view controller that should send data back, it gives me the error below

expression resolves to an unused function

Here is what I've tried:

The view controller that should send data back:

import Foundation
import UIKit

class RepeatDailyValueViewController: UITableViewController {

var delegate: RepeatDailyValueViewControllerDelegate? = nil

var daysSet : String = "bam"

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate!.updateData(daysSet)
    }
 }

The view controller that is supposed to receive the data

import Foundation
import UIKit

protocol RepeatDailyValueViewControllerDelegate {
    func updateData(daysSet: String)()
}

class NewAlarmViewController: UITableViewController,     RepeatDailyValueViewControllerDelegate {

var daysSet : String = "bam"

@IBOutlet var labelDay: UILabel!

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let vc = segue.destinationViewController as!   RepeatDailyValueViewController
        vc.delegate = self
}

func updateData(daysSet: String)() {
        self.labelDay.text = daysSet
    }
}

Your problem is that in your protocol you've declared that updateData as a function of type String -> (Void -> Void) (a function that takes a String and returns another function that takes no arguments and returns nothing) instead of what I think you were going for, a function of type String -> Void (a function that takes a String and returns nothing). So, when you give updateData a string, you are getting back a function that takes no argument. That's the unused function the error is describing.

This is called a curried function and can be very useful in other situations .

To fix this, you just need to delete the extra set of parentheses in your protocol declaration:

protocol RepeatDailyValueViewControllerDelegate {
    // Delete the parentheses after (daysSet: String)
    func updateData(daysSet: String) 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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