I want to replace the default keyboard of a UITextField with a custom keyboard. So I created a new subclass of a UIViewController with a xib-file (the other way like creating both files seperately and setting the File's Owner doesn't work either).
Then I added a button to the KeyboardView and connected it to an IBAction. After that I set the textfields inputView to the new view like this:
override func viewDidLoad() {
let keyboardVC = KeyboardViewController()
textField.inputView = keyboardVC.view
keyboardVC.view.autoresizingMask = .FlexibleHeight
keyboardVC.delegate = textField
}
It's working and the custom keyboard shows up, but if I touch the button, the IBAction is not called. What's the problem in my setup? (I checked some examples and they all do it the same way).
UPDATE:
I now removed the ViewController and subclassed the UIView. Now the actions are working. Why isn't it working with a ViewController?
Since no one holds the UIViewController- there is no reference to it after the viewDidLoad() ended, it is released from the memory.
When the button is pressed, the view controller that should response to the action is not exist -> you are holding only the view of the view controller as the textField.inputView.
Related
I have created a .xib file for a view that is going to be repetitive in my iOS app, inside of which there is a UIButton.
I have included that .xib view inside multiple UIViewControllers in my storyboard. I would like to link an #IBAction and an #IBOutlet to the button inside my .xib view that is specific to each UIViewController. In other words, I want every UIViewController to completely manage and handle the UIButton that is inside the .xib view.
Any idea if the above is feasible?
There are a couple of ways to do what you want.
The way I would do it is to give your custom view a closure that is run when the IBAction method is triggered. And each view controller that loads the view from the xib can pass in the closure to the view and the action will run when the button is clicked.
So here's the best solution I came up with so far.
Inside my .xib, I link the button to an #IBAction that is empty.
Again inside my .xib, I created a protocol with a single method that I will call inside the #IBAction created in step (1.)
The #IBAction will run the protocol method every time it is called, so every time the button is clicked.
Implement the protocol stub in every ViewController that needs to handle the #IBAction, and make sure to link that ViewController to the .xib using the protocol created in step (2.)
In your ViewController witch contains the xib view, just asign an action to the button inside xib view
class YourViewController:UIViewController{
override func viewDidLoad(){
//ListTitleView : a xib view, action button witch called theManageButton is inside it.
let theListTitleView = ListTitleView.init(frame:CGRect.init(x: 0, y: 0, width: self.view.frame.width, height: 100))
//Add action to theManageButton
theListTitleView.theManageButton.addTarget(self, action: #selector(yourFunction(Sender:)), for: .touchUpInside)
}
func yourFunction(Sender:UIButton){
//...Do something here
}
}
Good evening,
I'm wondering if displaying a nib file as a subview is the more standardized way of displaying a subview when compared to hiding and unhiding a view of the same class.
Also,
How would i be able to set an action on the buttons in the nib file?
let test = xWork.loadViewFromNib()
test.center = view.center
self.view.addSubview(test)
Currently doing the above. Even though my xWork nib has a class with an outlet for a button, i'm unsure as to how to set an action to it.
Thank you
Please clarify your first question. As for your second question, how to set the action of a button in a nib, try this:
In your nib, create a callback closure:
var onClickCallback: (Void -> Void)?
and in your button's IBAction, call the closure
#IBAction func buttonAction(sender: IBAction) {
onClickBallback?()
}
Then, where you use your nib, set the button callback:
let test = xWork.loadViewFromNib()
test.center = view.center
self.view.addSubview(test)
test.onClickCallback = {
print("Button clicked!")
// Try this to dismiss the view.
test.removeFromSuperview()
}
If your nib file owner is its super view(or view controller),just CTRL+DRAG an target-action.
I have a view in a xib file that contains a button and other 2 views and they don't overlap. The view file's owner is a custom class derived from UIViewController. The custom view controller is created programmatically by this code:
let sfvc = SelezioneFascicoloViewController(nibName: "SelezioneFascicoloView", bundle: nil)
viewObject.addSubview(sfvc.view)
sfvc.updateWithAttributes(attrs, inFascicolo: self.fascicolo!)
viewObject is a view contained in a custom UICollectionViewCell.
The button has a handler connected to the touchDown event (but any other event produce the same problem) and it is defined in the custom UIViewController (SelezioneFascicoloViewController)
I have flagged showsTouchOnHighlight for the button just to see if it receive the event.
Well, the view shows up correctly, if I tap the button I can see the glow but the handler isn't called.
All the views in the hierarchy have the UserInteractionEnabled set to true, and their sizes are correct so the button isn't outside the superview frame.
Also I have a UITapGestureRecognizer in the UICollectionViewController derived class that contains the UICollectionViewCell, and its handler is fired if I tap outside the button, but not when I tap inside the button, and this is as it should be.
Can someone give me some advice for this kind of problem?
Edit: added a screenshot
Interface Builder screen shot
Thank beyowulf.
Everything works if I add SelezioneFascicoloViewController as a child view controller, in this way:
let sfvc = SelezioneFascicoloViewController(nibName: "SelezioneFascicoloView", bundle: nil)
self.addChildViewController(sfvc)
viewObject.addSubview(sfvc.view)
sfvc.didMoveToParentViewController(self)
sfvc.updateWithAttributes(attrs, inFascicolo: self.fascicolo!)
The headline seems lengthy but what I'm trying to do is quite simple.
I have a couple of identical buttons lined in a row and set their tags to 0...n. Clicking on any of them (the 2nd for example) would bring up a popover view in which there are several buttons representing different options (A, B, C, D). What I want to do is to turn the 2nd Button's title to B if we click on option B.
The problem is that the popover view does not know which Button presented it, since all popoverViews are instances of the same UIViewController class. So I am thinking of distinguishing the n buttons by setting their tags to different values. However, I don't know how to get the UIButton's tag from a button inside the popover this UIButton presented.
Many thanks in advance!
This is how I will solve this in swift. I will declare a delegate in the popoverViewController and have a method e.g
protocol popOverViewControllerDelegate: class {
func popOverViewController(controller: PopOverViewController,
didSelectItem buttonTitle: String)
}
then I will add a target action to the UIButton in the popOver
button.addTarget(self, action: "selected:",forControlEvents:.TouchUpInside)
the select method will have sender passed to it
func selected(sender:UIButton){
delegate?.popOverViewController(self, didSelectItem: sender.currentTitle)
//dismiss viewcontroller
}
remember to declare
weak var delegate: popOverViewControllerDelegate!
now have the viewcontroller that called the popOver, subclass to this delegate and implement it's method. Whenever a button in the popOver is selected, this method will be called and the currentTitle of the method will be passed. You can now use that to set the currentTitle of the button that called the popOver.
In case you need further help with the last part, please ask.
I've fixed the problem by adding a property tagNumberso that after instantiating the popoverViewController's class, I set the instance's tagNumber to the sender's tag. Then I send the tagNumber back together with sender.currentTitle. This way, the presenter of the popover could know both the title and tag number of the UIButton.
So I basically have a form, consisting of several text fields. The user types into the fields as usual. But the user also has the option of double-tapping a text field, which presents a modal view controller, allowing the user to choose from a number of options relating to that field.
Can I somehow present the modal "over" the keyboard, such that when it is dismissed, the keyboard is still active for the field that had been first responder before I presented the modal?
Right now, the keyboard dismisses while the modal appears, and reappears as the modal is dismissed. It looks clunky to me, and distracting. Would love to streamline it, and reduce the amount of animation onscreen.
Edit: I've updated this answer for iOS 12 and Swift. The revised example project (containing new Swift and updated Objective-C implementations) is here.
You can create a new UIWindow and place that over the default window while hiding the keyboard's window.
I have an example project on Github here, but the basic process is below.
Create a new UIViewController class for your modal view. I called mine OverlayViewController. Set up the corresponding view as you wish. Per your question you need to pass back some options, so I made a delegate protocol OverlayViewController and will make the primary window's root view controller (class ViewController) our delegate.
protocol OverlayViewControllerDelegate: class {
func optionChosen(option: YourOptionsEnum)
}
Add some supporting properties to our original view controller.
class ViewController: UIViewController {
/// The text field that responds to a double-tap.
#IBOutlet private weak var firstField: UITextField!
/// A simple label that shows we received a message back from the overlay.
#IBOutlet private weak var label: UILabel!
/// The window that will appear over our existing one.
private var overlayWindow: UIWindow?
Add a UITapGestureRecognizer to your UITextField.
override func viewDidLoad() {
super.viewDidLoad()
// Set up gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTapRecognizer.numberOfTapsRequired = 2
doubleTapRecognizer.delegate = self
firstField.addGestureRecognizer(doubleTapRecognizer)
firstField.becomeFirstResponder()
}
UITextField has a built-in gesture recognizer, so we need to allow multiple UIGestureRecognizers to operate simultaneously.
extension ViewController: UIGestureRecognizerDelegate {
// Our gesture recognizer clashes with UITextField's.
// Need to allow both to work simultaneously.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
This is the interesting part. When the gesture recognizer is triggered, create the new UIWindow, assign your OverlayViewController as the root view controller, and show it. Note that we set the window level to UIWindowLevelAlert so it will appear in front. However, the keyboard will still be in front despite the alert window level, so we have to manually hide its window, too.
It is important to not set the new UIWindow as key or to change the first responder from the UITextField or the keyboard will be dismissed.
Previously (before iOS 10?) we could get away with overlayWindow.makeKeyAndVisible(), but now setting it as key will dismiss the keyboard. Also, the keyboard's window now has a non-standard UIWindow.Level value that is in front of every publicly defined value. I've worked around that by finding the keyboard's window in the hierarchy and hiding it instead.
#objc func handleDoubleTap() {
// Prepare the overlay window
guard let overlayFrame = view?.window?.frame else { return }
overlayWindow = UIWindow(frame: overlayFrame)
overlayWindow?.windowLevel = .alert
let overlayVC = OverlayViewController.init(nibName: "OverlayViewController", bundle: nil)
overlayWindow?.rootViewController = overlayVC
overlayVC.delegate = self
// The keyboard's window always appears to be the last in the hierarchy.
let keyboardWindow = UIApplication.shared.windows.last
keyboardWindow?.isHidden = true
}
The overlay window is now the original window. The user can now select whatever options you built into the overlay view. After your user selects an option, your delegate should take whatever action you intend and then dismiss the overlay window and show the keyboard again.
func optionChosen(option: YourOptionsEnum) {
// Your code goes here. Take action based on the option chosen.
// ...
// Dismiss the overlay and show the keyboard
overlayWindow = nil;
UIApplication.shared.windows.last?.isHidden = false
}
The overlay window should disappear, and your original window should appear with the keyboard in the same position as before.
I can't try this right now, but have implemented similar for other purposes. In the action for presenting the modal controller, I assume gesture recognizer or delegate method, first take a screenshot and place it in an imageView over the current subviews. Later when returning, simply remove the imageView.
Might sound crazy but I remember having done this for a transition where the keyboard moving during the transition caused similar clunky behavior. It was not difficult to implement at all.
If you have trouble trying it, perhaps someone will provide some code. I can reference my own work later and add an example, but not now.
#Rob Bajorek's answer is excellent.
For iOS 9,10 there are small changes.
Instead of the code:
[self.overlayWindow setWindowLevel:UIWindowLevelAlert];
[self.overlayWindow makeKeyAndVisible];
Put the following code:
NSArray *windows = [[UIApplication sharedApplication] windows];
UIWindow *lastWindow = (UIWindow *)[windows lastObject];
[self.overlayWindow setWindowLevel:lastWindow.windowLevel + 1];
[self.overlayWindow setHidden:NO];
In order to keyboard to visible any of text accepting fields such UITextField or UITextView or UISearchBar should be the first responder and they should be visible in the view. Meaning responding view should be in the top level hierarchy in the window.
If you don't need this effect, Instead of presenting a ViewController you can add ViewController.view as a subview of your self.view with animation.
You have access to the frame of the keyboard in iOS.
You need to implement code to listen to the keyboard notifications (like UIKeyboardWillShowNotification and UIKeyboardWillChangeFrameNotification). The notification will send you informations about the frame of the keyboard.
Giva a look to the description of the "Keyboard Notification User Info Keys" in the windows reference.
You'll find useful for you purpose:
UIKeyboardBoundsUserInfoKey The key for an NSValue object containing a CGRect that identifies the bounds rectangle of the
keyboard in window coordinates. This value is sufficient for obtaining
the size of the keyboard. If you want to get the origin of the
keyboard on the screen (before or after animation) use the values
obtained from the user info dictionary through the
UIKeyboardCenterBeginUserInfoKey or UIKeyboardCenterEndUserInfoKey
constants.
With the information of the keyboard frame you can show there you modal view.
Just add an tap gesture in your textfield and a UITextfield *flagTextfield;
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(DoubleTapMethod:)];
doubleTap.numberOfTapsRequired = 2;
[self.txtTest addGestureRecognizer:doubleTap];
-(void)DoubleTapMethod:(UITapGestureRecognizer *)gesture
{
[flagTextfield resignFirstResponder];
NSLog(#"DoubleTap detected");
//Set your logic on double tap of Textfield...
//presents a modal view controller
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
flagTextfield = textfield;
}