我正在尝试以编程方式制作public class FragmentBuyHouse extends Fragment {
private int[] imageResId;
private int fragnumber;
BuyHousesActivity bha = new BuyHousesActivity();
List<Integer> classes = bha.getClasses();
...
}
和UITabBarController
。我已经尝试了许多教程,但是大多数教程都使用了Swift 3,但它太过时且无法使用。
AppDelegate.swift代码段:
UINavigationController
JournalTableViewController.swift代码段:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let journalVC = JournalTableViewController()
let navController = UINavigationController(rootViewController: journalVC)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
有效的方法:
什么不起作用:
一旦加载到模拟器,构建就会崩溃
崩溃后, JournalTableViewController.swift代码段
var tabBarCnt = UITabBarController()
override func viewDidLoad() {
super.viewDidLoad()
tabBarCnt = UITabBarController()
tabBarCnt.tabBar.barStyle = .black
let journalVC = JournalTableViewController()
journalVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
tabBarCnt.viewControllers = [journalVC]
self.view.addSubview(tabBarCnt.view)
}
上出现错误Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee6c2ada8)
答案 0 :(得分:1)
如果要结合使用UITabBarController
和UINavigationController
,则希望选项卡栏控制器成为根视图控制器。如果需要,每个选项卡都可以有自己的导航控制器。
如果将导航控制器设为根目录,则一旦按下新的视图控制器,选项卡栏就会消失。通过将标签栏设为根,您可以拥有一系列导航层次结构,并使用标签按钮在它们之间快速切换。
AppDelegate.swift
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let journalVC = JournalViewController()
let navController = UINavigationController(rootViewController: journalVC)
let tabBarController = UITabBarController()
journalVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
tabBarController.viewControllers = [journalVC]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
视图控制器类中不需要任何特定的代码。
JournalTableViewController.swift代码段:
override func viewDidLoad() {
super.viewDidLoad()
}
答案 1 :(得分:0)
我认为您的代码段中有错字。在下面的代码中,我们添加了一个中间的vc来解决递归问题。
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let journalVC = JournalViewController()
let navController = UINavigationController(rootViewController: journalVC)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
这是日记vc:
class JournalViewController: UIViewController{
var tabBarCnt = UITabBarController()
override func viewDidLoad() {
super.viewDidLoad()
tabBarCnt = UITabBarController()
tabBarCnt.tabBar.barStyle = .black
let journalVC = JournalTableViewController()
journalVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
tabBarCnt.viewControllers = [journalVC]
self.view.addSubview(tabBarCnt.view)
}
}
tableviewcontroller应该是这样的:
class JournalTableViewController: UITableViewController{
}