I have created a custom subclass of UITextField, CustomTextView.
I created a
private var tapGesture = UITapGestureRecognizer()
in the customTextView class
In the initInView, I have the following code
tapGesture.addTarget(self, action: "tapTextField:")
self.addGestureRecognizer(tapGestureRecognizer)
CustomTextView implements UIGestureRecognizerDelgate
CustomTextView has a private func named tapTextField
Now when I use iOS simulator and click on the text field, the function tapTextField never gets called.
What am I missing here? I saw similar posts but none of them answer my question. I could not comment on those as I don't have reputation yet. So asking as a new question.
PS: Does this have to do with firstResponder being set? This is someone else's code I am working on, so I might have missed something. Let me know what I should look for
Related stack overflow questions:
Add UITapGestureRecognizer to UITextView without blocking textView touches
UITextview: Place focus on becomefirstresponder
I don't know swift, but try changing this "tapTextField:" to this "tapTextField" and make sure you don't have any arguments/parameters/whatever swift calls them in your "tapTextField" function.
Also, it looks like
self.addGestureRecognizer(tapGestureRecognizer) should be self.addGestureRecognizer(tapGesture)
You can try few things to debug the problem.
1> Make sure there is no mistake of tapTextField and tapTextField: i.e. you are adding selector with argument but you have implemented same method without any argument.
2> Make sure no any other transparent view obscuring your custom uitextfield.
3> print po yourTextFieldName in the xcode console to see whether actually your textfield has any gesture recognizer added in it or not.
Hope this helps
Related
I am relatively new to iOS development with Swift (I actually have 3 years of experience with Android development with Java, trying to learn a new technology). I am creating an app that requires the usage of a library known as SearchTextField:
https://github.com/apasccon/SearchTextField
In a shellnut, it's a UITextField subclass that has a dropdown suggestions/autocomplete functionality.
Below is the ViewController that uses it...
#IBOutlet var homeAddressTextField: SearchTextField!
#IBOutlet var workAddressTextField: SearchTextField!
override func viewDidLoad() {
super.viewDidLoad()
homeAddressTextField.delegate = self
workAddressTextField.delegate = self
homeAddressTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
workAddressTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
//vvvvvvvvv EXC_BAD_ACCESS CODE 2 THROWN BELOW vvvvvvvv
homeAddressTextField.filterStrings(["foo","bar"])
}
homeAddressTextField should be instantiated, otherwise any reference to it above should throw the same exception. When breakpointing into the problematic line, homeAddressTextField is NOT nil, and correctly shows that it is an instance of SearchTextField.
I have tried many things to fix or at least find the source of the error. As you can tell, I used a strong var instead of weak var for the Outlet.
I have tried using zombies to track any attempt to access a deallocated memory block, yet the zombie Instruments came up with no zombies accessed.
If it is worth noting, the error disappears as soon as the problematic line containing filterStrings() is removed. Any help is appreciated!
It seems bug in library, could you please check here
SearchTextField Issue
It is in still open issues at their repository.
Kindly watch issues in repository, if you try to use someone readymade code.
Are you sure you've attached your IBOutlet in interface builder?
Try putting a breakpoint on the line that's crashing. That will stop it right before executing that line. Then, in the console (command+shift+y) you'll see a line that says "lldb" - put your cursor there and type po homeAddressTextField and see if it returns a value, or nil. If nil, then the IBOutlet is not set properly, which would cause bad access.
Additionally, if it is indeed nil, you'll want to make sure that the subclass and module are both set within interface builder on the SearchTextField, as well as making sure to set the outlet itself. You can also try filtering these strings in the viewDidAppear() method just to see if it is indeed an issue with the reference to the SearchTextField.
Edit: I've looked through the code a bit of the repo. You might not want to set the datasource and delegate properties, as the SearchTextField has a datasource and delegate of its own. You simply need to set the filterable strings as you are on the last line. So try removing the calls to make the view controller the datasource/delegate.
Clicking the textField and then adding class name = SearchTextField.swift worked for me.
In my project, I needed to toggle a save button whenever the text in 2 of my textFields changed, so I instinctively looked into the methods of UITextFieldDelegate, since I was already using it.
I learned that there is no method for doing this, the closest is shouldChangeCharactersInRange, which does not solve my problem (technically it does, but it's way more complex than it needs to be).
I found this answer, where the edited part says to create an IBAction for each textField's editingChanged.
In UITextFieldDelegate, there are functions for shouldBegin/didBegin, and shouldEnd/didEnd. Why is there only a function for shouldChange and not didChange?
I'm stuck in this situation where I cannot link any UITextView delegate methods to my code, because they just don't show up in IB. That's all I've got :
I guess every delegate method should appear here (editingDidEnd, etc.) ?
UITextField delegates work fine though.
I also tried to create a #IBAction func textViewDidEndEditing(textView: UITextView) {} function manually, but I can't drag the UITextView to that function either.
Does someone know what is going on ?
Update: with 2 downvote of this question, I'd like to make this question a little bit useful to others - since I don't have the choice to delete it. The mistake I made was cut and paste codes which has interface outlet. As I was completely new at that time, I was assuming that when I copy and paste, the outlet link will be copied and pasted as well. Obviously it doesn't work that way.
I was writing a single-viewed app. It has one UITextField and one MKMapView. I want to do something when Return key is hit, so I basically followed
How to hide keyboard in swift on pressing return key?
But it does not fit well with my other codes. Any idea why it isn't working and how to fix it?
Make sure you connect your UITextField from StoryBoard to your searchText IBOutlet by control-dragging from the StoryBoard to the the searchText variable.
You have your outlet set up as implicitly unwrapped. That's the correct thing to do, but when your code executes the outlet must not be nil or your code will crash.
You probabably have a broken outlet link. Set a breakpoint and examine the outlet.
You can change your code to use an "if let" expression to prevent crashes. Search in the Swift language reference IBook for "Optional binding" to learn about it.
Edit:
The code might look like this:
if let requiredSerachText = searchtext
{
requiredSearchText.delegate = self
}
I am working on an iOS SDK 4 project with ARC enabled.
My class MyTextView (derived from UITextView with UITextViewDelegate protocol) implements the following static method:
+ (void)showInViewController:(UIViewController*)viewController
{
MyTextView *textEdit = [[MyTextView alloc] init];
textEdit.delegate = textEdit;
[viewController.view addSubview:textEdit];
// Show the keyboard
[textEdit becomeFirstResponder];
}
In one of my view controllers I call the following:
[MyTextView showInViewController:self]
This crashes with warning: Unable to restore previously selected frame. on becomeFirstResponder. Looks like some stack related crash because of some cycle. I am fairly new to ARC. The delegate property of UITextView is defined as assign (shouldn't ARC interpret that as weak?). I know this approach is rather strange memory-wise. However, I wanted to know if ARC can handle things like that. Obviously it can't. Any idea what might be the problem and how to solve it?
I don't think it has anything to do with the ARC and memory management, but just a more fundamental problem that a UITextView cannot be a delegate of itself. It gets locked in a loop. Put a logging message in textViewDidChangeSelection and you'll see it gets repeatedly invoked. Not a memory issue, methinks, but rather just a logic issue with UITextView delegates. Even if you don't do your problematic showInViewController but just create a standard UITextView subclass and try to set its delegate to itself, you'll see the same curious behavior.
old post, but here is the answer:
http://www.cocoabuilder.com/archive/cocoa/282093-uitextview-as-its-own-delegate-infinite-loop-on-keyboard-select.html
or here aswell
self.delegate = self; what's wrong in doing that?