resignfirstresponder崩溃我的ios应用程序

时间:2012-02-27 18:03:53

标签: iphone ios keyboard

我是IOS的新手,似乎无法使用它,我的应用程序上有一个输入文件,每当用户按下返回或相关按钮(searchGo)时我都想隐藏键盘< / p>

以下是我的代码:

mainViewController.h

 @interface kepnMainViewController : UIViewController <kepnFlipsideViewControllerDelegate, MKMapViewDelegate>
{
    MKMapView *_mapView;
    IBOutlet UITextField *searchBox;
    IBOutlet UIBarButtonItem *searchGo; 
    IBOutlet UIBarButtonItem *searchNearby;
    MKAnnotationView *annotationView;
}
@property (strong, nonatomic) MKMapView *_mapView;
@property (strong, nonatomic) MapAnnotation *annotation;
@property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
@property (strong, nonatomic) MKAnnotationView *annotationView;
@property (strong, nonatomic) UIBarButtonItem *searchGo; 



- (IBAction)showInfo:(id)sender;
- (IBAction)searchGo:(id)sender;
- (IBAction)showNearby:(id)sender;
- (IBAction)searchBoxReturn:(id)sender;

- (void) setPlaceMarker: (CLLocationCoordinate2D) coord :(NSString*) title :(NSString*) subtitle; 
@end

适当的.m片段

-(IBAction)searchGo:(id)sender
{
    NSLog(@"sender object %@",sender); 


         [sender resignFirstResponder];


    NSLog(@"search button pressed and textbox = %@",searchBox.text); 


}



-(IBAction)searchBoxReturn:(id)sender
{
    NSLog(@"search box return "); 

    [sender resignFirstResponder];

}

对不起,如果这是一个愚蠢的问题,但我做错了什么。??

3 个答案:

答案 0 :(得分:2)

UIBarButtonItem不是UIView,因此绝对不是UIResponder。相反,它是NSObject的子类,不响应- (void)resignFirstResponder

(解决方案:删除[sender resignFirstResponder];行)

答案 1 :(得分:1)

因此,如果您的视图是手动编程的,那么您可以在代码中添加以下检查:

 if ( [sender isKindOf: [UIResponder class]] == YES && [(UIResponder*)sender canResignFirstResponder] == YES )
     [sender resignFirstResponder];

答案 2 :(得分:0)

如果你想在按钮上隐藏键盘,假设你的搜索框是你应该写的当前响应者

[searchBox resignFirstResponder]; // This will close keyboard