这是我的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
SearchViewController.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];
}
这是nextController中的objectsArray,它填充了UITableView:
@property (nonatomic, strong) NSMutableArray *objectsArray;
ResultsTableViewController.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
答案 0 :(得分:1)
(张贴为答案 - 请参阅问题评论中的详细信息)
您使用或不使用ARC吗?因为,你有一些[UIViewController发布] ...
(与self.objectsArray相同)
cell.textLabel.text = [[self.objectsArray objectAtIndex:indexPath.row] valueForKey:@"Name"];
self.objectsArray = ...;
但仅限于您访问该物业时;提供,你有正确的@synthesize
- d (还有一件事,只是挑剔)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
顺便说一句,我刚刚注意到,你写道“你刚刚停用了ARC以便其他一些代码工作”......你没必要,你可以在每个文件的基础上禁用ARC;如果您没有内存管理和对象体验,请不要在拥有ARC时禁用它。这是iPhone Obj-C的一个很好的补充...请参阅我关于如何禁用ARC的帖子:Adding UIActivityIndicator to UITableView