单击“后退”按钮时 UINavigationController 应用程序崩溃按钮

发布于 2024-10-16 17:56:00 字数 3434 浏览 1 评论 0原文

希望有人能帮助我。

didSelectRowAtIndexPath 方法中,我正在加载并推送详细信息视图控制器,当您点击详细信息视图中的“后退”按钮返回到根视图时,应用程序将崩溃。没有错误日志或任何东西。

我 95% 确信这与过早发布“rides”对象有关,但无法弄清楚。

感谢您的帮助!

#import "RidesViewController.h"
#import "RideDetailViewController.h"
#import "JSON.h"

@implementation RidesViewController
@synthesize rides;

#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {

    [super viewDidLoad];

    self.tableView.backgroundView   = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table-view-background.png"]];

    rides                           = [[NSDictionary alloc] init];

    NSString        *filePath       = [[NSBundle mainBundle] pathForResource:@"rides" ofType:@"JSON"];
    NSData          *jsonData       = [NSData dataWithContentsOfFile:filePath];  
    NSString        *responseString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSDictionary    *results        = [responseString JSONValue];

    rides                           = [results objectForKey:@"rides"];

    [rides retain];
}

#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [rides count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) cell   = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    cell.textLabel.text     = [[rides valueForKey:@"title"] objectAtIndex:indexPath.row];
    cell.accessoryType      = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    RideDetailViewController *rideDetailViewController = [[RideDetailViewController alloc] initWithNibName:@"RideDetailViewController" bundle:nil];

    NSString        *aTitle         = [[NSString alloc] initWithString:[[rides valueForKey:@"title"] objectAtIndex:indexPath.row]];
    NSString        *aImagePath     = [[NSString alloc] initWithString:[[rides valueForKey:@"imagePath"] objectAtIndex:indexPath.row]];
    NSString        *aDescription   = [[NSString alloc] initWithString:[[rides valueForKey:@"description"] objectAtIndex:indexPath.row]];
    NSString        *aVideoPath     = [[NSString alloc] initWithString:[[rides valueForKey:@"videoPath"] objectAtIndex:indexPath.row]];

    [rideDetailViewController initWithTitle:aTitle imagePath:aImagePath description:aDescription videoPath:aVideoPath];

    [self.navigationController pushViewController:rideDetailViewController animated:YES];

    [rideDetailViewController   release];
    [aTitle                     release];
    [aImagePath                 release];
    [aDescription               release];
    [aVideoPath                 release];
}

#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {

    [super  didReceiveMemoryWarning];
}

- (void)dealloc {
    [super  dealloc];
}


@end

hoping someone can help me.

In the didSelectRowAtIndexPath method, I am loading and pushing detail view controller, when you tap the "back" button in the detail view to go back to the root view, the app will crash. No error logs or anything.

I'm 95% sure it's got something to do with the "rides" object being released too early, but can't figure it out.

Thanks for your help!

#import "RidesViewController.h"
#import "RideDetailViewController.h"
#import "JSON.h"

@implementation RidesViewController
@synthesize rides;

#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {

    [super viewDidLoad];

    self.tableView.backgroundView   = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table-view-background.png"]];

    rides                           = [[NSDictionary alloc] init];

    NSString        *filePath       = [[NSBundle mainBundle] pathForResource:@"rides" ofType:@"JSON"];
    NSData          *jsonData       = [NSData dataWithContentsOfFile:filePath];  
    NSString        *responseString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSDictionary    *results        = [responseString JSONValue];

    rides                           = [results objectForKey:@"rides"];

    [rides retain];
}

#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [rides count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) cell   = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    cell.textLabel.text     = [[rides valueForKey:@"title"] objectAtIndex:indexPath.row];
    cell.accessoryType      = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    RideDetailViewController *rideDetailViewController = [[RideDetailViewController alloc] initWithNibName:@"RideDetailViewController" bundle:nil];

    NSString        *aTitle         = [[NSString alloc] initWithString:[[rides valueForKey:@"title"] objectAtIndex:indexPath.row]];
    NSString        *aImagePath     = [[NSString alloc] initWithString:[[rides valueForKey:@"imagePath"] objectAtIndex:indexPath.row]];
    NSString        *aDescription   = [[NSString alloc] initWithString:[[rides valueForKey:@"description"] objectAtIndex:indexPath.row]];
    NSString        *aVideoPath     = [[NSString alloc] initWithString:[[rides valueForKey:@"videoPath"] objectAtIndex:indexPath.row]];

    [rideDetailViewController initWithTitle:aTitle imagePath:aImagePath description:aDescription videoPath:aVideoPath];

    [self.navigationController pushViewController:rideDetailViewController animated:YES];

    [rideDetailViewController   release];
    [aTitle                     release];
    [aImagePath                 release];
    [aDescription               release];
    [aVideoPath                 release];
}

#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {

    [super  didReceiveMemoryWarning];
}

- (void)dealloc {
    [super  dealloc];
}


@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撞了怀 2024-10-23 17:56:00

在标头中定义您的 NSString,然后将它们放入 cellForRowAtIndexPath:

Define your NSString's in your header, then place them in cellForRowAtIndexPath:

岛徒 2024-10-23 17:56:00

只需从 viewdidload剪切整个内容并将其粘贴viewwillapper 方法上即可。

just Cut whole thing from viewdidload and paste this on viewwillapper method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文