Override func of UIBarButtonItem used as an extension - swift

In a collectionView, I'm using several textFields with different issues. All have an "extended keyboard" with custom UIBarButtons. As I need to reuse it a lot, I solved my problem with using an extension, quite similar to this one : https://stackoverflow.com/a/31010668/8162027.
My buttons are always the same: "cancel" and "Done".
But the func linked must return different things. Is there a way to override those #objc func for these 2 buttons donePressed() and cancelPressed() from my UIcollectionView?
Here is a screenshot of my buttons:
https://imgur.com/BvXrwMh

Related

How to remove the option window for selected text in textfield, Swift

When text is selected/marked in my app this window pops up with different options if the user would want to copy/cut out etc, but I want this window not to be able to pop up at all. Anyone knows how to achieve this?
Thanks in advance, Pontus
You must create a subclass of your UITexField or UITextView (or UILabel) depending on which one you want to disable the UIMenu.
Then override:
func canPerformAction(_ action: Selector,
withSender sender: Any?) -> Bool
And return no for the selectors you want to disable.
If you prefer to disable selection of text in a UITextView, you can disable the selection behavior.

Is NSMenu willClose notitfication possible?

Is it possible for an NSMenu object to notify BEFORE it'll close, not after? Its delegate has method didClose(_:) but I want to update its items before it actually closes, since the disappearing animation is too long and the eye can see the change.
I've tried to monitor NSEvents, but it's useless because NSMenu hasn't public property containing its NSWindow object.
It's theoretically possible to achieve by creating a custom NSViews for each menu items. But I don't like this because then I'll have to draw all the drawing of the items, including selection and click animation.
UPDATE:
I've tried to subclass the NSPopUpButton to track menu updates:
class CustomPopUpButton: NSPopUpButton {
var isMenuShown: Bool = false
var onClosingMenu: ((NSMenu)->())?
override var needsDisplay: Bool {
willSet {
if let menu = self.menu, isMenuShown, newValue {
onClosingMenu?(menu)
isMenuShown = false
}
}
}
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
isMenuShown = true
super.willOpenMenu(menu, with: event)
}
}
I'm not proud of that piece of code but it works in general. Yet the 'onClosingMenu' method is being called just after the menu closing animation is finished. Not before.
Video of what I want to achieve: https://drive.google.com/file/d/1GAceKp-fTlurxSybdB3h0epVZrtQjthm/view?usp=sharing
Finally, I've found the solution. No need to fight the system and update something before the button menu closes. I've found the another way and it's pretty simple.
I've subclassed the NSPopUpButton and created another NSMenu in the subclass, called 'attributedMenu'. Overrided all properties of the NSPopUpButton that deal with menu items (insertions and removals) and redirected that actions to the 'attributedMenu' property.
The initial menu property of NSPopUpButton I'm using only for selected items, removing non-selected items right away.
I intercept the click on the button to show 'attributedMenu', not the default menu of the class.
That solution even made possible to display 'multiple values' title if I select more than one element. Like in Apple Pages' font picker when you select text written with multiple fonts. All it takes is to add an NSMenuItem with title 'Multiple Values' and call super to select it.
That's it, now it works as perfect as in Apple Pages font picker button. As long, as I'm not touching the original 'menu' property of NSPopUpButton class.
UPDATE
Uploaded the subclass to GitHub: https://github.com/CineDev/AttributedPopUpButton

CollectionViewCell's IBoutlet is nil in Custom Keyboard Extension

Well, it'd day 5, I have tried everything to make this work. Setup is simple
I am trying to write a custom keyboard.
Which is having the CollectionView, CollectionViewCell is powered by xib
I have one label, which would hold the emoji when running.
I did ready every single article here where IBOutlet is nil.
Nothing is pointing to the direction that I haven't tried.
class CategoryCollectionCell: UICollectionViewCell {
#IBOutlet weak var emojiLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
Outlet is connected to the class.
KeyboardViewController is CollectionViewData source and delegate.
I am able to write the same code and UIViewController is able to get the collectionView Cells and showing the emojis.
I have run out of options. So I am posting here.
There is nothing extraordinary that I am doing here. Except its a keyboard extension.
Also, SIGQUIT is the biggest problem, any suggestions what is causing that. as I haven't able to put a finger on it.

How to hide keyboard without animation in swift 2?

I have a segmented control that switches between 3 views, one of which is a chat. I have an #IBAction set up to that segmented control for when the index (tab selected) changes. Since I want the keyboard to go down, the first thing I do is call my dismissKeyboard function:
func dismissKeyboard() {
view.endEditing(true)
}
The problem is this method has a sliding down animation, I would want it to just disappear. Looking for a Swift 2 solution
You can do
UIView.performWithoutAnimation {
self.view.endEditing(true)
}
Looks like it's not possible in Swift 2 or 3 to dismiss a keyboard without the slide down animation.
resignFirstResponder doesn't take any arguments.

How is iPhone Contact app's detail View implemented

I would like to implement a view similar to the detail view of Apple's own Contacts app where it displays the name, phone number, note, etc. and its Edit mode.
Can you dissect how the whole view is done? Is that view done with a UITableView or a UIScrollView?
The contact details screen is actually quite simple to mimic.
Start with a UITableView and provide it with a UITableViewDataSource and UITableViewDelegate. You'll need to provide sections for all the data you want to present. This means 1 for the custom header, 1 for the custom footer (buttons / actions), and approximately 6 or so sections for the data (one section for phone numbers, another for e-mail addresses, and so on)
Inside of each section, a number of rows need to be provided from your datasource to indicate how much data there is for that section. For each row, a UITableViewCell can be used to display the actual contact data (fax label / fax number value, etc). You can get fancy if you like, but there doesn't seem to be a need. For things like ringtone you'll need to specify a disclosure indicator.
For the header you'll need a UIImageView and a UILabel, for the footer you'll need a few UIButtons. You can create a child of UITableViewCell in InterfaceBuilder with these views inside of it and wire it up like anything else. You can use NSBundle to load views from other xibs that are not already loaded.
An alternative is to dynamically generate the UI widgets at runtime with no xibs. It all depends on what you would rather manage (code or xibs), to me it seems about the same amount of effort either way. I highly recommend reading through the table view programming guide if you haven't already.
Or you could use Apple's own ABPersonViewController:
http://developer.apple.com/library/ios/#documentation/AddressBookUI/Reference/ABPersonViewController_Class/Reference/Reference.html
The allowsEditing property specifies whether the user can edit the person’s information.
My implementation uses a UITableView with custom header (for the "Add Photo" and edit name equivalents) and a custom footer (using the UISegmentedControl hack for a big button) for the "Delete" equivalent.
You can use F-Script for exploring this. Here's a screenshot from the F-Script browser while browsing Address Book. Basically, it looks like a lot of custom views which all inherit from NSView.
To do this yourself:
Download F-Script from the link above
Follow the instructions in the extras/F-Script Anywhere directory for attaching to Address Book
Choose F-Script -> Open Object Browser from the Address Book menu
Click Select View
Highlight the Address Book View you want to explore and click it.
Navigate around to your heart's content.
Just to show you the way, you can subclass UITableViewController for that purpose and then in order to implement the Edit mode similar to the Contacts app you would:
Add a property to store a reference to Cancel button.
var cancelButton: UIBarButtonItem!
In ViewDidLoad(), add edit button to the navigation bar as a right item and prepare Cancel button to later add it as a left item.
self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelPressed:")
Override setEditing(_:animated:) method to set up your cells for Edit/Preview mode and show/hide a Cancel button on the navigation bar, based on the editing flag.
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)
if editing {
// Set up cells and prepare for Edit mode here
self.navigationItem.setLeftBarButtonItem(self.cancelButton, animated: true)
} else {
// Set up cells and prepare for Preview mode here
self.navigationItem.setLeftBarButtonItem(nil, animated: true)
}
}
Override UITableViewDelegate's tableView(_:editingStyleForRowAtIndexPath:) and tableView(_:shouldIndentWhileEditingRowAtIndexPath:) methods to configure row styles and indentation when in Edit mode.
Implement cancelPressed method to exit Edit mode when Cancel is pressed.
func cancelPressed(button: UIBarButtonItem) {
self.setEditing(false, animated: true)
}
I know the question is pretty old, but somebody might find it helpful.