UISearchBar 在模拟器上工作但在设备上不起作用
我在我的应用程序中使用 UISearchBar
。当我在模拟器上运行时一切都很好。在设备上不起作用。在设备上,我的搜索栏框架正在更改,但键盘不显示。 NSlog 正在工作。我的代码在这里:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"wrote");
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self.mySearchBar setFrame:CGRectMake(0,100,mySearchBar.frame.size.width,mySearchBar.frame.size.height)];
mySearchBar.showsCancelButton=TRUE;
mySearchBar.text=@"nowwrite";
NSLog(@"chosed");
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"cancel");
}
- (void)viewDidLoad
{
mySearchBar.delegate=self;
[super viewDidLoad];
}
和我的 .h 文件
@interface sehirRehberi : UIViewController <UISearchBarDelegate,MKMapViewDelegate,CLLocationManagerDelegate,UINavigationControllerDelegate> {
IBOutlet UISearchBar *mySearchBar;
}
@property(retain, nonatomic) UISearchBar *mySearchBar;
I'm using an UISearchBar
in my app. When I run on simulator everything is fine. On device does not work. On device, my searchbar frame is changing but keybord doesn't show. NSlog
s are working. My code is here:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"wrote");
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self.mySearchBar setFrame:CGRectMake(0,100,mySearchBar.frame.size.width,mySearchBar.frame.size.height)];
mySearchBar.showsCancelButton=TRUE;
mySearchBar.text=@"nowwrite";
NSLog(@"chosed");
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"cancel");
}
- (void)viewDidLoad
{
mySearchBar.delegate=self;
[super viewDidLoad];
}
and my .h file
@interface sehirRehberi : UIViewController <UISearchBarDelegate,MKMapViewDelegate,CLLocationManagerDelegate,UINavigationControllerDelegate> {
IBOutlet UISearchBar *mySearchBar;
}
@property(retain, nonatomic) UISearchBar *mySearchBar;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已将视图设置为第一响应者?这是键盘显示所必需的。我不确定为什么它在模拟器上工作,但您应该在
viewDidLoad
方法中调用[selfBecomeFirstResponder];
以使视图能够接收键盘事件,从而显示键盘。完成后(viewWillDisappear
等),只需调用[self resignFirstResponder];
。我认为这是你的问题。如果没有更多的解释/代码,很难说清楚。Have you made the view first responder? That's necessary for the keyboard to show. I'm not sure why it's working on the simulator, but you should call
[self becomeFirstResponder];
in yourviewDidLoad
method to enable the view to receive keyboard events and thus show the keyboard. When you're done (viewWillDisappear
, etc.), just call[self resignFirstResponder];
. I think that's your problem. It's hard to tell without more explanation/code.我遇到了类似的问题,“UISearchBar”出现在模拟器中,但没有出现在实际设备中。当我将 UISearchBar 放入 nib 文件中时,我通过使用 NibName 调用构造函数解决了该问题。
I had the similar problem that 'UISearchBar' appeared in Simulator but not in actual device. I fixed the issue by calling Constructor with NibName as I placed the UISearchBar in nib file.