我正在创建一个 iPad 应用程序。在其中,我设置了一个显示 3 个 View 的 UITabBarController。 View 1、 View 2 和 View 3。这一切都很好。在 View 1 上,用户正在创建订单。然后他们触摸一个按钮来建立订单。这显示在模态视图中,允许用户在实际发送之前查看它。他们可以“提交”或“编辑”订单,无论哪种方式,我都会关闭模式并返回到 View 1。这也很好用。但是如果用户再次触摸“制作”订单按钮,这次加载模态视图会导致崩溃“EXC_BAD_ACCESS”。我复制了代码,就像我为应用程序中的另一个模态视图所做的一样,一次又一次地显示自己没有问题。在这一点上我很困惑,希望得到任何帮助。谢谢。调用模态的代码是:
-(IBAction) makeOrder {
NSMutableArray *orderItems = [[NSMutableArray alloc] init];
//code that populates orderItems array - removed for brevity
NSLog(@"order items count:%d", [orderItems count]);
// Create the modal view controller
PartsOrderViewController *modalController = [[PartsOrderViewController alloc] initWithNibName:@"PartsOrderView" bundle:nil];
//this is the only difference b/w this and the other modal view. The other
//modal presents as a formsheet
modalController.modalPresentationStyle = UIModalPresentationFullScreen;
modalController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
modalController.orderList = orderItems;
modalController.storeId = selectedCustomer.storeID;
modalController.customerInfo = customerInfo.text;
modalController.customerTamsId = selectedCustomer.customerTAMSID;
// show the Controller modally -- This is the line that cause the error after the second time
[self presentModalViewController:modalController animated:YES];
// Clean up resources
[modalController release];
}
它实际上进入了模式的 viewDidLoad,但是一旦运行结束就崩溃了。
这是模态的代码:
#import "PartsOrderViewController.h"
@implementation PartsOrderViewController
@synthesize customerTamsId;
@synthesize customerInfo;
@synthesize invoiceDate;
@synthesize invoiceTime;
@synthesize storeId;
@synthesize customerInfoLabel;
@synthesize invoiceDateLabel;
@synthesize invoiceTimeLabel;
@synthesize storeIdLabel;
@synthesize orderList;
@synthesize delegate;
#pragma mark -
#pragma mark View methods
-(IBAction) editOrder {
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction) submitOrder {
//code removed for brevity
}
#pragma mark -
#pragma mark View implementation methods
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.customerInfoLabel.text = self.customerInfo;
self.storeIdLabel.text = self.storeId;
self.invoiceDateLabel.text = self.invoiceDate;
self.invoiceTimeLabel.text = self.invoiceTime;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return NO;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
更新:找到解决方案:有问题的代码被标记为这样-
-(NSMutableArray *)buildOrderList {
NSMutableArray *orderItems = [[NSMutableArray alloc] init];
id cellObject = NULL;
int counter = 0;
NSEnumerator *theEnum = [self.partsList objectEnumerator];
while((cellObject = [theEnum nextObject]) != NULL)
{
GridTableCell *cell = (GridTableCell *)[self.partsListGrid cellForRowAtIndexPath:[NSIndexPath indexPathForRow:counter inSection:0]];
UILabel *lineAbbrev = (UILabel *)[cell.contentView.subviews objectAtIndex:0];
UILabel *partNo = (UILabel *)[cell.contentView.subviews objectAtIndex:1];
UITextView *orderQty = (UITextView *)[cell.contentView.subviews objectAtIndex:3];
//NSLog(@"OrderQty length: %d", [orderQty.text length]);
//NSLog(@"Part#:%@, OrderQty:%@", partNo.text, orderQty.text);
PartOrderIn *invItem = [[PartOrderIn alloc] init];
invItem.lineAbbrev = lineAbbrev.text;
invItem.partNumber = partNo.text;
invItem.orderQty = orderQty.text;
invItem.partMessage = @"";
if ([invItem.orderQty length] > 0) {
[orderItems addObject:invItem];
}
counter++;
[invItem release];
//The following three lines is what was killing it
//[lineAbbrev release];
//[partNo release];
//[orderQty release];
}
//NSLog(@"order items count:%d", [orderItems count]);
return orderItems;
}
最佳答案
冒着陈述显而易见的风险(抱歉;)您是否通过调试器逐步执行此操作?错误的访问可能是内存分配问题(再一次,很明显)。如何定义属性(是否保留 orderList?其他属性?)。检查崩溃的位置并记下属性值,使用调试器中的表达式或内存地址。我的猜测是您认为已保留的内容未保留。
关于objective-c - 调用当前 Modal ViewController 导致 “EXC_BAD_ACCESS”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4746903/