Why did calling [self startFirstponder] cause so many problems?

In the last few extremely unlucky days in my life, I have been trying to find out what is wrong with me. On a specific page, if I put UITextViews or UITextFields or MFMailComposer or MessageComposer or anything with fields requiring editing, the fields simply will not respond to clicks. I could not edit anything when I started the application. I could not edit text views or email fields or anything else. I tried everything, but nothing worked. It turns out that on the main page ( MainVC ), which leads to the page where the fields do not respond ( GiftVC ), in the viewDidAppear method (in MainVC ), I say: [self becomeFirstResponder]; ,

Now I'm not quite sure why I wrote this, but it turns out that commenting out this line eliminates everything and does all the fields and text comments and email composers, and everything works fine again.

I also have this on the MainVC page:

 -(BOOL)canBecomeFirstResponder { return YES; } 

and commenting on it also fixes the problem.

The strange part is that even with the [self becomeFirstResponder] everything worked perfectly in the new iOS 5 (simulator and device), but in iOS 4 (simulator and device), this line would not work with this. Now that I have deleted it, it works fine in both cases.

+4
source share
4 answers

Check if MainVC has a method called canResignFirstResponder that returns NO (at least sometimes). If so, then as soon as he becomes the first responder, he will not allow anyone else to become the first responder until he returns YES from this method. (All UITextViews, etc. Must be edited by the first responder.)

Actually just look everywhere in all your code for canResignFirstResponder if it is in a superclass or something like that.

Otherwise, the only thing that would stop editing text fields and views would probably be if they got set userInteractionEnabled = NO, but since it depended on the getFirstResponder operator, it would most likely be associated with canResignFirstResponder.

+3
source

If your subclass of UIViewController has the following:

 - (BOOL)canBecomeFirstResponder { return YES; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (self.view.window) { [self becomeFirstResponder]; } } 

then you probably intended to allow this subclass to handle motion events (shaking) or something similar. So thatโ€™s probably why it is there.

If you were unable to edit the UITextField , then this subclass probably became the first responder and did not redirect the event to the actual UITextField . When a subclass of UIViewController calls UIViewController overrides to return YES and makes it the first responder itself (ie [self becomeFirstResponder] , if you want this user class to not handle touch events for UITextField , you must override the nextResponder method.

An example from my own product. Essentially, I have a subclass of UIViewController that does two things: 1) it handles shaking events and 2) it displays a different view when some kind of button is used. UITextField there are several UITextField s. To allow my subclass of UIViewController forward touch events to my modal view, I added the following:

 - (UIResponder *)nextResponder { if (!self.view.window) { // If the modal view is being displayed, forward events to it. return self.modalViewController; } else { // Allow the superclass to handle event. return [super nextResponder]; } } 

This will work on iOS 4 and 5 using sdk.

Now, in your case, you obviously donโ€™t remember how to add code to become the first responder in the first place, so you do not need the aforementioned interceptors. However, this is good to know in the future.

Let's get back to your current question - as soon as you upgrade your SDK to 5, why not work on iOS 4, but they will work on iOS 5? iOS 5 does some event forwarding for you, so it works there. It should have never worked on iOS 4 at the beginning. Apple fixed some bugs that allowed it to work on 4, so it no longer works on 4.

I know that the question has already accepted the accepted answer; I just wanted to clear up any confusion.

+4
source

In iOS 4, the subclass must override canBecomeFirstResponder in order to be able to become the first responder. Maybe this is different for iOS 5 or it is a bug.

+2
source

Try this, make sure you add uiTextViewDelegate and

 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ NSLog(@"textViewShouldBeginEditing:"); return YES; } 
+1
source

All Articles