iPhone打开表视图项目上的多个视图控制器单击

时间:2013-11-22 13:14:48

标签: ios iphone objective-c tableview

是否可以根据女巫表视图单元格用户点击打开不同的视图控制器?我试着这样做:

[self presentViewController:obj animated:YES completion:nil];

但是当出现下一个视图时,没有导航栏,我无法返回到桌面视图。

修改

这是我正在使用的MasterViewController类

#import "MasterViewController.h"

@interface MasterViewController () {
    NSArray *viewArray;
}
@end

@implementation MasterViewController
@synthesize items,itemImges;

- (void)awakeFromNib
{
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7" options:NSNumericSearch] != NSOrderedAscending) {
        self.preferredContentSize = CGSizeMake(320.0, 480.0);
    }
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
    }
    self.title = NSLocalizedString(@"MasterTitle",@"Options:");

    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    items = [NSArray arrayWithObjects:@"Media Explorer",@"Live TV",@"Settings",nil];
    itemImges = [NSArray arrayWithObjects:
             [UIImage imageNamed:@"listicon_guide.png"],
             [UIImage imageNamed:@"listicon_livetv.png"],
             [UIImage imageNamed:@"listicon_settings.png"],
             nil];

// Do any additional setup after loading the view, typically from a nib.
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

    MediaExpDetailViewController *DVCA = [self.storyboard instantiateViewControllerWithIdentifier:@"MediaExpDetailViewController"];
    LiveTVDetailViewController *DVCB = [self.storyboard instantiateViewControllerWithIdentifier:@"LiveTVDetailViewController"];
    SettingsDetailViewController *DVCC = [self.storyboard instantiateViewControllerWithIdentifier:@"SettingsDetailViewController"];

    //Create Array of views
    viewArray = [NSArray arrayWithObjects:DVCA, DVCB, DVCC, nil];
}

#pragma mark - Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return items.count;
}

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

    NSString *object = items[indexPath.row];
    UIImage *image = itemImges[indexPath.row];
    cell.textLabel.text = [object description];
    cell.imageView.image = image;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //for iPad
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        //something goes here
    }
    else { //for iPhone
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        MediaExpDetailViewController *objSynergy = (MediaExpDetailViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"MediaExpDetailViewController"];
        [self.navigationController pushViewController:objSynergy animated:YES];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}

@end

3 个答案:

答案 0 :(得分:10)

首先在Interface Builder中为Next View控制器设置Storyboard ID,然后。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Check Row and Select Next View controller
   if (indexPath.row == 1)
   {     
       // Push Selected View
       UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"StoryboardID"];
       [self.navigationController pushViewController:view1 animated:YES];
   }
}

答案 1 :(得分:0)

那是因为

[self presentViewController:obj animated:YES completion:nil];

以模态方式呈现新视图控制器(在现有可见控制器之上)。如果您想使用导航控制器推送到新的视图控制器,您将需要使用它。当然,您需要确保您正在推动的视图控制器嵌入在UINavigationController中。

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

回答你的第一个问题,是的,这绝对是可能的。只需在didSelectRowAtIndexPath: UITableViewDelegate方法中添加一些条件逻辑。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (someCondition) {
        [self.navigationController pushViewController:obj animated:YES];
    }else{
        [self.navigationController pushViewController:otherObj animated:YES];
    }
}

答案 2 :(得分:0)

默认情况下,模态显示的视图控制器没有导航栏。你必须将它们嵌入到UINavigationController中才能拥有它们。您还应该实现如何自己解除显示的视图控制器,在适当的时候调用 dismissViewControllerAnimated

但是,如果您不需要模态功能,我建议推送您的视图控制器(使用 pushViewControllerAnimated ),而不是以模态方式呈现它们。