简体   繁体   中英

UISearchBar works on simulator but doesn't work on device

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;

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM