segueを使わないで画面遷移

view1からview2に遷移する場合

ViewController1.m

- (IBAction)btnPage2:(UIButton *)sender {
    UIStoryboard* myStoryboard = self.storyboard;

    ViewController2* viewController2 = [myStoryboard instantiateViewControllerWithIdentifier:@"view2"];

    viewController2.dataString = @"aaa";
    
    [self presentViewController:viewController2 animated:YES completion:nil];
}

 

view2からの戻り遷移

ViewController2.m

- (IBAction)btnBack:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

 

ポイント

ViewControllerのIDは事前に設定しておく必要あり。

ボタンのクリックイベントとメソッドの紐付けは、control+ドラッグで行なっている。

遷移したいタイミングで、遷移が可能なので、segue を使うより感覚的に、わかりやすい。

思ったよりも簡単だった。