我正在使用 PickerView 来选择值,并且我想在按下按钮后重置它(因此它不会显示以前的选择)。
问题是我总是遇到两个错误之一,它在运行之间发生变化,没有明显的模式。
有谁知道可能导致错误的原因以及如何解决问题?
代码:
NSString * chosen;
NSString * elemString;
NSMutableArray *secondTableArray ;
NSInteger p = 0;
@implementation LagerViewController
@synthesize requestObject;
@synthesize choosenDocKind;
@synthesize pickerView1;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
secondTableArray = [[NSMutableArray alloc] init];
secondTableView.scrollEnabled = YES;
secondTableView.bounces = YES;
elemString = @"";
choosenDocKind = [[NSString alloc] initWithString:@""];
[pickerView removeFromSuperview];
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(pickerViewItems != nil)
{
[pickerViewItems release];
pickerViewItems = nil;
}
if((indexPath.section == 1)&&(indexPath.row == 0))
{
NSString * temp = [[NSString stringWithFormat:@"%@",elemString] copy];
NSString * tableString;
if([temp length] == 0) {
[secondTableArray removeAllObjects];
[secondTableView reloadData];
elemString = [[NSString stringWithFormat:@"ARTIKEL: %@ %@ %@", matchcode, quantity, choosenDocKind] copy];
tableString = [[NSString stringWithFormat:@"%@ %@ %@", matchcode, quantity, choosenDocKind] copy];
// THESE 2 ROWS BELOW CAUSE THE ERRORS
[pickerView reloadAllComponents];
[pickerView selectRow:0 inComponent:0 animated:YES];
}
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[theTableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - button events
- (IBAction)btnSubmitPVSelection:(id)sender
{
if([choosenPrompt isEqualToString:NSLocalizedString(@"DOCUMENT_REQUEST_LAGER_UM", nil)])
{
if(choosenDocKind != nil)
{
chosen = choosenDocKind;
[choosenDocKind release];
choosenDocKind = nil;
}
choosenDocKind = [[[NSString alloc] initWithString:[pickerViewItems objectAtIndex:[pickerView selectedRowInComponent:0]]] retain];
}
[tvArchDocParam reloadData];
if(choosenPrompt != nil)
{
[self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
[choosenPrompt release];
choosenPrompt = nil;
}
[pickerView reloadAllComponents];
[pickerView selectRow:0 inComponent:0 animated:YES];
}
.h
@interface ... {
DocRequest *requestObject;
NSArray *pickerViewItems;
NSString *choosenPrompt;
UITextView *matchtext;
IBOutlet UITableView *tvArchDocParam;
UIAlertController *actionSheet;
UITableView *theTableView;
IBOutlet UITableView *secondTableView;
}
@property (nonatomic, retain)DocRequest* requestObject;
@property (nonatomic, retain)UIPickerView *pickerView1;
-(UITableViewCell*)getSelectorCell:(NSString*)CellIdentifier;
-(UITableViewCell*)getTextFieldCell:(NSString*)CellIdentifier;
-(void)showPickerView;
错误:
1. Thread 1: EXC_BAD_ACCESS
2. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason NSDictionaryM reloadAllComponents
最佳答案
首先也是最重要的 — 停止使用手动内存管理。
第二个关于崩溃,看起来两个崩溃都是相关的并且是由同一问题引起的(由于您给我们的代码,不可能猜测到底是什么导致应用程序崩溃),显然您的 pickerView 是在 [pickerView reloadAllComponents] 之前的某个地方发布的;被称为。
可能是因为[pickerView removeFromSuperview]引起的;例如,在 viewDidLoad 中,如果您分配了 pickerView 或类似的东西。
关于iOS:重置 UIPickerView EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34203882/