Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 添加为子视图时,从UISearchBar中退出键盘_Objective C_Search - Fatal编程技术网

Objective c 添加为子视图时,从UISearchBar中退出键盘

Objective c 添加为子视图时,从UISearchBar中退出键盘,objective-c,search,Objective C,Search,我想在没有情节提要的情况下将UISearchBar添加到视图控制器中,因此我以编程方式将其添加为子视图。我已经设置了代理,但我似乎无法通过单击搜索按钮来关闭键盘 这是我的密码: @interface ViewController : UIViewController <UISearchBarDelegate> { IBOutlet UISearchBar* mySearchBar; } ---- - (void)viewDidLoad { [mySearchBar s

我想在没有情节提要的情况下将UISearchBar添加到视图控制器中,因此我以编程方式将其添加为子视图。我已经设置了代理,但我似乎无法通过单击搜索按钮来关闭键盘

这是我的密码:

@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没有任何关系。