UISearchBar 在模拟器上工作但在设备上不起作用

发布于 2025-01-07 19:27:32 字数 980 浏览 6 评论 0原文

我在我的应用程序中使用 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. NSlogs 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2025-01-14 19:27:32

您是否已将视图设置为第一响应者?这是键盘显示所必需的。我不确定为什么它在模拟器上工作,但您应该在 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 your viewDidLoad 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.

拥醉 2025-01-14 19:27:32

我遇到了类似的问题,“UISearchBar”出现在模拟器中,但没有出现在实际设备中。当我将 UISearchBar 放入 nib 文件中时,我通过使用 NibName 调用构造函数解决了该问题。

[MyUITableViewController initWithNibName:@"MyUITableViewController" bundle:nil];

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.

[MyUITableViewController initWithNibName:@"MyUITableViewController" bundle:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文