0

画面左側のMasterViewTableViewCellをそれぞれタップすると、画面右側のDetailViewに背景色が違うViewをそれぞれ表示させようとしているのですが、現在はTableViewCellをタップするとMasterView内で背景色の違うViewに画面遷移してしまい、DetailViewの方には何も変化がない状態です。
どうすればiPhoneの設定アプリのように画面左側のセルを押すとその内容が画面右側に表示されるようになるでしょうか?
下記に現在のコードとStoryboardの内容を記します。
どなたか分かる方がいれば教えていただきたいです。
すみませんが、よろしくお願いします。

@interface TableViewController ()

@end

@implementation TableViewController

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

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

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSString *str = @"color";
    cell.textLabel.text = [str stringByAppendingString:[NSString stringWithFormat:@"%ld", (long)indexPath.row]];

    return cell;
}

-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    if (indexPath.row == 0) {
        PinkViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        GreenViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"GreenViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end

画像の説明をここに入力

1 件の回答 1

1

showDetailViewControllerを使えば、期待されるような動作になるかと思います。

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController* vc = nil;
    if (indexPath.row == 0) {
        vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    } else {
        vc = [storyboard instantiateViewControllerWithIdentifier:@"GreenViewController"];
    }
    [self.splitViewController showDetailViewController:vc sender:nil];
}

こんな感じでどうでしょうか?

1
  • ご回答ありがとうございます。ご提示のコードの通りshowDetailViewControllerを使うことで右側の画面が変わるようになりました!誠にありがとうございました。
    – tarofess
    2015年7月3日 7:50

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。