self.navigationController和pushViewController问题

时间:2012-12-11 08:24:13

标签: objective-c xcode uitableview

我无法从tableview推送到详细视图。当我单击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];
}

2 个答案:

答案 0 :(得分:0)

你试试:

[self presentModalViewController: detailViewController animated:YES];

答案 1 :(得分:0)

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