无法识别的选择器发送到 ios 教程上的实例



(我知道这个问题已经被问了很多次,但他们没有解决我的问题)

您好,我正在其网站上关注IOS入门教程,我按照描述做了所有操作,但是我得到了一个奇怪的异常。我不知道为什么。我也是IOS环境和快速的新手(这是我第一次使用Mac,两天前我得到了它。

这是我的代码:

import UIKit
class ViewController: UIViewController {
// MARK: Properties
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var mealNameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// MARK: Actions
@IBAction func setDefaultMealNameLabel(_ sender: AnyObject) {
mealNameLabel.text = "Default Meal Name"
}
}

它应该将mealNameLabel的文本更改为"默认餐食名称"。我怎么会在标题中得到奇怪的例外。我也不知道什么是选择器,我才刚刚开始。

编辑: 名为setDefaultMealNameLabel的功能的参数类型以前是 UIButton。它都没有奏效。

编辑: 发生错误的位置:

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { // DEBUGGER SHOWS THAT, IN THIS PLACE, ERROR OCURED.
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

可能是您在从故事板建立连接后更改了 IBAction 的名称吗?在这种情况下,当您点击按钮时,它会尝试调用不再存在的方法。

如果是这种情况,则需要删除连接的操作,并在情节提要中按钮的已发送事件上更新它。

Swift 中的选择器

尝试检查您的故事板,如果存在任何不良的 IBActionIBOutlet,请将其删除。

希望它能帮助你.

最新更新