Adding a UIMenuItem doesn't work - ios

I have a UICollectionView with two UITextView's in each cell and want to add a custom menu item, when text is selected. What I did in my CollectionViewCell-class:
override func awakeFromNib()
{
var menuController = UIMenuController.sharedMenuController()
var translateSelectionItem = UIMenuItem(title: "Translate", action: "translateSelection")
menuController.menuItems = NSArray(array: [translateSelectionItem])
}
But the item doesn't show up in the menu. Where is the problem ?

It takes more than creating the custom menu item. To make the custom menu item actually appear, the first responder must also return true from canPerformAction:withSender: for this action, and of course there must be an implementation of the action method as well.

Related

how to detect when user click lyft Button swift

We want Lyft button touch event because I am working in analytics, so, I need how many people choose Lyft but I can't put UIView click event. I try below code.
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
cell.lyftButton.addGestureRecognizer(gesture)
How can i achieve this?
You can directly assign a selector method to lyftButton e.g
lyftButton.addTarget(self, action: #selector(lyftButtonAction(_:)), for: .touchUpInside)
#objc
func lyftButtonAction(_sender: UIButton) {
//Do your action
}
To retrieve the LyftButton, you'll need to fetch the button inside the Lyft view, after retrieving it, I tried to add another target to it which was your 'checkAction' method, but for some reason it is not being called. One workaround solution is:
On Auto Layout, created a transparent button on top of the Lyft Button View, let's callet it 'Transparent Lyft Button': Example (I've embeded in another view because it was on a stackView);
On the code, retrieved the button with the above method, held it in a variable, let's call it 'requestLyftButton' and disabled it.
Created an IBAction for the 'Transparent Lyft Button' that triggers the method 'self.checkAction' that you've created and also calls requestLyftButton.sendActions(for: .touchUpInside), which triggers the original Lyft SDK action.
To Retrieve Lyft UIButton:
#IBOutlet weak var lyftButton: LyftButton!
#IBOutlet weak var transparentLyftButton: UIButton!
var requestLyftButton: UIButton?
func retrieveLyftButton(in view: UIView) {
for view in view.subviews {
if let lyftBtn = view as? UIButton {
lyftBtn.isEnabled = false
requestLyftButton = lyftBtn
} else {
retrieveLyftBtn(in: view)
}
}
}
transparentLyftButton IBAction to trigger your method + lyft sdk original action:
#IBAction func requestLyft(_ sender: UIButton) {
if let lyftBtn = requestLyftButton {
checkAction() // Your method
lyftBtn.sendActions(for: .touchUpInside)
}
}
I hope that you can understand what was done, if you have any questions, just let me know.

How to add more styling options to the UIMenuController of UITextView?

When UITextView's allowsEditingTextAttributes property is enabled,
textView.allowsEditingTextAttributes = true
the textview can show the BIU (bold/italic/underlined) styling options in the context menu by UIMenuController.
UIMenuController - BIU Styling Options #1
UIMenuController - BIU Styling Options #2
I wonder how to add more styling options (e.g., strikethrough, highlight) to the context menu inside the BIU. For example, the iOS' native Notes app has four options (BIU + strikethrough) inside the styling menu.
BIU Styling Options in the native Notes app
Is there any way to do it? I have been spending hours to find a way to override the "Selector(("_showTextStyleOptions:"))" but couldn't find out how.. Please help me!!
When the editing menu is about to become visible, you get a canPerformAction(_:withSender:) call in your UITextView. This method is called again when the user selects a button within the menu. You can check if the font style button was selected and add a custom button to that submenu.
class MyTextView: UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let menuController = UIMenuController.shared
if var menuItems = menuController.menuItems,
(menuItems.map { $0.action }).elementsEqual([.toggleBoldface, .toggleItalics, .toggleUnderline]) {
// The font style menu is about to become visible
// Add a new menu item for strikethrough style
menuItems.append(UIMenuItem(title: "Strikethrough", action: .toggleStrikethrough))
menuController.menuItems = menuItems
}
return super.canPerformAction(action, withSender: sender)
}
#objc func toggleStrikethrough(_ sender: Any?) {
print("Strikethrough button was pressed")
}
}
fileprivate extension Selector {
static let toggleBoldface = #selector(MyTextView.toggleBoldface(_:))
static let toggleItalics = #selector(MyTextView.toggleItalics(_:))
static let toggleUnderline = #selector(MyTextView.toggleUnderline(_:))
static let toggleStrikethrough = #selector(MyTextView.toggleStrikethrough(_:))
}
According to documentation, you may have to call update() on the UIMenuController after adding the button. But this was not necessary in my case.

Remove items from UIMenuController using Swift

I want to know how to do two things:
Remove the UIMenuController entirely. I want the text in WKWebView to be selected, but the UIMenuController is causing issues when the long press is released.
Add a new items to the list of menu items, and remove others.
Right now I can add an item to the end of the list, but nothing else, using the following code.
func enableCustomMenu() {
let lookup = UIMenuItem(title: "Add comment", action: "addComment")
UIMenuController.sharedMenuController().menuItems = [lookup]
}
func disableCustomMenu() {
UIMenuController.sharedMenuController().menuItems = nil
}
func addComment() {
//let text = self.webView.("window.getSelection().toString();")
print("Add Comment")
}

Swift- How to get label value in custom table view cell on button click

I'm very new to Swift and Xcode so this question may sound elementary to some.
I have a button and a label in a custom table view cell. I'm trying to change the value of the label text when the button click is detected.
Currently, I'm setting the button.tag = indexPath.row (as shown in the code below) and I'm catching it in the btn_click function.
cell.btn.tag = indexPath.row
cell.btn.addTarget(self, action: "btn_click:", forControlEvents: UIControlEvents.TouchUpInside)
How can I access the label contained within the same cell as the button clicked so I can change the value of the label text? Can I use indexPath.row to return the correct label object?
You don't need to use the tag to accomplish your goal, in your CustomTableViewCell class you have to set an action to the button and a outlet to the label inside the cell, something like this :
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
// action to tap the button and change the label text
#IBAction func tappedButton(sender: AnyObject) {
self.label.text = "Just Clicked"
}
}
You can the manually set the action for the button and the outlet for the label in the Interface Builder, you don't need to set in code.
Then, when you make a tap inside the button in the cell the label it's changed only inside the cell.
I hope this help you.

Create checkmark for a Table View with a detail segue?

I would like to create an app with a list of TODO items. Each TODO should have a functional checkmark/checkbox on the left side of the cell. If I click on the right side of the TODO then I would like to show its details.
I have create a Table View. When I click on a cell I follow a segue to a UIViewController that shows the details of the cell.
I think displaying the checkmark may be possible by defining the cell's imageView. However, I can't make the imageView functional, i.e., clickable. Every time I click on it it simply shows the details.
Can you provide basic sample code to show how I can have both a segue to the details of the cell AND a checkmark that is clickable?
Thanks, Daniel
OK, I figured it out. Added this code snippet:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
//...
let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapped"))
singleTap.numberOfTapsRequired = 1
cell.imageView?.userInteractionEnabled = true
cell.imageView?.addGestureRecognizer(singleTap)
//...
}
func tapped() {
println("tapped!")
}

Resources