繁体   English   中英

UISearchBar - ReturnKeyType不适用于iOS 8

[英]UISearchBar - ReturnKeyType not working for iOS 8

我在storyboard中的tableview控制器中使用UISearchbar returnKeyTypeUIReturnKeySearch

在此输入图像描述

它适用于iOS7,但returnKeyType不适用于iOS8。 在iOS8中,每次键盘都会出现返回键。 我试图在控制器的viewDidLoad方法中设置returnkeytype 在iOS8中设置returnKeyType = UIReturnKeySearch需要做什么?

尝试制作SearchBar的IBOutlet

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

并将以下行代码添加到viewDidLoad方法

 // if u want Done return key and change accordingly.
_searchBar.returnKeyType = UIReturnKeyDone;

SearchViewController.h

//
#import <UIKit/UIKit.h>

@interface SearchViewController : UIViewController
 <UISearchBarDelegate, UITableViewDataSource> {
    NSMutableArray *tableData;

    UIView *disableViewOverlay;

    UITableView *theTableView;
    UISearchBar *theSearchBar;
}

@property(retain) NSMutableArray *tableData;
@property(retain) UIView *disableViewOverlay;

@property (nonatomic, retain) IBOutlet UITableView *theTableView;
@property (nonatomic, retain) IBOutlet UISearchBar *theSearchBar;

- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active;

@end

SearchViewController.m

//
#import "SearchViewController.h"

@implementation SearchViewController
@synthesize tableData;
@synthesize disableViewOverlay;
@synthesize theSearchBar;
@synthesize theTableView;


// Initialize tableData and disabledViewOverlay 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableData =[[NSMutableArray alloc]init];
    self.disableViewOverlay = [[UIView alloc]
     initWithFrame:CGRectMake(0.0f,44.0f,320.0f,416.0f)];
    self.disableViewOverlay.backgroundColor=[UIColor blackColor];
    self.disableViewOverlay.alpha = 0;
}

// Since this view is only for searching give the UISearchBar 
// focus right away
- (void)viewDidAppear:(BOOL)animated {
    [self.theSearchBar becomeFirstResponder];
    [super viewDidAppear:animated];
}

#pragma mark -
#pragma mark UISearchBarDelegate Methods

- (void)searchBar:(UISearchBar *)searchBar
    textDidChange:(NSString *)searchText {
  // We don't want to do anything until the user clicks 
  // the 'Search' button.
  // If you wanted to display results as the user types 
  // you would do that here.
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    // searchBarTextDidBeginEditing is called whenever 
    // focus is given to the UISearchBar
    // call our activate method so that we can do some 
    // additional things when the UISearchBar shows.
    [self searchBar:searchBar activate:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    // searchBarTextDidEndEditing is fired whenever the 
    // UISearchBar loses focus
    // We don't need to do anything here.
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    // Clear the search text
    // Deactivate the UISearchBar
    searchBar.text=@"";
    [self searchBar:searchBar activate:NO];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // Do the search and show the results in tableview
    // Deactivate the UISearchBar

    // You'll probably want to do this on another thread
    // SomeService is just a dummy class representing some 
    // api that you are using to do the search
    NSArray *results = [SomeService doSearch:searchBar.text];

    [self searchBar:searchBar activate:NO];

    [self.tableData removeAllObjects];
    [self.tableData addObjectsFromArray:results];
    [self.theTableView reloadData];
}

// We call this when we want to activate/deactivate the UISearchBar
// Depending on active (YES/NO) we disable/enable selection and 
// scrolling on the UITableView
// Show/Hide the UISearchBar Cancel button
// Fade the screen In/Out with the disableViewOverlay and 
// simple Animations
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active{  
    self.theTableView.allowsSelection = !active;
    self.theTableView.scrollEnabled = !active;
    if (!active) {
        [disableViewOverlay removeFromSuperview];
        [searchBar resignFirstResponder];
    } else {
        self.disableViewOverlay.alpha = 0;
        [self.view addSubview:self.disableViewOverlay];

        [UIView beginAnimations:@"FadeIn" context:nil];
        [UIView setAnimationDuration:0.5];
        self.disableViewOverlay.alpha = 0.6;
        [UIView commitAnimations];

        // probably not needed if you have a details view since you 
        // will go there on selection
        NSIndexPath *selected = [self.theTableView 
            indexPathForSelectedRow];
        if (selected) {
            [self.theTableView deselectRowAtIndexPath:selected 
                animated:NO];
        }
    }
    [searchBar setShowsCancelButton:active animated:YES];
}


#pragma mark -
#pragma mark UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"SearchResult";
    UITableViewCell *cell = [tableView
     dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
         initWithStyle:UITableViewCellStyleDefault 
         reuseIdentifier:MyIdentifier] autorelease];
    }

    id *data = [self.tableData objectAtIndex:indexPath.row];
    cell.textLabel.text = data.name;
    return cell;
}

#pragma mark -
#pragma mark Memory Management Methods

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [theTableView release], theTableView = nil;
    [theSearchBar release], theSearchBar = nil;
    [tableData dealloc];
    [disableViewOverlay dealloc];
    [super dealloc];
}

@end

使用UISearchBar和UITableView构建SearchView

这可能会帮助你:)

我认为你现在可以使用你的硬编码逻辑。 如果我能为您的问题找到更好的解决方案,我会更新。

-(void)viewDidLoad {
    [self setReturnKeyTypeSearchForView:searchBar];
}

-(void)setReturnKeyTypeSearchForView:(UIView *)view
{
    for (id subView in view.subviews) {
        if ([subView isKindOfClass:[UITextField class]]) {
            [subView setReturnKeyType:UIReturnKeySearch];
        }
        else {
            [self setReturnKeyTypeSearchForView:subView];
        }
    }
    if ([view isKindOfClass:[UITextField class]]) {
        [(UITextField *)view setReturnKeyType:UIReturnKeySearch];
    }
}

我不确定我是否理解你的问题。 你想要“搜索”按钮而不是“返回”按钮,对吧? 在ios 8中有一个新的SearchController,尝试一下:

YourTableViewController.h

@interface YourTableViewController : UITableViewController<UISearchResultsUpdating>
@end

现在实施:

YourTableViewController.m

- (void)viewDidLoad {
// initializing with the same controller as presenting
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, searchController.searchBar.frame.size.width, 44.0f);
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.delegate = self;
searchController.searchBar.returnKeyType = UIReturnKeySearch; //should be search by default.. you can change to whatever you want.

// adding searchBar into HeaderView
self.tableView.tableHeaderView = searchController.searchBar;

// just to be able to present results on the same controller
self.definesPresentationContext = YES;
}

您还必须从UISearchResultsUpdating协议实现方法:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
   // you can leave it blank
}

编辑:如果它不是你想要的,请评论,所以我可以相应地更新我的答案

viewDidLoad尝试这个:

UITextField *txfSearchField = [yourSearchbar valueForKey:@"_searchField"];

    if([txfSearchField conformsToProtocol:@protocol(UITextInputTraits)]) {
       [txfSearchField setReturnKeyType:UIReturnKeyDefault];
    } 

暂无
暂无

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

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