我有两天被困,因为没有工作我的NSArray。
当我运行应用程序时一切正常,但当我触摸搜索栏时,应用程序关闭原因:'- [__NSArrayM tableView]:未识别的选择器发送到实例0x16e34c50'
@implementation ViewController
-(BOOL)prefersStatusBarHidden
{
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
monthArray = [[ NSMutableArray alloc] initWithObjects:@"Paliwizumab",@"Opis przedmiotu zamówienia",@"Paliwizumab a 0,1g inj I.M ( proszek + rozpuszczalnik)",@"Paliwizumab a 0,05 g I.M ( proszek + rozpuszczalnik )",@"Nazwa międzynarodowa",@"PALIVISUMABUM*",@"Paliwizumab01",@"Paliwizumab02",@"Paliwizumab03",@"Paliwizumab04",@"Paliwizumab05",@"Paliwizumab06", nil];
[searchBar setParentController:self];
[searchBar setParentController:monthArray];
[searchBar setDelegate:searchBar];
[self prefersStatusBarHidden];
}
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:searchBar selector:@selector(keyboardWillShow:) name: (UIKeyboardWillShowNotification ) object:nil];
}
-(void)viewDidDisappear:(BOOL)animated
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:searchBar];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self monthArray]count];
}
-(NSMutableArray*)monthArray
{
if (searchBar.isSearching == 1)
return searchBar.searchArray;
else
return monthArray;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellId = @"CellId";
UITableViewCell *cell = [self->_tableView dequeueReusableCellWithIdentifier:CellId];
if (! cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
[cell.textLabel setText:[[self monthArray] objectAtIndex:indexPath. row]];
return cell;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "JPSearchBar.h"
@interface ViewController : UIViewController<UITableViewDelegate>
{
NSMutableArray *monthArray;
IBOutlet JPSearchBar *searchBar;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;
-(NSMutableArray*)monthArray;
@end
在Objective-C中,"unrecognized selector"错误意味着你试图在一个对象上执行一个方法(即:"发送一条消息",其中"消息"由"选择器"标识),但对象不实现该方法(即:"不识别选择器")。
当您拥有错误类的对象时,通常会发生这种情况。在这种情况下,我猜searchBar
的setParentController:
方法期望实现tableView
的一些对象(可能是self
,因为我看到它有一个tableView
属性,这意味着它有一个tableView
getter方法),但你给它monthArray
代替。这只是一个猜测,因为你的代码的其余部分是缺失的。(例如,JPSearchBar
是什么?)