web-dev-qa-db-ja.com

Swiftを使用してあるView Controllerから別のView Controllerに移動する方法

ビューコントローラ間を移動したいのですが。次のObjective-CコードをSwiftに変換する方法を教えてください。

UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController pushViewController:navi animated:YES];
75
sathish

2番目のView Controller用のSwiftファイル(SecondViewController.Swift)を作成し、適切な関数で次のように入力します。

let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)

スイフト2+

let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController
self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)

スイフト4

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)
179

私の経験ではnavigationControllerはnilでしたので、私は自分のコードをこれに変更しました:

let next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)

ViewControllerのStoryBoard IdStoryBoard - > identity inspectorに設定することを忘れないでください

32
Mojtabye

戻るボタンを表示したくない場合(これは私の場合ですが、私はユーザーがログインした後に表示したかったので)、ナビゲーションコントローラのルートを設定する方法は次のとおりです。

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
        let navigationController = UINavigationController(rootViewController: vc)
        self.presentViewController(navigationController, animated: true, completion: nil)
16
Keith Holliday

Swift 3.01

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "Conversation_VC") as! Conversation_VC
self.navigationController?.pushViewController(secondViewController, animated: true)
15
Maksim Kniazev

Swift 4.0では

var viewController: UIViewController? = storyboard().instantiateViewController(withIdentifier: "Identifier")
var navi = UINavigationController(rootViewController: viewController!)
navigationController?.pushViewController(navi, animated: true)
6
Rahul Fate

Swift 4.1とXcode 10では

ここでAddFileViewControllerはセカンドビューコントローラです。

ストーリーボードIDはAFVCです

let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
self.present(next, animated: true, completion: nil)

//OR

//If your VC is DashboardViewController
let dashboard = self.storyboard?.instantiateViewController(withIdentifier: "DBVC") as! DashboardViewController
self.navigationController?.pushViewController(dashboard, animated: true)

必要ならばthread。を使ってください。

例:

DispatchQueue.main.async { 
    let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
    self.present(next, animated: true, completion: nil) 
}

移動したい場合しばらくしてから

例:

//To call or execute function after some time(After 5 sec)
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
    let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
    self.present(next, animated: true, completion: nil) 
} 
2
iOS

Swift

let secondviewController:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: "StoryboardIdOfsecondviewController") as? SecondViewController

self.navigationController?.pushViewController(secondviewController, animated: true)
2
SAURAV JUSTIN
let objViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationController?.pushViewController(objViewController, animated: true)
1
Davender Verma

スウィフト3

let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController        
self.navigationController?.pushViewController(nextVC, animated: true)
1
Zeeshan

Swift 4

ナビゲーションコントローラーを押して画面を切り替えることができます。最初にナビゲーションコントローラーを設定する必要がありますIViewController

let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourStoryboardID") as! swiftClassName

self.navigationController?.pushViewController(vc, animated: true)
0

ザマリン(C#)

NavigationController.PushViewController(new HomePage(), true);

Destination ViewControllerを渡します。

迅速

navigationController?.pushViewController(HomePage(), animated: true);
0
Shanu Singh