gpt4 book ai didi

ios - #selector 函数被意外调用

转载 作者:行者123 更新时间:2023-11-28 14:11:16 24 4
gpt4 key购买 nike

我正在实现 UIToolBar,它有两个按钮,“新建目录”和“编辑(或完成)”。我写了下面的代码,但是,当我按下“完成按钮”时,switchMode 函数和 createNewDirectory 函数都被调用(后一个应该只从“新建目录”按钮调用)

请给我任何建议。

--附加评论

我没有为工具栏和两个按钮使用 Storyboard。由于多种原因,我无法采用 Storyboard。

class DetailViewController: UIViewController {
@IBAction func goBack(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}

var newDirectory: UIBarButtonItem!
var editDirectory: UIBarButtonItem!
var toolbar: UIToolbar!
let flexibleItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
var isEditMode: Bool!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
toolbar = UIToolbar(frame: CGRect(x: 0, y: self.view.bounds.height - 44, width: self.view.bounds.width, height: 44))
newDirectory = UIBarButtonItem(title: "New D", style: .plain, target: self, action: #selector(createNewDirectory(_:)))
newDirectory.isEnabled = false
newDirectory.tintColor = UIColor.init(white: CGFloat(bitPattern: 0), alpha: CGFloat(bitPattern: 0))

editDirectory = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(switchMode(_:)))
editDirectory.tintColor = self.view.tintColor

isEditMode = false

toolbar.items = [newDirectory, flexibleItem, editDirectory]
self.view.addSubview(toolbar)
}

func createNewDirectory(_ sender: UIBarButtonItem) {
print("createNewDirectory was called", sender.title!)
}

func switchMode(_ sender: UIBarButtonItem) {
print("switchMode was called", sender.title!, isEditMode)
if isEditMode == false {
isEditMode = true
newDirectory.isEnabled = true
newDirectory.tintColor = self.view.tintColor
sender.title = "Done"

} else {
isEditMode = false
newDirectory.isEnabled = false
newDirectory.tintColor = UIColor.init(white: CGFloat(bitPattern: 0), alpha: CGFloat(bitPattern: 0))
sender.title = "Edit"
}
}
// some other function not related to this post

--

感谢许多有用的评论!为简化起见,我重新创建了一个示例项目并重现了该问题。

  • 删除了主 Storyboard和相关设置
  • 使用 Swift 3.3
  • swift 类只有 AppDelegate.swift 和 ViewController.Swift。 (代码如下)

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
let viewController = ViewController()
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()

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:.
}


}

ViewController.swift

import UIKit

class ViewController: UIViewController {
var newDirectory: UIBarButtonItem!
var editDirectory: UIBarButtonItem!
var toolbar: UIToolbar!
let flexibleItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
var isEditMode: Bool!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.white
toolbar = UIToolbar(frame: CGRect(x: 0, y: self.view.bounds.height - 44, width: self.view.bounds.width, height: 44))
newDirectory = UIBarButtonItem(title: "New D", style: .plain, target: self, action: #selector(createNewDirectory(_:)))
newDirectory.isEnabled = false
newDirectory.tintColor = UIColor.init(white: CGFloat(bitPattern: 0), alpha: CGFloat(bitPattern: 0))

editDirectory = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(switchMode(_:)))
editDirectory.tintColor = self.view.tintColor

isEditMode = false

toolbar.items = [newDirectory, flexibleItem, editDirectory]
self.view.addSubview(toolbar)
}

@objc func createNewDirectory(_ sender: UIBarButtonItem) {
print("createNewDirectory was called", sender.title!)
}

@objc func switchMode(_ sender: UIBarButtonItem) {
print("switchMode was called", sender.title!, isEditMode)
if isEditMode == false {
isEditMode = true
newDirectory.isEnabled = true
newDirectory.tintColor = self.view.tintColor
sender.title = "Done"

} else {
isEditMode = false
newDirectory.isEnabled = false
newDirectory.tintColor = UIColor.init(white: CGFloat(bitPattern: 0), alpha: CGFloat(bitPattern: 0))
sender.title = "Edit"
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

我重新启动了 iOS 模拟器,现在工作正常。不太确定它之前无法正常工作的确切原因。但无论如何,我都会关闭这个答案。谢谢大家。

--

为了这个问题,我一共创建了三个项目。所有这些都具有相同的逻辑并以相同的方式失败。重启模拟器后,一切正常。所以这似乎是由模拟器内部问题或其他原因引起的。

关于ios - #selector 函数被意外调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499302/

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