gpt4 book ai didi

ios - UIPickerView上的EXC_BAD_ACCESS-为什么需要一些指导

转载 作者:行者123 更新时间:2023-12-01 18:53:41 25 4
gpt4 key购买 nike

运行项目并尝试更改选择器时,出现exc_bad_access错误。

错误发生在

 - (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

最佳答案

请将名称属性的assign属性更改为retain。还有其他数组或从NSObject继承的对象。它是一个对象,您将其保留为assign属性。仅将分配用于原始数据类型。试试这个,让我知道。

关于ios - UIPickerView上的EXC_BAD_ACCESS-为什么需要一些指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com