How To Detect Which NSMenuItem Was Selected - swift

Given that I have a menuBar app with 3 items on a subMenu:
let delaySubMenu = NSMenu()
delaySubMenu.addItem(NSMenuItem(title: "5", action: #selector( setReminder(_:)), keyEquivalent: ""))
delaySubMenu.addItem(NSMenuItem(title: "10", action: #selector(setReminder(_:)), keyEquivalent: ""))
delaySubMenu.addItem(NSMenuItem(title: "15", action: #selector(setReminder(_:)), keyEquivalent: ""))
How do I detect which of my delaySubMenu items has been selected without making a unique setReminder function for each?
Thanks

The action selector will receive the sender object just like it would if you were using Interface Builder. So your setReminder(_:) selector could have the signature:
func setReminder(_ sender: Any) {
// Coerce sender to NSMenuItem and use it to make your decisions
}
or:
func setReminder(_ sender: NSMenuItem) {
// Don't do any coercion work you don't need to do…
}
You could also set the tag property of the NSMenuItem to your delay values. The tag property is an Int type so a good match for your values.
As you are creating multiple entries you could use a for in loop to traverse an array or dictionary, creating a new NSMenuItem for each entry. So we could change your original code to something like this example where I use a dictionary:
let delaySubMenu = NSMenu()
let delays = ["5 Minutes" : 5, "10 Minutes" : 10, "15 Minutes" : 15] // This is a dictionary of String:Int
for (titleKey, value) in delays {
let menuItem = NSMenuItem(title: titleKey, action: #selector(setReminder(_:)), keyEquivalent: nil)
menuItem.tag = value
delaySubMenu.addItem(menuItem)
}
func setReminder(_ sender: NSMenuItem) {
let delayValue = sender.tag // delayValue is a Int by inference from tag
// Do something with your delay value
}
disclaimer: this is just cut and paste in the browser so it may needs some tweaking to actually work.

Related

Swift How to pass argument to function #selector [duplicate]

I'm programmatically adding a UITapGestureRecognizer to one of my views:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(modelObj:myModelObj)))
self.imageView.addGestureRecognizer(gesture)
func handleTap(modelObj: Model) {
// Doing stuff with model object here
}
The first problem I encountered was "Argument of '#selector' does not refer to an '#Objc' method, property, or initializer.
Cool, so I added #objc to the handleTap signature:
#objc func handleTap(modelObj: Model) {
// Doing stuff with model object here
}
Now I'm getting the error "Method cannot be marked #objc because the type of the parameter cannot be represented in Objective-C.
It's just an image of the map of a building, with some pin images indicating the location of points of interest. When the user taps one of these pins I'd like to know which point of interest they tapped, and I have a model object which describes these points of interest. I use this model object to give the pin image it's coordinates on the map so I thought it would have been easy for me to just send the object to the gesture handler.
It looks like you're misunderstanding a couple of things.
When using target/action, the function signature has to have a certain form…
func doSomething()
or
func doSomething(sender: Any)
or
func doSomething(sender: Any, forEvent event: UIEvent)
where…
The sender parameter is the control object sending the action message.
In your case, the sender is the UITapGestureRecognizer
Also, #selector() should contain the func signature, and does NOT include passed parameters. So for…
func handleTap(sender: UIGestureRecognizer) {
}
you should have…
let gesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
Assuming the func and the gesture are within a view controller, of which modelObj is a property / ivar, there's no need to pass it with the gesture recogniser, you can just refer to it in handleTap
Step 1: create the custom object of the sender.
step 2: add properties you want to change in that a custom object of the sender
step 3: typecast the sender in receiving function to a custom object and access those properties
For eg:
on click of the button if you want to send the string or any custom object then
step 1: create
class CustomButton : UIButton {
var name : String = ""
var customObject : Any? = nil
var customObject2 : Any? = nil
convenience init(name: String, object: Any) {
self.init()
self.name = name
self.customObject = object
}
}
step 2-a: set the custom class in the storyboard as well
step 2-b: Create IBOutlet of that button with a custom class as follows
#IBOutlet weak var btnFullRemote: CustomButton!
step 3: add properties you want to change in that a custom object of the sender
btnFullRemote.name = "Nik"
btnFullRemote.customObject = customObject
btnFullRemote.customObject2 = customObject2
btnFullRemote.addTarget(self, action: #selector(self.btnFullRemote(_:)), for: .touchUpInside)
step 4: typecast the sender in receiving function to a custom object and access those properties
#objc public func btnFullRemote(_ sender: Any) {
var name : String = (sender as! CustomButton).name as? String
var customObject : customObject = (sender as! CustomButton).customObject as? customObject
var customObject2 : customObject2 = (sender as! CustomButton).customObject2 as? customObject2
}
Swift 5.0 iOS 13
I concur a great answer by Ninad. Here is my 2 cents, the same and yet different technique; a minimal version.
Create a custom class, throw a enum to keep/make the code as maintainable as possible.
enum Vs: String {
case pulse = "pulse"
case precision = "precision"
}
class customTap: UITapGestureRecognizer {
var cutomTag: String?
}
Use it, making sure you set the custom variable into the bargin. Using a simple label here, note the last line, important labels are not normally interactive.
let precisionTap = customTap(target: self, action: #selector(VC.actionB(sender:)))
precisionTap.customTag = Vs.precision.rawValue
precisionLabel.addGestureRecognizer(precisionTap)
precisionLabel.isUserInteractionEnabled = true
And setup the action using it, note I wanted to use the pure enum, but it isn't supported by Objective C, so we go with a basic type, String in this case.
#objc func actionB(sender: Any) {
// important to cast your sender to your cuatom class so you can extract your special setting.
let tag = customTag as? customTap
switch tag?.sender {
case Vs.pulse.rawValue:
// code
case Vs.precision.rawValue:
// code
default:
break
}
}
And there you have it.
cell.btn.tag = indexPath.row //setting tag
cell.btn.addTarget(self, action: #selector(showAlert(_ :)), for: .touchUpInside)
#objc func showAlert(_ sender: UIButton){
print("sender.tag is : \(sender.tag)")// getting tag's value
}
Just create a custom class of UITapGestureRecognizer =>
import UIKit
class OtherUserProfileTapGestureRecognizer: UITapGestureRecognizer {
let userModel: OtherUserModel
init(target: AnyObject, action: Selector, userModel: OtherUserModel) {
self.userModel = userModel
super.init(target: target, action: action)
}
}
And then create UIImageView extension =>
import UIKit
extension UIImageView {
func gotoOtherUserProfile(otherUserModel: OtherUserModel) {
isUserInteractionEnabled = true
let gestureRecognizer = OtherUserProfileTapGestureRecognizer(target: self, action: #selector(self.didTapOtherUserImage(_:)), otherUserModel: otherUserModel)
addGestureRecognizer(gestureRecognizer)
}
#objc internal func didTapOtherUserImage(_ recognizer: OtherUserProfileTapGestureRecognizer) {
Router.shared.gotoOtherUserProfile(otherUserModel: recognizer.otherUserModel)
}
}
Now use it like =>
self.userImageView.gotoOtherUserProfile(otherUserModel: OtherUserModel)
You can use an UIAction instead:
self.imageView.addAction(UIAction(identifier: UIAction.Identifier("imageClick")) { [weak self] action in
self?.handleTap(modelObj)
}, for: .touchUpInside)
that may be a terrible practice but I simply add whatever I want to restore to
button.restorationIdentifier = urlString
and
#objc func openRelatedFact(_ sender: Any) {
if let button = sender as? UIButton, let stringURL = factButton.restorationIdentifier, let url = URL(string: stringURL) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
}
}

Swift 4 - Creating a common function for multiple buttons

I'm wondering if there is a more efficient way to code an action that is the same with the exception of which button has been pressed and which item in a struct it relates to. Basically, I have a struct of 10 variables all of which are a boolean type and I have 10 buttons. When the user presses the button, I want to check whether it has already been pressed (using the struct) and then change the background of the button depending on the state and reverse the state. I've copied my current code for one of the buttons but thought I should be able to avoid doing this 10 times!
#IBAction func architectureButtonPressed(_ sender: Any) {
if myInterests.architecture {
myInterests.architecture = false
architectureButton.setBackgroundImage(imageUncheckedNarrow, for: .normal)
} else {
myInterests.architecture = true
architectureButton.setBackgroundImage(imageCheckedNarrow, for: .normal)
}
}
Well one simple way is to have each UIButton point to the same architectureButtonPressed IBAction method. Since the button that's pressed is passed into the method (sender) you can consult it's tag property to know the index of which field in your struct should be updated. (And then you might want to change your struct to just store an array of 10 bools, but up to you).
Then for each UIButton, whether programmatically in storyboard or nib, you'd assign the appropriate index value to the button's tag field.
Create yours IBOutlet for each button.
Create a array and store all buttons like : var arrayButtons : [UIButton] = []
arrayButtons.append[button1]
arrayButtons.append[button2]
...
Create a array of booleans to store true/false: var arrayBools : [Bool] = [] and initialize if some value.
Note that the indexes of the arrayButtons and arrayBools must be same related.
Create selector function to listen touch buttons.
button.addTarget(self, action: #selector(my_func), for: .touchUpInside)
#objc func my_func(_ sender : UIButton) {
for i in 0...arrayButtons.size-1 {
if arrayButtons[i] == sender {
if arrayBooleans[i] {
arrayBooleans[i] = false
arrayButtons[i].setImage()
} else {
arrayBooleans[i] = true
arrayButtons[i].setImage()
}
}
}
}
My suggestion is to manage the images in Interface Builder via State Config (Default/Selected)
Then assign an unique tag starting from 100 to each button and set the isSelected value in the IBAction to the corresponding struct member in a switch statement:
#IBAction func buttonPressed(_ sender: UIButton) {
switch sender.tag {
case 100: myInterests.architecture = sender.isSelected
case 101: myInterests.art = sender.isSelected
...
default: break
}
}
Alternatively use Swift's native KVC with WriteableKeypath
let keypaths : [WritableKeyPath<Interests,Bool>] = [\.architecture, \.art, \.fashion, \.history, \.localCulture, \.music, \.nature, \.shopping, \.sport, \.anything]
#IBAction func buttonPressed(_ sender: UIButton) {
let index = sender.tag - 100
let keypath = keypaths[index]
myInterests[keyPath: keypath] = sender.isSelected
}

Using NSPredicate to add enable users to 'Favourite' objects, which will then appear in a separate TableView (without CoreData!)

I currently have a tableView which is parsing and displaying data from a JSON file. When a cell is pressed, then the user is taken to a detailView screen to view further information.
I am now trying to set up a Favourite button within the detailView, so that users can press a button which will add the object to a favouriteView (this will be set up exactly the same as the tableView, but only show locations which have been favourited.
My understanding is that the best way to achieve this is to use NSPredicate. I have been looking around for hours and I can't seem to find a solution that does not use CoreData (although I have found a source which says that CoreData is not necessary). I would like to achieve the Favourite function without using CoreData.
I already have a favourite button set up in the detailView, which sets a variable isFavourite from false to true. Please see all the relevant code below.
Sample JSON object:
{
"locations": [
{
"id": "0001",
"name": "Helensburgh Tunnels",
"type": "Tunnels, Beach, Views",
"location": "Helensburgh, South Coast",
"image": "Helensburgh-Tunnels.jpg",
"activity": "Walking, photography, tunnelling",
"isVisited": false,
"isFavourite": false,
"latitude": -34.178985,
"longitude": 150.992867
}
]
}
The favourite button I have set up in the detailView:
#IBOutlet weak var favouriteButton: UIButton!
#IBAction func favouriteButtonTapped(_ sender: Any) {
self.location.isFavourite = self.location.isFavourite ? false : true
print(location.isFavourite)
// Favourite button
if location.isFavourite == false {
favouriteButton?.setImage(UIImage(named: "starLine.png"), for: .normal)
} else {
favouriteButton?.setImage(UIImage(named: "starFull.png"), for: .normal)
}
}
How can I take all the objects which have been set to isFavourite = true and display them in a tableView?
This is a section of the FavouriteLocationsViewController class, what do I need to fix here to make it functional?
class FavouriteLocationsViewController: UITableViewController {
var locations = [Location]()
override func viewDidLoad() {
super.viewDidLoad()
// Remove the title of the back button
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
if let locationJson = readLocation(){
if let locationArray = locationJson["locations"] as? [[String:Any]]{
for location in locationArray{
locations.append(Location.init(locationInfo: location))
}
print(locations.count)
self.tableView.reloadData()
}
}
}
// Filter to show only favourite locations
func filterArrayOfDictonaryWithKeyValueUsingPredicate(arrOfDict: NSArray, keyOfDict: String, strSearchText: String) -> NSArray{
let favouritePredicate = NSPredicate(format: "isFavourite == true")
let arrResult = arrOfDict.filtered(using: favouritePredicate)
return arrResult as NSArray
}
}
you want to show only favourite data there?
you have array of dictonary as per you are showing above.
you can filter that array using predicate.
and you can get only object which isFavourite == "True"
use below function pass array of Location, Pass keyofDict - isFavourite
strSearchText - for your example use true
func filterArrayOfDictonaryWithKeyValueUsingPredicate(arrOfDict: NSArray, keyOfDict: String, strSearchText: String) -> NSArray{
let searchPredicate = NSPredicate(format: "\(keyOfDict) CONTAINS[C] %#", strSearchText)
let arrResult = arrOfDict.filtered(using: searchPredicate)
return arrResult as NSArray
}

Delete Nsmenu item with specific tag

I use this function to create a NSMenuitems. They all get tagged with 2.
func addToComputerInfoMenu (title: String)
{
let addToComputerItem : NSMenuItem = NSMenuItem(title: "\(title)" , action: #selector(openWindow), keyEquivalent: "")
addToComputerItem.attributedTitle = NSAttributedString(string: "\(title)", attributes: [NSFontAttributeName: NSFont.systemFontOfSize(14), NSForegroundColorAttributeName: NSColor.blackColor()])
addToComputerItem.tag = 2
addToComputerItem.enabled = true
computerInfoMenu.addItem(addToComputerItem)
}
I would like to programmatically delete all items with "2" tag. I tried using .itemWithTag and .indexOfItemWithTag. I can't seem to iterate through the list.
let itemswithindex2 = computerInfoMenu.itemWithTag(2)
I found a way to accomplish my goal. Not sure its the best solution but it works.
for item in computerInfoMenu.itemArray
{
if (item.tag) == 2
{
computerInfoMenu.removeItem(item)
}
}

Contextual menu with search result for a NSSearchField

I would like to have a contextual menu that shows search results as text is entered in a search field. This is an
image of the default mail app in OS X that does this. I know how to filter an array of strings according to the search request of the user, but I do not know how to display it this way. I am using Swift and for a Cocoa application. Any help is appreciated.
Building from the previous answer, here is a simple Swift 3 class which you can use to automatically handle recent searches. You can add it as a custom class in your storyboard, or directly.
It will look like this:
import Cocoa
class RecentMenuSearchField: NSSearchField {
lazy var searchesMenu: NSMenu = {
let menu = NSMenu(title: "Recents")
let recentTitleItem = menu.addItem(withTitle: "Recent Searches", action: nil, keyEquivalent: "")
recentTitleItem.tag = Int(NSSearchFieldRecentsTitleMenuItemTag)
let placeholder = menu.addItem(withTitle: "Item", action: nil, keyEquivalent: "")
placeholder.tag = Int(NSSearchFieldRecentsMenuItemTag)
menu.addItem( NSMenuItem.separator() )
let clearItem = menu.addItem(withTitle: "Clear Menu", action: nil, keyEquivalent: "")
clearItem.tag = Int(NSSearchFieldClearRecentsMenuItemTag)
let emptyItem = menu.addItem(withTitle: "No Recent Searches", action: nil, keyEquivalent: "")
emptyItem.tag = Int(NSSearchFieldNoRecentsMenuItemTag)
return menu
}()
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
initialize()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initialize()
}
//create menu
private func initialize() {
self.searchMenuTemplate = searchesMenu
}
}
NSSearchField searchMenuTemplate (NSMenu) contains Menu Items (NSMenuItem) with specific tags used by the Search Field to populate the Menu.
The recentSearches Array here is just to pass additional String used in complement of Recents Search strings and is not required (I thought that it was to store recent search but no). NSSearchField also clear this Array when the user clears Recents Search.
You can also configure a Menu with category, more info here:
Configuring a Search Menu — Apple Developer
Example:
#IBOutlet weak var search: NSSearchField!
/// Array of string containing additional recents search (custom search)
var recentSearches = [String]()
/// Search Field Recents Menu
lazy var searchesMenu: NSMenu = {
let menu = NSMenu(title: "Recents")
let i1 = menu.addItem(withTitle: "Recents Search", action: nil, keyEquivalent: "")
i1.tag = Int(NSSearchFieldRecentsTitleMenuItemTag)
let i2 = menu.addItem(withTitle: "Item", action: nil, keyEquivalent: "")
i2.tag = Int(NSSearchFieldRecentsMenuItemTag)
let i3 = menu.addItem(withTitle: "Clear", action: nil, keyEquivalent: "")
i3.tag = Int(NSSearchFieldClearRecentsMenuItemTag)
let i4 = menu.addItem(withTitle: "No Recent Search", action: nil, keyEquivalent: "")
i4.tag = Int(NSSearchFieldNoRecentsMenuItemTag)
return menu
}()
override func viewDidLoad() {
super.viewDidLoad()
recentSearches = ["Toto","Titi","Tata"]
search.delegate = self
search.recentSearches = recentSearches
search.searchMenuTemplate = searchesMenu
}
You need to make an NSMenu Item with special tags that are placeholders so the search field knows where to put recent items, the clear action, etc.
Look at documentation for searchMenuTemplate, and the Tags : NSSearchFieldRecentsMenuItemTag etc.
Basically, make a contextual menu in IB. Drag your search field to use that menu as the searchMenuTemplate, and then populate menu items with the tags you want for clear, recent items, etc.