Ios 在UIPickerView上执行错误访问-我需要了解一下原因
在运行项目并尝试更改选择器时,出现exc_bad_访问错误 错误发生在上Ios 在UIPickerView上执行错误访问-我需要了解一下原因,ios,objective-c,xcode,uipickerview,Ios,Objective C,Xcode,Uipickerview,在运行项目并尝试更改选择器时,出现exc_bad_访问错误 错误发生在上 - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 下面是我的代码。通过阅读其他SO文章,我意识到我可能没有保留变量。我是新来的,正在学习,感谢你的帮助 #import <UIKit/UIKit
- (NSString*)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
下面是我的代码。通过阅读其他SO文章,我意识到我可能没有保留变量。我是新来的,正在学习,感谢你的帮助
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@class RootViewController;
@interface AddConditionViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UITextField *txtConditionDetail;
IBOutlet UITextField *txtConditionArea;
IBOutlet UIPickerView *conditionNamesPicker;
NSMutableArray *names;
NSMutableArray *conditionDefs;
RootViewController *rvc;
NSString *conditionName;
}
@property (retain, nonatomic) IBOutlet UIPickerView *conditionNamesPicker;
@property (nonatomic,assign) RootViewController *rvc;
@property (nonatomic, retain) NSString *conditionName;
@property (nonatomic,assign) NSMutableArray *names;
@property (nonatomic,assign) NSMutableArray *conditionDefs;
@end
#import "AddConditionViewController.h"
#import "ConditionsAppDelegate.h"
#import "Condition.h"
#import "ConditionDef.h"
#import "Formula.h"
@implementation AddConditionViewController
@synthesize rvc, conditionNamesPicker, names, conditionDefs, conditionName;
/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Add Condition";
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:@selector(cancel_Clicked:)] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:@selector(save_Clicked:)] autorelease];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
ConditionsAppDelegate *appDelegate = (ConditionsAppDelegate *)[[UIApplication sharedApplication] delegate];
conditionDefs = appDelegate.getConditionDefs;
self.names = [NSMutableArray arrayWithCapacity:[conditionDefs count]];
for (ConditionDef *def in conditionDefs) {
NSString *condition_name = def.condition_name;
if (!condition_name) {
condition_name = @"<Unknown Account>";
}
[names addObject:condition_name];
}
self.conditionNamesPicker.dataSource = self;
self.conditionNamesPicker.delegate = self;
NSLog(@"LINE 48");
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Set the textboxes to empty string.
txtConditionArea.text = @"";
txtConditionDetail.text = @"";
//Make the Category name textfield to be the first responder.
[txtConditionArea becomeFirstResponder];
NSLog(@"LINE 63");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSLog(@" LINE 87 - COUNT OF CONDITION DEFS TO SHOW = %i", names.count);
[conditionNamesPicker setDataSource:self];
return [names count];
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
// NSLog(@"LINE 94 - here is the bug: conditionDefs[row] %@", names[row]);
return names[row];
}
// Catpure the picker view selection
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
conditionName = names[row];
}
- (void) save_Clicked:(id)sender {
ConditionsAppDelegate *appDelegate = (ConditionsAppDelegate *)[[UIApplication sharedApplication] delegate];
//Create a Condition Object.
Condition *c = [[Condition alloc] init];
NSInteger newId = c.getNextConditionId;
Condition *cond = [[Condition alloc] initWithPrimaryKey:newId];
cond.condition_area = txtConditionArea.text;
cond.condition_detail = txtConditionDetail.text;
cond.condition_name = conditionName;
//Add the object
// [appDelegate addCondition:cond];
[appDelegate populateFromDatabase];
// ADD TO THE ARRAY:
// [cvc.categories addObject:cond];
// [cvc.Conditions addObject:cond];
rvc.Conditions = [appDelegate activeConditions];
// UPDATE THE TABLEVIEW
[rvc.tableView reloadData];
// release
[cond release];
[c release];
//Dismiss the controller.
[self.navigationController dismissViewControllerAnimated:YES completion: nil];
}
- (void) cancel_Clicked:(id)sender {
//Dismiss the controller.
[self.navigationController dismissViewControllerAnimated:YES completion: nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
NSLog(@"LINE 159");
[theTextField resignFirstResponder];
return YES;
}
- (void)dealloc {
[txtConditionArea release];
[txtConditionDetail release];
[conditionNamesPicker release];
[super dealloc];
}
@end
#导入
#导入“RootViewController.h”
@类RootViewController;
@接口AddConditionViewController:UIViewController{
IBOUTLE UITextField*txtConditionDetail;
IBOUTLE UITextField*txtConditionArea;
IBOUTLE UIPickerView*条件名称选择器;
NSMutableArray*名称;
NSMutableArray*conditionDefs;
RootViewController*rvc;
NSString*条件名称;
}
@属性(保留,非原子)IBOUTLE UIPickerView*条件名称选择器;
@属性(非原子,赋值)RootViewController*rvc;
@属性(非原子,保留)NSString*conditionName;
@属性(非原子,赋值)NSMutableArray*名称;
@属性(非原子,赋值)NSMutableArray*conditionDefs;
@结束
#导入“AddConditionViewController.h”
#导入“ConditionsAppDelegate.h”
#导入“条件.h”
#导入“ConditionDef.h”
#导入“公式h”
@实现AddConditionViewController
@合成rvc、conditionNamesPicker、names、conditionDefs、conditionName;
/*
//实现loadView以编程方式创建视图层次结构。
-(void)负荷视图{
}
*/
//实现viewDidLoad以在加载视图后执行其他设置。
-(无效)viewDidLoad{
[超级视图下载];
self.title=@“添加条件”;
self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
目标:自我操作:@selector(取消)]autorelease];
self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
目标:自我操作:@selector(保存:)]autorelease];
self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
ConditionsAppDelegate*appDelegate=(ConditionsAppDelegate*)[[UIApplication sharedApplication]委托];
conditionDefs=appDelegate.getConditionDefs;
self.names=[NSMutableArray阵列容量:[conditionDefs count]];
用于(条件定义*条件定义中的定义){
NSString*条件_名称=定义条件_名称;
如果(!条件_名称){
条件_name=@”;
}
[名称添加对象:条件_名称];
}
self.conditionNamesPicker.dataSource=self;
self.conditionNamesPicker.delegate=self;
NSLog(“第48行”);
}
-(无效)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
//将文本框设置为空字符串。
txtConditionArea.text=@;
txtConditionDetail.text=@;
//将类别名称文本字段设为第一响应者。
[TXT条件区域成为第一响应者];
NSLog(“第63行”);
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
-(无效)未收到记忆警告{
[super-didReceiveMemoryWarning];//如果没有superview,则释放该视图
//释放任何不重要的内容,例如缓存数据
}
//数据的列数
-(int)部件编号pickerView:(UIPickerView*)pickerView
{
返回1;
}
//数据的行数
-(int)pickerView:(UIPickerView*)pickerView行数组件:(NSInteger)组件
{
NSLog(@“第87行-要显示的条件定义计数=%i”,name.COUNT);
[conditionNamesPicker setDataSource:self];
返回[姓名计数];
}
//要为传入的行和组件(列)返回的数据
-(NSString*)pickerView:(UIPickerView*)pickerView
titleForRow:(NSInteger)行
forComponent:(NSInteger)组件
{
//NSLog(@“第94行-这里是错误:conditionDefs[row]@”,names[row]);
返回名称[行];
}
//Catpure选择器视图选择
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row不完整项:(NSInteger)组件
{
//每当用户更改选择器选择时,就会触发此方法。
//名为row and component的参数表示所选内容。
conditionName=名称[行];
}
-(无效)已单击保存:(id)发件人{
ConditionsAppDelegate*appDelegate=(ConditionsAppDelegate*)[[UIApplication sharedApplication]委托];
//创建一个条件对象。
条件*c=[[Condition alloc]init];
NSInteger newId=c.getNextConditionId;
条件*cond=[[Condition alloc]initWithPrimaryKey:newId];
cond.condition\u area=txtConditionArea.text;
cond.condition_detail=txtcontditiondetail.text;
cond.condition_name=条件名称;
//添加对象
//[appDelegate addCondition:cond];
[appDelegate populateFromDatabase];
//添加到阵列中:
//[cvc.addObject:cond];
//[cvc.Conditions addObject:cond];
rvc.Conditions=[appDelegate activeConditions];
//更新TABLEVIEW
[rvc.tableView重载数据];
//释放
[第二次释放];
[c释放];
//关闭控制器。
[self.navigationController dismissViewControllerAnimated:YES完成:nil];
}
-(无效)取消\u单击:(id)发件人{
//关闭控制器。
[self.navigationController dismissViewControllerAnimated:YES完成:nil];
}
-(BOOL)文本字段应返回:(UITextField*)文本字段{
NSLog(“第159行”);
[文本字段辞职第一响应者];
返回YES;
}
-(无效)解除锁定{
[TXT区域释放];
[txtConditionDetail发布];
[条件名称选择器释放];
[super dealoc];
}
@结束
您正在设置数据源
[conditionNamesPicker setDataSource:self];
numberOfRowsInComponent: