将文本从搜索栏发送到superview

时间:2015-08-09 19:43:01

标签: ios objective-c uitableview

我有一个tableviewcontroller和一个自定义headerview(uiview)

Headerview界面

@interface HMDiscoveryHeaderView : UIView <UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar; 

@end

在layoutSubviews方法

中初始化搜索栏
- (void)layoutSubviews {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 20, 250, 50)];
    [self addSubview:self.searchBar];
}

我可以像这样获取文本

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"search text %@", searchBar.text);
}

在我的tableview中,界面就是这个

@interface HMDiscoveryViewController () <UISearchBarDelegate>

@property (nonatomic, strong) NSString * stuff;
@property (nonatomic, strong) HMDiscoveryHeaderView *headerView;


@end

我在viewdidload中设置标题,如此

- (void)viewDidLoad {
    [super viewDidLoad];
    self.headerView = [[HMDiscoveryHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];

    [[self.headerView searchBar] setDelegate:self];

    [self.tableView setTableHeaderView:self.headerView];
    NSString *testy  = [[self.headerView searchBar] text];
// this prints null
    NSLog(@"test %@", testy);
}

这两种尝试获取文本的方法永远不会被称为

- (void)setStuff:(NSString *)stuff
{
    _stuff = stuff;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self setStuff:searchBar.text];
    NSLog(@"lol shit %@", self.stuff);
}

如何在tableviewcontroller的headerview中获取UISearchBar中的输入文本?

1 个答案:

答案 0 :(得分:1)

“我的标题视图有一个UISearchbar作为在viewdidload中初始化的子视图”

这是不可能的,因为headerView是你所说的UIview,而UIViews没有“viewDidLoad”方法,但是,我离题了......假设你的意思是UITAbleViewController在视图中声明了headerview加载然后你需要做这样的事情:

HeaderView.h

@interface HeaderView : UIView
@property (nonatomic) UISearchBar *searchBar
@end

现在,您已通过在headerViews头文件中声明UISearchBar将UISearchBar暴露给其他类。然后这样做

headerview *headerView = [[headerview alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];

self.tableView.tableHeaderView = headerView;

[headerview searchBar].text <==this is the search bar text!

现在,为了将此Text强制进入表视图,请执行以下操作:

在tableView的头文件中声明了一个NSString属性

@property (nonatomic) NSString * stuff;

然后,在tableview implmentaiton文件中执行以下操作:

- (void)setStuff:(NSString *)stuff
{
    _stuff = stuff;
}

现在_stuff包含一个字符串值

在表视图控制器中,使用此方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"search text %@", searchBar.text);
}

将其更改为:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [tableView setStuff:searchBar.text];
}

然后,在您的tableView中,您可以使用搜索栏中的文本进行操作

代表团是为那些谁不知道如何正确使用getter和setter方法,你不必使用委派几乎永远,我会躲得远远的形成,因为它会变成你的应用程序进入的野兽在你达到成千上万行代码之后的噩梦。

setters&amp;具有子类的getter比委托大约好4万亿次,在使用UIKit元素时使用委托,要求你知道关于你正在子类化的对象的东西,否则,与块操作混合的setter和getter是最简单,最容易阅读的,和大多数可移植的代码方式。纯粹的封装就是你想要的。

哦,是的,让你的UITableViewController成为一个UISearchBarDelegte,这样你就可以在UITableViewController中使用上面的功能,这就是为什么你现在可能会摸不着头脑,所以这会进入你的UITableViewController:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [tableView setStuff:searchBar.text];
}
麻烦,你有,这个:

UITableViewController的实现文件:

@interface CustomSuperStarTableViewController () <UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@end
@implementation CustomSuperStarTableViewController
{
     SuperNiftyHeaderView * iLikeCats;
}

-(void)viewDidLoad
{
     iLikeCats = [SuperNiftyHeaderView new];
     [[iLikeCats iLikeCatsSearchBar] setDelegate:self];
     [self.tableView setTableHeaderView:iLikeCats];
      // you can now access the searchBar!
    //here's how you access the searchBar
     NSString * bubbleGum = [[iLikeCats iLikeCatsSearchBar] text]; <=== search bar text
     UIColor * hello = [[iLikeCats iLikeCatsSearchBar] backGroundColor]; <=== search bar backGroundColor
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
}

我还应该注意,这假设您实际创建了一个单独的.h和.m文件,它是一个子类UITableView,对吗?并且这个子类在它的头文件中有一个暴露的属性,如:

@property (nonatomic) MyNiftyCustomHeaderView * customHeaderView

好的,情况如此不同:

headerView是自定义的,它包含UISearchBar,对吗?是的,非常好,所以现在让我们宣布一下标题视图!

SuperNiftyHeaderView.h

@interface SuperNiftyHeaderView : UIView

@property (nonatomic) UISearchBar * iLikeCatsSearchBar;

@end

SuperNiftyHeaderView.m

@implementation SuperNiftyHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    { 
        _iLikeCatsSearchBar = [[UISearchBar alloc] init];
        [_iLikeCatsSearchBar setPlaceholder:@"  Search ..."];
        [_iLikeCatsSearchBar setTranslucent:TRUE];
        [_iLikeCatsSearchBar setBarStyle:UIBarStyleDefault];
        [_iLikeCatsSearchBar setSearchBarStyle:UISearchBarStyleProminent];
        [_iLikeCatsSearchBar setBackgroundColor:[UIColor blueColor]];
        [_iLikeCatsSearchBar setTranslatesAutoresizingMaskIntoConstraints:FALSE];
        [self addSubView:_iLikeCatsSearchBar];

        /// do more work, and then more work
        /// add constraints because you are going to learn to do all subviews programmatically from here on out and you love to program IOS so much that 1000s of lines of code are better than storyboard easy street
    }
  return self;
}
@end

现在,回顾一下我为CustomSuperStarTableViewController做的事情

更重要的是,我们可以通过添加到UITableViewController并强制SearchBar成为ViewController的委托,通过文本访问搜索,但是,我们首先误解了对方,所以重点是: 1.表视图控制器=搜索栏代表 拦截文字 3.将此文本发送到tableview以按照您的意愿执行,如果要使用它来过滤tablview,那么大约有10亿个教程使用谓词和数组来实现这种魔力,但当然它会更容易为了你这样做,因为你使用了UITableViewController而不是UIViewController,非常棒并且度过了美好的一天。

这是您从SearchBar中捕获善良的地方:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [_categories removeAllObjects];
    _searchText = searchText;
    NSArray * ss = [test copy];
    _categories = [NSMutableArray arrayWithArray:[ss filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[c] %@", _searchText]]];
    if (_categories.count ==0 && _searchText.length ==0) {
        _categories = [NSMutableArray arrayWithArray:test];
    }
    [[[self contentView] tableView] reloadData];
}

不要对此次通话中的方法调用感到不满,他们对你正在做的事情并不重要,只是担心这个:

  - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        _searchText = searchText;
    }

和_searchText在这样的实现文件中声明,它是一个IVAR

@implementation WhatEverYouWanttoNameItTableViewController
{
     NSString * _searchText;
}