Objective c 添加为子视图时,从UISearchBar中退出键盘
我想在没有情节提要的情况下将UISearchBar添加到视图控制器中,因此我以编程方式将其添加为子视图。我已经设置了代理,但我似乎无法通过单击搜索按钮来关闭键盘 这是我的密码:Objective c 添加为子视图时,从UISearchBar中退出键盘,objective-c,search,Objective C,Search,我想在没有情节提要的情况下将UISearchBar添加到视图控制器中,因此我以编程方式将其添加为子视图。我已经设置了代理,但我似乎无法通过单击搜索按钮来关闭键盘 这是我的密码: @interface ViewController : UIViewController <UISearchBarDelegate> { IBOutlet UISearchBar* mySearchBar; } ---- - (void)viewDidLoad { [mySearchBar s
@interface ViewController : UIViewController <UISearchBarDelegate>
{
IBOutlet UISearchBar* mySearchBar;
}
----
- (void)viewDidLoad
{
[mySearchBar setDelegate: self];
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.origin.y, 320, 44)];
[self.view addSubview:mySearchBar];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)mySearchBar
{
[mySearchBar resignFirstResponder];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)mySearchBar
{
[mySearchBar resignFirstResponder];
}
@界面ViewController:UIViewController
{
ibuisearchbar*mySearchBar;
}
----
-(无效)viewDidLoad
{
[mySearchBar setDelegate:self];
mySearchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0,self.navigationController.navigationBar.frame.origin.y,320,44)];
[self.view addSubview:mySearchBar];
}
-(无效)搜索栏搜索按钮已选中:(UISearchBar*)我的搜索栏
{
[mySearchBar辞职FirstResponder];
}
-(无效)SearchBartextdidediting:(UISearchBar*)mySearchBar
{
[mySearchBar辞职FirstResponder];
}
在分配搜索栏之前,您正在设置代理。尝试在之后设置委托
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.origin.y, 320, 44)];
[mySearchBar setDelegate: self];
首先检查是否调用了委托方法总是很有帮助的。设置断点并查看:)这与Xcode没有任何关系。