Ios 如何将textfield输入视图设置为搜索栏?

Ios 如何将textfield输入视图设置为搜索栏?,ios,objective-c,uitextfield,uisearchbar,Ios,Objective C,Uitextfield,Uisearchbar,我已经尝试得到正确的解决方案,但我不能任何程序员请给我一个想法如何使 1.是否可以将uitextfield设置为uisearchbar UISearchBar *search=[[UISearchBar alloc]init]; search.delegate=self; [_txtLocation setInputView:search]; 2.当用户键入所需的值时,我会在文本字段底部将其显示为覆盖 3.请帮我找出解决办法 In.h文件 #import <UIKit/

我已经尝试得到正确的解决方案,但我不能任何程序员请给我一个想法如何使

1.是否可以将uitextfield设置为uisearchbar

 UISearchBar *search=[[UISearchBar alloc]init];
  search.delegate=self;
  [_txtLocation setInputView:search];
2.当用户键入所需的值时,我会在文本字段底部将其显示为覆盖

3.请帮我找出解决办法

In.h文件

    #import <UIKit/UIKit.h>

    @interface SEMainVC : UIViewController <UITextFieldDelegate>{
        NSMutableArray *dummyArray;
        NSMutableArray *searchArray;
        NSString *searchTextString;
    }

    @property (weak, nonatomic) IBOutlet UITextField *searchTextField;
    @property (weak, nonatomic) IBOutlet UITableView *contentTableView;

    - (void) setupData;

    @end

要使文本字段像搜索栏一样工作,请实现
-(BOOL)textField:(UITextField*)textField
应更改字符范围:(NSRange)范围
replacementString:(NSString*)string
UITextField的委托方法。所以,基本上,您需要实现自动完成功能,这就是您如何做到的。 要检测触发了哪个textfield,请将标记分配给textfield

ViewController.h
文件中:

@interface ViewController :
 <UIViewController<UITextFieldDelegate,UITableViewDataSource, UITableViewDelegate>
{
    UITableView *autocompleteTableView;
    UITextField * searchtextField1;
    UITextField * searchtextField2;
    NSMutableArray *autoCompleteList;
    NSMutableArray *initalList;

}
- (void)viewDidLoad {
searchtextField1 = [[UITextField alloc]
                              initWithFrame:CGRectMake(5,0, 245, 33)];

searchtextField2.tag = 2;
searchtextFeild1.tag = 1;   


    searchtextField.placeholder = @"eg: make up";

    searchtextField.textColor = [UIColor whiteColor];

    //[imageView addSubview:searchtextField];
    //[self.view addSubview:imageView];


    autoCompleteList = [[NSMutableArray alloc] init];
    searchtextField1.delegate = self;
    searchtextField2.delegate = self;

    autocompleteTableView = [[UITableView alloc]init];



    if(screenRect.size.height == 568)
    {
        float X_Co_tbl = (self.view.frame.size.width - 271)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 105, 271, 120)];
    }

    else if (screenRect.size.width == 414)
    {
        float X_Co_tbl = (self.view.frame.size.width - 281)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 145, 281, 120)];
    }
    else if(screenRect.size.width == 375)
    {
        float X_Co_tbl = (self.view.frame.size.width - 281)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 125, 281, 120)];
    }
    else
    {
        float X_Co_tbl = (self.view.frame.size.width - 271)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 95, 271, 120)];
    }
    autocompleteTableView.delegate = self;
    autocompleteTableView.dataSource = self;
    autocompleteTableView.scrollEnabled = YES;
    autocompleteTableView.hidden = YES;
    [self.view addSubview:autocompleteTableView];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    autocompleteTableView.hidden = YES;

    return YES;
}


- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
    autocompleteTableView.hidden = NO;
    if (textField.tag == 1) {
    /// Initialize your array for searchTextFeild1;
    initialList = [[NSMutableArray alloc]initWithObjects:@"Face wash",@"Morning face wash",@"Cleanser", nil];
}
if (textField.tag == 2) {
    /// Initialize your array for searchTextFeild2;
    initialList = [[NSMutableArray alloc]initWithObjects:@"Face wash",@"Morning face wash",@"Cleanser", nil];
}
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring
                 stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

/// You make Text Field work as Search Bar here
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

    // Put anything that starts with this substring into the autocompleteUrls array
    // The items in this array is what will show up in the table view
    [autoCompleteList removeAllObjects];
    for(NSString *curString in initialList) {
        //NSRange substringRange = [curString rangeOfString:substring];

        if ([curString rangeOfString:substring options:NSCaseInsensitiveSearch].location != NSNotFound) {
            [autoCompleteList addObject:curString];
        }
    }
    [autocompleteTableView reloadData];
}

#pragma mark UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return autoCompleteList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
    }

    cell.textLabel.text = [autoCompleteList objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    searchtextField.text = selectedCell.textLabel.text;

    autocompleteTableView.hidden = YES;

    [self goPressed];

}

对于表视图,我想再添加一个视图控制器或当前视图控制器就足够了。这取决于您的要求。您可以再添加一个视图控制器或当前视图控制器就足够了。您可以直接使用textfield进行搜索您是否希望自动填充该文本字段,对吗?@ShrikantKankatti是您更正了确定您希望自动填充UItextfield,对吗?所以我可以发布我的答案。是的,发布你的答案对我有帮助@Shrikantkankatii有疑问,4 uitextfeld被放在我的故事板中,我如何为特定的文本字段给出这个所有方法都可以使用@selector?@KishoreKumar请检查我的答案。@KishoreKumar更新了2个文本字段的答案。检查一下。我按照你的建议给出了所有内容@Rumin,但没有发生任何事情我认为我这边有问题。@KishoreKumar如果你尝试其他人的答案,请评论他/她的答案。不管怎样,你试过我的吗?
- (void)viewDidLoad {
searchtextField1 = [[UITextField alloc]
                              initWithFrame:CGRectMake(5,0, 245, 33)];

searchtextField2.tag = 2;
searchtextFeild1.tag = 1;   


    searchtextField.placeholder = @"eg: make up";

    searchtextField.textColor = [UIColor whiteColor];

    //[imageView addSubview:searchtextField];
    //[self.view addSubview:imageView];


    autoCompleteList = [[NSMutableArray alloc] init];
    searchtextField1.delegate = self;
    searchtextField2.delegate = self;

    autocompleteTableView = [[UITableView alloc]init];



    if(screenRect.size.height == 568)
    {
        float X_Co_tbl = (self.view.frame.size.width - 271)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 105, 271, 120)];
    }

    else if (screenRect.size.width == 414)
    {
        float X_Co_tbl = (self.view.frame.size.width - 281)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 145, 281, 120)];
    }
    else if(screenRect.size.width == 375)
    {
        float X_Co_tbl = (self.view.frame.size.width - 281)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 125, 281, 120)];
    }
    else
    {
        float X_Co_tbl = (self.view.frame.size.width - 271)/2;
        [autocompleteTableView setFrame:CGRectMake(X_Co_tbl, 95, 271, 120)];
    }
    autocompleteTableView.delegate = self;
    autocompleteTableView.dataSource = self;
    autocompleteTableView.scrollEnabled = YES;
    autocompleteTableView.hidden = YES;
    [self.view addSubview:autocompleteTableView];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    autocompleteTableView.hidden = YES;

    return YES;
}


- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
    autocompleteTableView.hidden = NO;
    if (textField.tag == 1) {
    /// Initialize your array for searchTextFeild1;
    initialList = [[NSMutableArray alloc]initWithObjects:@"Face wash",@"Morning face wash",@"Cleanser", nil];
}
if (textField.tag == 2) {
    /// Initialize your array for searchTextFeild2;
    initialList = [[NSMutableArray alloc]initWithObjects:@"Face wash",@"Morning face wash",@"Cleanser", nil];
}
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring
                 stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

/// You make Text Field work as Search Bar here
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

    // Put anything that starts with this substring into the autocompleteUrls array
    // The items in this array is what will show up in the table view
    [autoCompleteList removeAllObjects];
    for(NSString *curString in initialList) {
        //NSRange substringRange = [curString rangeOfString:substring];

        if ([curString rangeOfString:substring options:NSCaseInsensitiveSearch].location != NSNotFound) {
            [autoCompleteList addObject:curString];
        }
    }
    [autocompleteTableView reloadData];
}

#pragma mark UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return autoCompleteList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
    }

    cell.textLabel.text = [autoCompleteList objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    searchtextField.text = selectedCell.textLabel.text;

    autocompleteTableView.hidden = YES;

    [self goPressed];

}