
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

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/10 01:19