UISearchBar适用于模拟器,但不适用于设备

时间:2012-02-22 21:42:45

标签: ios uikit ios-simulator device 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;

2 个答案:

答案 0 :(得分:0)

您是否已将视图作为第一响应者?这是键盘显示的必要条件。我不确定它为何在模拟器上运行,但您应该在[self becomeFirstResponder];方法中调用viewDidLoad以使视图能够接收键盘事件并显示键盘。完成后(viewWillDisappear等),只需致电[self resignFirstResponder];即可。我认为这是你的问题。没有更多解释/代码就很难说清楚。

答案 1 :(得分:0)

我遇到类似的问题,'UISearchBar'出现在模拟器中但不出现在实际设备中。我通过使用NibName调用Constructor解决了这个问题,因为我将UISearchBar放在了nib文件中。

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