tableView関連めも

UItableViewでUITableCellの選択時に別ページに移動

1.移動させたいページ同士をstoryboardで結ぶ。(modal)
2.segueにID設定する
3.ViewController.m
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [self performSegueWithIdentifier:@"設定したsegueのID" sender:self];
}

移動先ページから戻るボタンで元ページに戻る

1.ViewController.m 移動元に関数設置する
//戻る用メソッド
- (IBAction)returnMainView:(UIStoryboardSegue *)segue
{
    ;
}
2.移動先のページから戻る時はstoryboard上からUIButtonとexitを結びつける-
  • exitに関数がでないときはxcodeを再起動させると出る

別のviewcontrollerにパラメーターを渡す

文字列でも配列でもハッシュでも何でも渡せる。
渡したいときは3つのファイルをいじる。

1.ViewController.m
  • importに渡す先のヘッダーを入れる
  • 変数を設定する
#import "detailPage.h"

...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"toSecondPage"]) {
        
        //遷移先にデータをパラメーターを渡す
        NSIndexPath *indexPath = [_myTableView indexPathForSelectedRow];
        detailPage *vc = [segue destinationViewController];
        
        //NSStringを渡す
        vc.receiveString = items[indexPath.row];

        //NSArrayを渡す
        vc.receiveArray = @[@"a",@"b",@"c"];
        
        //NSDictionaryを渡す
        vc.receiveDict = @{@"key":@"unko",@"key2":@"value2"};

    }
}
2.SecondPage.h
@property NSString *receiveString;
@property NSArray *receiveArray;
@property NSDictionary *receiveDict;

@end
3.SecondPage.m
...

@synthesize receiveString;
@synthesize receiveArray;
@synthesize receiveDict;

...

- (void)viewDidLoad
{
    [super viewDidLoad];

..
    
    NSLog(@"string=[%@]",receiveString);
    NSLog(@"array=[%@]",receiveArray[0]);
    NSLog(@"dictionary=[%@]",receiveDict[@"key"]);

..    
    
    
}