开发者

Correct (App Store Safe) way to resign a first responder?

What's the correct way to resign the current firstResponder?

I've seen the following: Looping through fields and calling resignFirstResponder on each.

[[self textFieldForRow:0] resignFirstResponder];
[[self textFieldForRow:1] resignFirstResponder];
[[self textFieldForRow:2] resignFirstResponder];
[[self textFi开发者_如何学GoeldForRow:3] resignFirstResponder];

And this which looks like it's calling a private function, is this app store safe?:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];   
[firstResponder resignFirstResponder];      

Is there a better way?

Thanks!

Comments: Looks like the second method is using a private api and someone's app has been rejected because of it: link


There's actually an API to do this:

[view endEditing:YES];

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/endEditing:


Have a look at this question: How do I [legally] get the current first responder on the screen on an iPhone?

My answer to that question should work perfectly in your situation.


The code you have up there is not calling any private function. What is happening is:

 //Gets the application window
 UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
 //Gets the first Responder View for the window
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];  
//Make the UView resign the first responder 
[firstResponder resignFirstResponder];     

All of those are public functions, so you will not have any problem with the app store as far as I know.

Additionally is there a better way to do what exactly?. If you are talking about getting the UIView that is the firstResponder to the keyWindow, you could do when creating your uiview:

myView.tag = 100;

and to get it:

UIView *firstResponder = [self viewWithTag:100];


My suggestion would be to implement a hacky & slow version of firstResponder yourself in a category and then file a radar requesting they add the method. Something along these lines:

- (id)my_FirstResponder
    for view in [self subviews]
        if [self isFirstResponder]
            return self
        return [self my_firstResponder]
    return nil

Using performSelector: to invoke a method not in the public headers is definitely grounds for rejection, so don't do that. Oh, and don't forget to use a prefix (like the above code does) when adding a category to avoid conflicts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜