gpt4 book ai didi

ios - 使用选择器使用未解析的标识符

转载 作者:可可西里 更新时间:2023-11-01 01:56:19 25 4
gpt4 key购买 nike

我一直收到此错误 Use of unresolved identofier 'rightBarButtonDidClick'

我已经声明了rightBarButtonDidClick类,
但为什么它显示错误?
根据之前关于相同主题的帖子,我尝试了所有可能的解决方案
就像检查目标成员资格等等。没关系。
这是完整的代码

import UIKit
import RealmSwift

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@objc func rightBarButtonDidClick() {
let alertController = UIAlertController(title: "Logout", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Yes, Logout", style: .destructive, handler: {
alert -> Void in
SyncUser.current?.logOut()
self.navigationController?.setViewControllers([WelcomeViewController()], animated: true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}

}

class ItemsViewController: UIViewController {

let realm: Realm
let items: Results<Item>

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Constants.REALM_URL)
self.realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: syncConfig, objectTypes:[Item.self]))
self.items = realm.objects(Item.self).sorted(byKeyPath: "timestamp", ascending: false)
super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(rightBarButtonDidClick))
}

// Do any additional setup after loading the view.
}

我正在使用 Swift 4.2 Xcode 10.0

最佳答案

您需要将此方法 rightBarButtonDidClick 移动到 ItemsViewController 中。

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

class ItemsViewController: UIViewController {

let realm: Realm
let items: Results<Item>

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Constants.REALM_URL)
self.realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: syncConfig, objectTypes:[Item.self]))
self.items = realm.objects(Item.self).sorted(byKeyPath: "timestamp", ascending: false)
super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(rightBarButtonDidClick))
}

@objc func rightBarButtonDidClick() {
let alertController = UIAlertController(title: "Logout", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Yes, Logout", style: .destructive, handler: {
alert -> Void in
SyncUser.current?.logOut()
self.navigationController?.setViewControllers([WelcomeViewController()], animated: true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}

关于ios - 使用选择器使用未解析的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53058391/

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