这是我的 SearchViewController 的代码。但是当我推送到 nextController 时,应用程序崩溃并且它没有到达 nextController,或者在某些情况下,我在崩溃之前瞥了一眼它。我在日志中收到错误“发送到选择器的错误实例”。SearchViewController 在情节提要中附加了一个 NavigationController,如您所见,我推动了导航控制器。
如果我将 'pushViewController' 更改为 'presentModalViewController' ,它现在可以加载 UITableViewController,但是如果我滚动(滑动)TableView(如果它填充了结果),它现在会崩溃。如果它是空的(没有匹配的结果),它不会崩溃。奇怪的。此外,TableView 上方没有导航栏。
你能告诉我我做错了什么并帮助我纠正它吗?感觉这是一个简单的问题。
SearchViewController.h
#import <UIKit/UIKit.h>
@interface SearchViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *allObjectsArray;
@property (nonatomic, strong) NSMutableArray *resultObjectsArray;
@property (nonatomic, strong) IBOutlet UITextField *nameTextField;
-(IBAction)searchButtonPressed:(id)sender;
@end
搜索视图控制器.m
-(IBAction)searchButtonPressed:(id)sender{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
allObjectsArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *nameString = [NSString stringWithFormat:@"%@", [nameTextField text]];
resultObjectsArray = [NSMutableArray array];
for(NSDictionary *wine in allObjectsArray)
{
NSString *wineName = [wine objectForKey:@"Name"];
NSRange range = [wineName rangeOfString:nameString options:NSCaseInsensitiveSearch];
if(range.location != NSNotFound)
[resultObjectsArray addObject:wine];
}
NSLog(@"Objects: %@", allObjectsArray);
ResultsTableViewController *nextController = [[self storyboard] instantiateViewControllerWithIdentifier:@"ResultsController"];
nextController.objectsArray = [[NSMutableArray alloc]initWithArray:resultObjectsArray];
NSLog(@"Results: %@", nextController.objectsArray);
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
这是填充 UITableView 的 nextController 中的 objectsArray:
@property (nonatomic, strong) NSMutableArray *objectsArray;
结果表视图控制器.m:
#import "ResultsTableViewController.h"
@interface ResultsTableViewController ()
@end
@implementation ResultsTableViewController
@synthesize objectsArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%s", __FUNCTION__);
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [objectsArray count];
NSLog(@"%s", __FUNCTION__);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s", __FUNCTION__);
static NSString *CellIdentifier = @"searchResultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Use the default cell style.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchResultCell"] autorelease];
}
// Configure the cell...
cell.textLabel.text = [[objectsArray objectAtIndex:indexPath.row] valueForKey:@"Name"];
return cell;
}
@end