iphone - 在 TableView 之间来回切换时 EXC_BAD_ACCESS 错误

标签 iphone ios asihttprequest nsobject

简要概述我正在尝试做的事情。

我正在 UITableViewController 子类中使用 tableView:didSelectRowAtIndexPath: 方法,该方法从该 View 中捕获行选择,如下所示...

//..... inside tableView:didSelectRowAtIndexPath:
if (indexPath.section == 0) {
        //--- Get the subview ready for use
        VehicleSearchResponseTableViewController *vehicleSearchResponseTableViewController = [[VehicleSearchResponseTableViewController alloc] initWithNibName:@"VehicleSearchResponseTableViewController" bundle:nil];
        // ...
        //--- Sets the back button for the new view that loads
        self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:vehicleSearchResponseTableViewController animated:YES];

        if(indexPath.row == 0) {
            vehicleSearchResponseTableViewController.title = @"Mans";

            EngineRequests *engineRequest = [[EngineRequests alloc] init];
            [engineRequest getMans]; 
            [engineRequest release]; 
        }
        if(indexPath.row == 1) {
//.... etc etc

正如你在这个方法中看到的,我设置了一些东西,将新 View 推送到 View 堆栈上并更改后退按钮文本,然后我开始捕获不同的行,然后在 nsobject 的子类中启动一个方法,其中我想让我的所有连接/请求都继续进行。

在我的 NSObject 中,我对不同的单元格有几种不同的方法,您可以在 UITableViewController 上选择它们,基本上它们指定不同的字符串,然后初始化我的 ASIHTTPRequest 包装器以连接到 php 脚本并捕获将要执行的所有数据。从数据库返回.. NSObject 看起来像这样。

//.... NSObject.m
- (IBAction) getMans
{
    NSString *mansString = [[NSString alloc] initWithString:@"mans.php"];
    [self grabURLInBackground:mansString];
    [manusString release];
}
//....cont....
//--- Connect to server and send request ---------------->>
- (IBAction)grabURLInBackground:(NSString *)setUrlString
{
    NSString *startURL = [NSString stringWithFormat:@"http://127.0.0.1:8888/CodeTest/%@", setUrlString];
    NSURL *url = [NSURL URLWithString:startURL];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString]; //Pass request text from server over to NSString 
    NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; //Create NSData object for Parser Delegate and load with responseString
    NSLog(@"stuff %@",responseData);

}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@", error);
}

从这里我想将从 requestFinished 方法获取的数据传递回新推送的 UITableView..但是,在我能够得到这么远之前,我需要解决一个错误...如果我运行模拟器并在 View 之间来回单击(带有单元格的主 UITableViewController,然后是我想要放置数据的新弹出的 View ),应用程序崩溃并在 ma​​in.m< 中弹出错误/strong> 线程 1:程序接收信号 EXC_BAD_ACCESS ..我只是不知道是什么原因,因为据我所知,我的代码并没有那么糟糕。

此外,当我调试我的应用程序时,我注意到,一旦grabURLInBackground方法完成,它就会弹回getMans方法,然后返回到UITableViewController并继续执行if语句,完全忽略requestFinished和requestFailed方法,我只是无法弄清楚为什么。

我想我不确定我是否在正确的地方调用了我需要使用的方法和函数,因此如果您对我如何改进有任何建议或答案,或者您是否知道我的错误来自哪里,那么不胜感激。

最佳答案

上面的代码有一些问题,但我猜你的错误访问异常是由于 EngineRequests 的处理和 AsiHttpRequest 的使用造成的。

代码在这里

EngineRequests *engineRequest = [[EngineRequests alloc] init];
[engineRequest getMans]; 
[engineRequest release]; 

有效地创建一个对象,然后在 getMans 完成运行后立即释放。

然后在engineRequest对象内这段代码

[request setDelegate:self];
[request startAsynchronous];

请求 AsiHttpRequest 在请求完成后通知几乎肯定已释放的对象

这里可能还有其他问题,但我会首先进行重组,尝试保留该对象,直到至少在它收到来自 AsiHttpRequest 的响应之后。

关于iphone - 在 TableView 之间来回切换时 EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7265554/

相关文章:

iphone - 在设备和模拟器上进行调试时,特设 iPhone SIGSEGV 崩溃

iphone - 一次向多个对象发送消息(objective-c)

iphone - 如何阻止单元格缩进 - shouldIndentWhileEditingRowAtIndexPath 无效

iphone - 有没有办法在不向 Apple 注册的情况下在 iPod touch 上测试 iPhone 应用程序?

javascript - 获取所选文本相对于主 div 的范围(包含所有文本)

ios - NSURLConnection 的分块传输编码有哪些替代方案

ios - 应用程序运行时无法将蓝牙耳机与AVAudioSessionCategoryPlayAndRecord连接

ios - 如何使用 AVCaptureSession 从 CMSampleBuffer 获取 UIImage

ios - 如何将 NSURL session 进度添加到 TableView 单元格

objective-c - Objective-C - 从 WiFi 切换到 3G,反之亦然