0

我无法从表格视图推送到详细视图。当我单击 tableview 单元格时,单元格会突出显示但不会推送到详细视图。我正在使用它来转换到详细视图:

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

我读过这是一个常见问题,但不知何故无法找到解决方案。我的完整 .m 文件如下。如果有人有任何建议,那就太棒了。谢谢!

#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"title";

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

NSURL *url = [NSURL URLWithString:@"http://website.com/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [news count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}

cell.textLabel.text = textForMyLabel;
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];

[[self navigationItem] setBackBarButtonItem: newBackButton];

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.title = [[news objectAtIndex:indexPath.row] objectForKey:@"name"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
4

2 回答 2

0

您可以使用 UINavigationController 执行推送,如果您的 Controller 是 UINavigationController,上述代码将起作用。由于您尝试集成两个项目,只需检查 RootViewController 是否为 UINavigationController。

于 2012-12-11T08:37:13.973 回答
0

你试试:

[self presentModalViewController: detailViewController animated:YES];
于 2012-12-11T08:33:38.020 回答