4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テーブルのセルをクリックしたときセレクタの選択に依存して遷移先を変える

Last updated at Posted at 2015-02-25

テーブルのセルをクリックしたときセレクタの選択に依存して遷移先を変えるサンプル。

#サンプルの構成
・Navigation Controller:
・ViewController1:TableView, TableViewCell, ToolBar+Segment Controllを配置※1
・ViewController2:"ViewController A"のLabelを配置
・ViewController3:"ViewController B"のLabelを配置

・segueA:ViewController1とViewController2との間のセグエのidentifier名※2
・segueB:ViewController1とViewController3との間のセグエのidentifier名※2
・selMode:Segment Controllの変数名

#参考
※1 Storyboard - UIViewにUITableViewを追加する
※2 条件によって遷移先のシーンを変更する方法

#ソースコード

SelectorTableViewController.h
#import <UIKit/UIKit.h>

@interface SelectorTableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UISegmentedControl *selMode;
@end
SelectorTableViewController.m
#import "SelectorTableViewController.h"

@interface SelectorTableViewController ()
@end

@implementation SelectorTableViewController

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

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"testIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    // Configure the cell...
    cell.textLabel.text = @"Test";
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"didSelectRowAtIndexPath");
    NSLog(@"SegmentedControle =%d", _selMode.selectedSegmentIndex);
    NSString *segueName;
    if (_selMode.selectedSegmentIndex == 0 ) {
        segueName = @"segueA";
    } else {
        segueName = @"segueB";
    }
    [self performSegueWithIdentifier:segueName sender:self];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSLog(@"prepareForSegue");
}

#ストーリーボード

スクリーンショット 2015-02-25 23.04.33.png

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?