将数据传递回上一个视图控制器(实例成员不能在类型错误时使用)

Passing data back to previous view controller (Instance member cannot be used on type error)

提问人:hudsonhooper 提问时间:5/21/2020 更新时间:5/22/2020 访问量:79

问:

我有 2 个值,用于跟踪用户可以在应用内购买的权益类型的当前值。我想在purchaseViewController中更新它们,并在关闭purchaseViewController后在上一个视图控制器中使用它们。我尝试使用协议/委托和回调来执行此操作。但是,我得到的错误基本上相同,两者都说“实例成员'委托/回调'不能用于类型'purchasesViewController'”。这是我第一次尝试传回数据,我主要只是按照教程进行操作,并且可能在此过程中错过了一步,但我认为这两种方法都会出现相同类型的错误,这很奇怪。

父视图控制器上的代码:

func sendBack(staticIncreases: Int, percentIncreases: Int) { //Protocol function
    self.avalibleIncreases = staticIncreases
    self.currentPercentIncreases = percentIncreases
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier ==  "improveFromOpen"{
        let next = segue.destination as! purchasesViewController
        next.currentPercentIncreases = currentPercentIncreases
        next.username = username
        next.avalibleIncreases = avalibleIncreases
        purchasesViewController.delegate = self //Error "Instance member 'delegate' cannot be used on type 'purchasesViewController'"
        purchasesViewController.callback = { result in //Error "Instance member 'delegate' cannot be used on type 'purchasesViewController'"
            self.avalibleIncreases = result[0]
            self.currentPercentIncreases = result[1]
        }
    }

purchaseViewController 上的代码:

protocol updateBoosts {
    func sendBack(staticIncreases: Int, percentIncreases: Int)
}

class purchasesViewController: UIViewController {

    var delegate: updateBoosts? = nil
    var callback : (([Int])->())?

    var username: String!
    var currentPercentIncreases: Int!
    var avalibleIncreases: Int!

func sendBackVals(){
    if self.delegate != nil {
        self.delegate?.sendBack(staticIncreases: avalibleIncreases, percentIncreases: currentPercentIncreases)
    }

    let out = [avalibleIncreases!, currentPercentIncreases] as [Int]
    callback?(out)
}

override func viewWillDisappear(_ animated: Bool) {
    sendBackVals()
}

知道我的错误在哪里吗?

iOS Swift UIVieviceController 回调 委托

评论

0赞 rbaldwin 5/21/2020
协议必须是 ,应该是 ,并确保符合父类中的协议。您也不需要 nil check in ,因为您使用的是可选的链接。protocol NAME: class { }delegateweaksendBackVals()
0赞 rbaldwin 5/21/2020
stackoverflow.com/questions/24099230/delegates-in-swift

答:

0赞 Mohammad Kamar Shad 5/21/2020 #1

您的代码存在一些设计问题。

您应该一次使用其中之一,因为两者都用于相同的目的。否则,同样的事情将被执行两次。delegateclosure

如果您使用委托,则必须更改协议,如下所述。此外,您必须确认协议。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier ==  "improveFromOpen"{
        let next = segue.destination as! purchasesViewController
        next.currentPercentIncreases = currentPercentIncreases
        next.username = username
        next.avalibleIncreases = avalibleIncreases
        next.delegate = self 
    }
}

确认协议

假设您要获取回调的当前视图控制器是HomeViewController

// Confirming the protocol
extension HomeViewController: UpdateBoostsDelegate {
   func sendBack(staticIncreases: Int, percentIncreases: Int) {
    // write code here for handling the callback
   }
}

如果要使用闭包,请删除委托。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier ==  "improveFromOpen"{
        let next = segue.destination as! purchasesViewController
        next.currentPercentIncreases = currentPercentIncreases
        next.username = username
        next.avalibleIncreases = avalibleIncreases
        next.callback = { result in
                self.avalibleIncreases = result[0]
                self.currentPercentIncreases = result[1]
       }
    }
// Would be good if you add the semantic to protocol name
protocol UpdateBoostsDelegate: class {
    func sendBack(staticIncreases: Int, percentIncreases: Int)
}

PurchaseViewController

class purchasesViewController: UIViewController {
// Make it weak so that no retain cycle is formed
   weak var delegate: UpdateBoostsDelegate?
// rest code will follow ....

建议

  1. 对于 Classes、struct、enum、protocol,每个单词的第一个字符应始终为大写字母。
  2. 该协议应该具有语义,即如果创建它是为了通知事件,则添加 Delegate 后缀,或者如果创建它是为了满足数据需求,则在协议名称中添加数据源后缀。

有关闭合的更多信息

委派设计模式

评论

0赞 hudsonhooper 5/21/2020
我尝试实现您建议的更改,当我从准备 segue 中删除委托时,我收到与之前相同的关于关闭的错误。我还实现了你们所有人对委托/协议流程的建议,然后摆脱了闭包,但再次具有相同的“实例成员'委托'不能用于类型'purchasesViewController';你是不是想改用这种类型的值?“错误,与之前相同。另外,不确定它是否应该,但是当我输入它时,它绝不会像大多数其他事情那样自动建议委托purchasesViewController.delegate
0赞 Mohammad Kamar Shad 5/22/2020
@hudsonhooper 让我们在这里讨论 chat.stackoverflow.com/rooms/214353/ios-basic-discussion
0赞 Mohammad Kamar Shad 5/22/2020
再次检查代码,您将不得不使用哪个实例,您应该设置委托或闭包,例如或nextpurchasesViewControllernext.delegatenext.callback = { result in self.avalibleIncreases = result[0] self.currentPercentIncreases = result[1] }
0赞 Mohammad Kamar Shad 5/22/2020
欢迎您:)那么你应该接受答案@hudsonhooper