I'm implementing UIToolBar, which has two buttons , "New Directory" and "Edit(or Done)". I wrote the following code, however, when I pushed "Done button", both of switchMode function and createNewDirectory function is called (latter one should be called from New Directory button only)
Kindly give me any advice.
-- additional comment
I don't use storyboard for toolbar and two buttons. For several reasons, I cannot adopt to use 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
--
Thanks for many helpful comments! To simplify, I re-created a sample project and reproduced the issue.
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.
}
}
I rebooted the iOS simulator and worked fine now. Not quite sure for the exact reason why it didn't work properly before. but anyway with this answser I'll close. thanks everyone.
--
For this issue, I created three projects in total. All of them have the same logic and failed in the same way. After rebooting the simulator, all of them worked fine. So this seems to be caused by simulator internal issue or something.