質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

6761閲覧

swift:UITabBarに設定したViewControllerにコードで画面遷移したい

退会済みユーザー

退会済みユーザー

総合スコア0

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2018/03/09 22:29

編集2018/03/09 23:25

iOS開発の初心者です。
【UIKit】UITabBarの使い方
こちらのブログの方法を丸写しで、Storyboardを使わないでUITabBarで画面遷移できるようにしました。
他のViewControllerでボタンを押すと、UITabBarに設定したViewControllerに画面遷移したいのですが、方法がわかりません。

Storyboard を使わずコードだけで画面を生成、遷移をしてみる
こちらの方法を真似しようとしましたが、動かすことができませんでした。

どうか教えていただけないでしょうか。
よろしくお願いします。

環境:
Xcode,9.2
Swidt 4

Swift4

1AppDelegate.swift 2 3import UIKit 4 5@UIApplicationMain 6class AppDelegate: UIResponder, UIApplicationDelegate { 7 8 var window: UIWindow? 9 10 11 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 12 { 13 14 //ViewControllerのインスタンスを生成。 15 let tab1 = OneViewController() 16 let tab2 = TwoViewController() 17 18 19 20 21 //タブを配列に代入 22 let myTabs = NSArray(objects:tab1,tab2) 23 24 //UITabControlerのインスタンスを生成 25 let tabbarController = UITabBarController() 26 27 //tabbarControllerにタブを設定する 28 tabbarController.setViewControllers(myTabs as? [UIViewController], animated: false) 29 30 //rootにtabbarControllerを設定 31 self.window!.rootViewController = tabbarController 32 33 //表示 34 self.window!.makeKeyAndVisible() 35 36 // Override point for customization after application launch. 37 return true 38 } 39 40 func applicationWillResignActive(_ application: UIApplication) { 41 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 42 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 43 } 44 45 func applicationDidEnterBackground(_ application: UIApplication) { 46 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 47 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 48 } 49 50 func applicationWillEnterForeground(_ application: UIApplication) { 51 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 52 } 53 54 func applicationDidBecomeActive(_ application: UIApplication) { 55 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 56 } 57 58 func applicationWillTerminate(_ application: UIApplication) { 59 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 60 } 61 62 63}

Swift4

1OneViewController.swift 2 3import UIKit 4 5class OneViewController: UIViewController 6{ 7 init() 8 { 9 super.init(nibName: nil, bundle: nil) 10 //背景色の設定 11 self.view.backgroundColor = UIColor.blue 12 13 //システムアイコンの設定 14 let systemIcon = UITabBarItem(tabBarSystemItem: .bookmarks , tag: 1) 15 16 //タブのアイコンの設定 17 self.tabBarItem = systemIcon 18 } 19 20 required init?(coder aDecoder: NSCoder) 21 { 22 super.init(coder: aDecoder) 23 } 24 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29    // ボタンを追加 30 var jumpButton : UIButton! 31 32 jumpButton = UIButton(frame: CGRect(x: (self.view.frame.size.width) / 2, y: (self.view.frame.size.height) / 2, width: (self.view.frame.size.width) / 4 , height: (self.view.frame.size.height) / 4 )) 33 jumpButton.setTitle("Text", for: .normal) 34 35 jumpButton.addTarget(self, action: #selector(jumpView), for: .touchUpInside) 36 // Do any additional setup after loading the view. 37 } 38 39 override func didReceiveMemoryWarning() { 40 super.didReceiveMemoryWarning() 41 // Dispose of any resources that can be recreated. 42 } 43 44  // ボタンが押された時の動作 45 @objc func jumpView(sender: UIButton) 46 { 47 print ("ボタンが押されました") 48 // ここでTwoViewControllerに画面遷移したい 49 50 let nextVC = TwoViewController() 51 let naviVC = UINavigationController(rootViewController: nextVC) 52 self.present(naviVC, animated: true, completion: nil) 53 // これだとTabBarが隠れてしまう 54 } 55 56 57 /* 58 // MARK: - Navigation 59 60 // In a storyboard-based application, you will often want to do a little preparation before navigation 61 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 62 // Get the new view controller using segue.destinationViewController. 63 // Pass the selected object to the new view controller. 64 } 65 */ 66 67} 68

Swift4

1 2import UIKit 3 4class TwoViewController: UIViewController 5{ 6 init() 7 { 8 super.init(nibName: nil, bundle: nil) 9 //背景色の設定 10 self.view.backgroundColor = UIColor.white 11 12 //システムアイコンの設定 13 let systemIcon = UITabBarItem(tabBarSystemItem: .favorites, tag: 2) 14 15 16 //タブのアイコンの設定 17 self.tabBarItem = systemIcon 18 } 19 20 required init?(coder aDecoder: NSCoder) 21 { 22 super.init(coder: aDecoder) 23 } 24 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 // Do any additional setup after loading the view. 30 } 31 32 override func didReceiveMemoryWarning() { 33 super.didReceiveMemoryWarning() 34 // Dispose of any resources that can be recreated. 35 } 36 37 38 /* 39 // MARK: - Navigation 40 41 // In a storyboard-based application, you will often want to do a little preparation before navigation 42 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 43 // Get the new view controller using segue.destinationViewController. 44 // Pass the selected object to the new view controller. 45 } 46 */ 47 48} 49

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

ごめんなさい。私が質問の勘違いをしていました。
例えば以下のような形でいかがでしょうか?

AppDelegate.swift

1 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 2 { 3 4 //ViewControllerのインスタンスを生成。 5 let tab1 = OneViewController() 6 let nv1 = UINavigationController(rootViewController: tab1) 7 let tab2 = TwoViewController() 8 let nv2 = UINavigationController(rootViewController: tab2) 9 10 //タブを配列に代入 11 let myTabs = NSArray(objects:nv1,nv2) 12 13 //UITabControlerのインスタンスを生成 14 let tabbarController = UITabBarController() 15 16 //tabbarControllerにタブを設定する 17 tabbarController.setViewControllers(myTabs as? [UIViewController], animated: false) 18 19 //rootにtabbarControllerを設定 20 self.window!.rootViewController = tabbarController 21 22 //表示 23 self.window!.makeKeyAndVisible() 24 25 // Override point for customization after application launch. 26 return true 27 } 28

OneViewController.swift

1import UIKit 2 3class OneViewController: UIViewController 4{ 5 private lazy var button: UIButton = { 6 7 let button = UIButton(type: .system) 8 button.translatesAutoresizingMaskIntoConstraints = false 9 button.setTitle("push", for: .normal) 10 button.tintColor = UIColor.white 11 button.backgroundColor = UIColor.red 12 button.addTarget(self, action: #selector(onTappedPush(_:)), for: .touchUpInside) 13 button.widthAnchor.constraint(equalToConstant: 200).isActive = true 14 button.heightAnchor.constraint(equalToConstant: 40).isActive = true 15 return button 16 }() 17 18 init() 19 { 20 super.init(nibName: nil, bundle: nil) 21 //背景色の設定 22 self.view.backgroundColor = UIColor.blue 23 24 //システムアイコンの設定 25 let systemIcon = UITabBarItem(tabBarSystemItem: .bookmarks , tag: 1) 26 27 //タブのアイコンの設定 28 self.tabBarItem = systemIcon 29 30 31 } 32 33 required init?(coder aDecoder: NSCoder) 34 { 35 fatalError("init(coder:) has not been implemented") 36 } 37 38 39 override func viewDidLoad() { 40 41 super.viewDidLoad() 42 43 navigationItem.title = "Nav1" 44 45 46 self.view.addSubview(button) 47 48 button.translatesAutoresizingMaskIntoConstraints = false 49 button.widthAnchor.constraint(equalToConstant: 200).isActive = true 50 button.heightAnchor.constraint(equalToConstant: 40).isActive = true 51 button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 52 button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 53 } 54 55 override func didReceiveMemoryWarning() { 56 super.didReceiveMemoryWarning() 57 // Dispose of any resources that can be recreated. 58 } 59 60 @objc func onTappedPush(_ sender: UIButton) { 61 let vc = TwoViewController() 62 navigationController?.pushViewController(vc, animated: true) 63 } 64 65}

TwoViewController.swift

1import UIKit 2 3class TwoViewController: UIViewController 4{ 5 init() 6 { 7 super.init(nibName: nil, bundle: nil) 8 9     //背景色の設定 10 self.view.backgroundColor = UIColor.white 11 12 //システムアイコンの設定 13 let systemIcon = UITabBarItem(tabBarSystemItem: .favorites, tag: 2) 14 15 16 //タブのアイコンの設定 17 self.tabBarItem = systemIcon 18 } 19 20 required init?(coder aDecoder: NSCoder) 21 { 22 super.init(coder: aDecoder) 23 } 24 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 navigationItem.title = "Nav2" 30 31 // Do any additional setup after loading the view. 32 } 33 34 override func didReceiveMemoryWarning() { 35 super.didReceiveMemoryWarning() 36 // Dispose of any resources that can be recreated. 37 } 38} 39

投稿2018/03/09 23:31

newmt

総合スコア1277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/03/10 01:19

newmtさん、ありがとうございます。 思っていた画面遷移ができるようになりました。 数日悩んでいた問題が一気に解決して、ホッとしています。 本当にありがとうございました。
guest

0

これでいかがでしょうか?

Swift

1 2//tabbarControllerにタブを設定する 3tabbarController.setViewControllers(myTabs as? [UIViewController], animated: false) 45tabbarController.setViewControllers([tab1, tab2], animated: false)

投稿2018/03/09 22:57

newmt

総合スコア1277

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/03/09 23:22

newmtさん、すいませんでした。 コードと文章が足りませんでした。 OneViewControllerに作成したボタンが押されると、TwoViewControllerに画面遷移したいのですが、 let nextVC = TwoViewController() let naviVC = UINavigationController(rootViewController: nextVC) self.present(naviVC, animated: true, completion: nil) で画面遷移するとTabBarが次の画面の下に隠れてしまうのです。 その質問だったのですが、文章とコードが足りませんでした。 すいません。
newmt

2018/03/09 23:32

ごめんなさい。私が質問を勘違いしていたようです。別で回答しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問