gpt4 book ai didi

ios - 如何解决 "Method does not override any method from its superclass"错误?

转载 作者:行者123 更新时间:2023-11-28 12:57:49 32 4
gpt4 key购买 nike

我试图通过“shouldPerformSegueWithIdentifier”覆盖函数进行一种条件转换,它报告错误“方法不覆盖其父类(super class)中的任何方法”并告诉我删除“覆盖”一词。当我这样做时,它会报告下一个错误:

"Method 'shouldPerformSegueWithIdentifier(:sender:)' with Objective-C selector 'shouldPerformSegueWithIdentifier:sender:' conflicts with method 'shouldPerformSegueWithIdentifier(:sender:)' from superclass 'UIViewController' with the same Objective-C selector".

现在这是我的代码:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
//MARK: Propierties
@IBOutlet weak var whateverLabel: UILabel!
@IBOutlet weak var errorMessageLabel: UILabel!
@IBOutlet weak var textFieldRandomWord: UITextField!
//MARK: Actions
@IBAction func printWhatever(sender: UIButton) {whateverLabel.text = "Whatever"
}

@IBOutlet weak var goOnButton: UIButton!

override func shouldPerformSegueWithIdentifier(identifier: String, sender: UIButton?) -> Bool {
if identifier == "firstsegue" && sender == goOnButton { // you define it in the storyboard (click on the segue, then Attributes' inspector > Identifier

if textFieldRandomWord.text == "Whatever" {
errorMessageLabel.textColor = UIColor .redColor()
errorMessageLabel.text = "*** NOPE, segue wont occur"
return false
}
else {
errorMessageLabel.text = "*** YEP, segue will occur"
}
}

// by default, transition
return true
}
override func viewDidLoad() {
super.viewDidLoad()
textFieldRandomWord.delegate = self //ViewController is textFieldRandomWord's delegate, so "self" reffers to itself (ViewController)//
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
//Hide the keyboard.
textFieldRandomWord.resignFirstResponder()
return true}
func textFieldDidEndEditing(textField: UITextField){
whateverLabel.text = textFieldRandomWord.text}
//Above, the textFieldShouldReturn function makes the text field inactive when return (enter) is pressed. The last function gets activated automatically when this happens.//
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

sender 应该是 AnyObject?。你不能改变方法声明中的类型来满足你当前的需要。如果您的 senderUIButton,您必须检查函数内部。

关于ios - 如何解决 "Method does not override any method from its superclass"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579539/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com