Closed CoderQHao closed 7 years ago
你可以自定义返回
,然后在返回
方法中执行self.navagationController
的dismissViewControllerAnimated:completion:
如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"SearchResultViewController";
self.view.backgroundColor = PYSEARCH_RANDOM_COLOR;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
}
- (void)back
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
非常感谢您的回复,感谢您对开源做出的贡献!我现在的需求是搜索到结果的时候导航栏上又有搜索框,点击又会到搜索控制器,再次搜索完怎么回到主页,类似淘宝的搜索功能,不知道我讲明白了没有~
在 2017年4月10日,16:39,CoderKo1o notifications@github.com 写道:
你可以自定义返回,然后在返回方法中执行self.navagationController的dismissViewControllerAnimated:completion: 如下:
(void)viewDidLoad { [super viewDidLoad];
self.title = @"SearchResultViewController"; self.view.backgroundColor = PYSEARCH_RANDOM_COLOR;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)]; }
(void)back { [self.navigationController dismissViewControllerAnimated:YES completion:nil]; } — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
@CoderQHao 我看了一下淘宝的效果。你可以通过以下修改来实现
点击搜索时->搜索控制器的nvigationContrller ->dismiss - > 首页的navigationController -> push 到搜搜结果控制器。 在搜索结果控制器上面弄一个搜索栏,监听搜索栏的点击,点击后 present 搜索控制器 。Demo中代码修改具体代码如下:
PYSearchExampleController.m
// 2. 创建控制器
PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:NSLocalizedString(@"PYExampleSearchPlaceholderText", @"搜索编程语言") didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
// 开始搜索执行以下代码
[searchViewController.navigationController dismissViewControllerAnimated:NO completion:^{
PYTempViewController *searchResultViewController = [[PYTempViewController alloc] init];
searchResultViewController.searchViewController = searchViewController;
// 如:跳转到指定控制器,这里的self指首页控制器
[self.navigationController pushViewController:searchResultViewController animated:NO];
}];
}];
PYTempViewController.h
#import <UIKit/UIKit.h>
@class PYSearchViewController;
@interface PYTempViewController : UIViewController
@property (nonatomic, strong) PYSearchViewController *searchViewController;
@end
PYTempViewController.m
#import "PYTempViewController.h"
#import "PYSearchConst.h"
#import "PYSearchViewController.h"
@interface PYTempViewController () <UISearchBarDelegate>
@property (nonatomic, weak) UISearchBar *searchBar;
@end
@implementation PYTempViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"SearchResultViewController";
self.view.backgroundColor = PYSEARCH_RANDOM_COLOR;
// 创建搜索框
UIView *titleView = [[UIView alloc] init];
titleView.py_x = PYSEARCH_MARGIN * 0.5;
titleView.py_y = 7;
titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2;
titleView.py_height = 30;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
searchBar.placeholder = [NSBundle py_localizedStringForKey:PYSearchSearchPlaceholderText];
searchBar.backgroundImage = [NSBundle py_imageNamed:@"clearImage"];
searchBar.delegate = self;
self.searchBar = searchBar;
[titleView addSubview:searchBar];
titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = titleView;
__weak typeof(self) _weakSelf = self;
self.searchViewController.didSearchBlock = ^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
_weakSelf.searchBar.text = searchText;
// 这里发生请求,更新搜索结果
// 移除搜索控制器
[searchViewController.navigationController dismissViewControllerAnimated:YES completion:nil];
};
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.searchViewController];
[self presentViewController:nav animated:NO completion:nil];
// 不进入编辑状态
return NO;
}
@end
您好,根据你的思路大致实现了我的需求,但在开发过程中还有两个问题不知道应该怎么解决,还得向您请教。 第一个问题是我在首页点击搜索框进入搜索控制器大概会延迟一秒弹出键盘,从搜索到结果的页面进入搜索界面不会出现这种情况,请问下可以将所搜框中的占位文字居左吗? 第二个问题是我在搜索到结果的控制器中加了返回,不管搜索多少次都popToRootViewController,但是用手势右滑返回时还是会一层一层的返回,请问下这种情况下有好的实现吗?
在 2017年4月10日,下午6:22,CoderKo1o notifications@github.com 写道:
@CoderQHao https://github.com/CoderQHao 我看了一下淘宝的效果。你可以通过以下修改来实现 点击搜索时->搜索控制器的nvigationContrller ->dismiss - > 首页的navigationController -> push 到搜搜结果控制器。 在搜索结果控制器上面弄一个搜索栏,监听搜索栏的点击,点击后 present 搜索控制器 。Demo中代码修改具体代码如下: PYSearchExampleController.m
// 2. 创建控制器 PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:NSLocalizedString(@"PYExampleSearchPlaceholderText", @"搜索编程语言") didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) { // 开始搜索执行以下代码 [searchViewController.navigationController dismissViewControllerAnimated:NO completion:^{ PYTempViewController *searchResultViewController = [[PYTempViewController alloc] init]; searchResultViewController.searchViewController = searchViewController; // 如:跳转到指定控制器,这里的self指首页控制器 [self.navigationController pushViewController:searchResultViewController animated:NO]; }]; }];
PYTempViewController.h
import <UIKit/UIKit.h>
@class PYSearchViewController;
@interface PYTempViewController : UIViewController
@property (nonatomic, strong) PYSearchViewController *searchViewController;
@end PYTempViewController.m
import "PYTempViewController.h"
import "PYSearchConst.h"
import "PYSearchViewController.h"
@interface PYTempViewController ()
@property (nonatomic, weak) UISearchBar *searchBar;
@end
@implementation PYTempViewController
(void)viewDidLoad { [super viewDidLoad];
self.title = @"SearchResultViewController"; self.view.backgroundColor = PYSEARCH_RANDOM_COLOR; // 创建搜索框 UIView titleView = [[UIView alloc] init]; titleView.py_x = PYSEARCH_MARGIN 0.5; titleView.py_y = 7; titleView.py_width = self.view.py_width - 64 - titleView.py_x 2; titleView.py_height = 30; UISearchBar searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds]; searchBar.placeholder = [NSBundle py_localizedStringForKey:PYSearchSearchPlaceholderText]; searchBar.backgroundImage = [NSBundle py_imageNamed:@"clearImage"]; searchBar.delegate = self; self.searchBar = searchBar; [titleView addSubview:searchBar]; titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth; self.navigationItem.titleView = titleView; __weak typeof(self) _weakSelf = self; self.searchViewController.didSearchBlock = ^(PYSearchViewController searchViewController, UISearchBar searchBar, NSString *searchText) { _weakSelf.searchBar.text = searchText; // 这里发生请求,更新搜索结果
// 移除搜索控制器 [searchViewController.navigationController dismissViewControllerAnimated:YES completion:nil];
}; }
pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar )searchBar { UINavigationController nav = [[UINavigationController alloc] initWithRootViewController:self.searchViewController]; [self presentViewController:nav animated:NO completion:nil]; // 不进入编辑状态 return NO; }
@end — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/iphone5solo/PYSearch/issues/76#issuecomment-292909725, or mute the thread https://github.com/notifications/unsubscribe-auth/ATirhuLeIo1Kf-M1FWz_MKBzaSU04kskks5rugMCgaJpZM4M4d-U.
@CoderQHao
关于问题一:键盘的延迟弹出是以下代码造成的。
PYSearceViewController.m line in 97
/** 视图完全显示 */
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 弹出键盘
[self.searchBar becomeFirstResponder];
}
占位文居左,这种问题可以自己Google
!
关于问题二:
如果你按照我上面贴出的代码实现的话,实际上只push 一个搜索结果控制器
,所以不存在无你所说的问题。只要正常pop
即可!
present搜索控制器后再push,导航栏的返回怎么回到主页而不是回到搜索控制器呢?