简体   繁体   English

通过搜索按钮推送视图控制器

[英]push view controller by search button

i have Storyboard with UINavigationController i have created the first screen with searchbar i have added second viewcontrol via IB to storyboard我有带有UINavigationController Storyboard我用搜索UINavigationController创建了第一个屏幕 我已经通过 IB 添加了第二个视图控件到情节Storyboard

how do i push the second viewcontroller to navigationcontroller ?我如何将第二个视图控制器推送到导航控制器?

i have tried我试过了

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];

    ViewController *screen2 = [[ViewController alloc] initWithNibName:@"ViewController2" bundle:nil];

    [self.navigationController pushViewController:screen2 animated:YES];
}

i have set Custom Class of second view controller to ViewController2我已将第二个视图控制器的Custom Class设置为ViewController2

but when i run the app and click the search button in keyboard i get the following error但是当我运行应用程序并单击键盘上的搜索按钮时,我收到以下错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle  (loaded)' with name 'ViewController2''

The error you are getting is because Xcode cannot find the xib named ViewController2.您收到的错误是因为Xcode找不到名为 ViewController2 的xib

You may just call self.performSegueWithIdentifier(@"identifierToViewController")您可以调用self.performSegueWithIdentifier(@"identifierToViewController")

But first make sure that your searchViewController is a child of an UINavigationController但首先要确保您的 searchViewController 是 UINavigationController 的子项在此处输入图片说明

Then make sure that the segue has a proper identifier:然后确保 segue 具有正确的标识符: 在此处输入图片说明

First of all make sure that your second view controller class name is : ViewController2首先确保你的第二个视图控制器类名是: ViewController2

After that in storyboard give your View Controller's class name and Storyboard Id as @"ViewController2", Please refer screen shot.之后在故事板中将您的视图控制器的类名和故事板 Id 指定为 @"ViewController2",请参阅屏幕截图。

在此处输入图片说明

Actually you use wrong method to create instance of UIViewController as it is used when you use xib file.实际上,您使用错误的方法来创建 UIViewController 的实例,因为它在您使用 xib 文件时使用。

Change your code like this :像这样改变你的代码:

    ViewController2 *screen2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];

[self.navigationController pushViewController:screen2 animated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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