I am a newbie trying to learn ios programming.
So, I have Xcode 11.1
I deleted the main story board.
My AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = ViewController() //UINavigationController(rootViewController: ViewController())
return true
}
My ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
}
}
I removed the references of main from project settings and Info.plist
But, when I try to run the app, the ios simulator shows a black screen
If it helps, trying to follow this tutorial: https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj
Related
I am trying to make a UITabBarController and a UINavigationController programmatically. I've tried many tutorials but most use Swift 3 which is too outdated and doesn't work.
AppDelegate.swift Snippet:
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 Snippet:
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)
}
What works:
The build loads onto the simulator
What doesn't work:
The build crashes once loading onto the simulator
After crashing, the error Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee6c2ada8) apears over this line 12 of JournalTableViewController.swift Snippet
If you are going to combine a UITabBarController and a UINavigationController then you will want the tab bar controller to be the root view controller. Each tab can have its own navigation controller if required.
If you make the navigation controller the root, then as soon as you push a new view controller, the tab bar will disappear. By making the tab bar the root, you can have a series of navigation hierarchies and switch quickly between them using the tab buttons.
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()
navController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
tabBarController.viewControllers = [navController]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
There is no need for any specific code in your view controller class.
JournalTableViewController.swift Snippet:
override func viewDidLoad() {
super.viewDidLoad()
}
I think there is a typo in your snippet. In the following code, we added an intermediate vc to solve recursive problems.
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
}
Here is journal 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)
}
}
while the tableviewcontroller should be like this:
class JournalTableViewController: UITableViewController{
}
In my case, I'm making an app for real-time chat. I'm using large titles and search bar inside of my main view. However I wanted to add tabBar to my app navigationController and tabBar not working correct.
NOTE: I'm doing everything with code, please not tell me about storyboard.
Here is what supposed to be:
What is happening when I add tabBar:
AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
FirebaseApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: TabBarController())
return true
}
TabBarController.swift:
override func viewDidLoad() {
super.viewDidLoad()
let messagesController = UINavigationController(rootViewController: MessagesController())
messagesController.tabBarItem.title = "Sohbetler"
messagesController.tabBarItem.image = UIImage(named: "chats")
viewControllers = [messagesController]
}
You put two navigation one for tabbar and second for controller hide one navigation bar and your problem will solve
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
FirebaseApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let objNav = UINavigationController(rootViewController: TabBarController())
objNav.isNavigationBarHidden = true
window?.rootViewController = objNav
return true
}
try the following line in your viewcontroller where you want to show the tabbar
self.tabBarController?.tabBar.isHidden = false
With a very basic single view application, I've deleted the main storyboard file and removed any references to it. As such I'm setting the window rootViewController programmatically. However, while this displays the single view (containing a label) correctly in simulator, it displays a black screen when running it on device. Here is the only code for the app.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = DummyViewController()
window?.makeKeyAndVisible()
return true
}
I've removed the entry for Main storyboard from the info.plist file, as well as the 'Main Interface' entry in the General settings.
I'm using Swift 3 and targeting an iOS 8 device. I'm using XCode 8.3.1.
There is no output in the console, and there are no exceptions. The viewDidLoad function is even triggering on breakpoint, so the codepath seems to be running correctly.
Any ideas?
Here's the bare bones code for DummyViewController upon request.
class DummyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
General settings showing no reference to Main Interface
Here is the image for the .xib linked to DummyViewController
** The solution to get around this case is to manually specify the .xib to load for the DummyViewController **
It looks like the ViewController is not set to display anything. Unless you are using a xib (in which case you would need to load the view controller in a different way, see below), there is nothing describing how the ViewController's view should render.
To test this out, you can add the line self.view.backgroundColor = UIColor.red to the ViewController's viewDidLoad() method, then run it again on the device- if the background color turns red, then hooray! The next step will be programmatically adding a UILabel.
Loading UIViewController From a XIB
let vc = MyViewController(nibName: "xibname", bundle: nil)
Alternatively, you can mask the loading by adding a custom init inside MyViewController:
class MyViewController: UIViewController {
required init() {
super.init(nibName: "xibname", bundle: nil)
}
}
(Thank you zonily-jame for the addition of hiding it in the class)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginView : BaseClassVC = mainStoryboardIpad.instantiateViewControllerWithIdentifier("BaseClassVC") as BaseClassVC
let navigationController = UINavigationController(rootViewController: loginView)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
change Your window root as and set color
let viewController:DummyViewController = DummyViewController()
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = viewController
And change your controller
override func viewDidLoad() {
self.view.backgroundColor = UIColor.white
}
You just need to initialize viewcontroller object and implement white background color to its instance.
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
//Add below 2 lines
let vc = DummyViewController()
vc.view.backgroundColor = .white
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
I am trying to launch a UITableViewController as a RootController from AppDelegate without using Storyboards.
Here is my TasksTableViewController:
class TasksTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
The nib file is called TasksTableViewController. The view property of the TasksTableViewController is hooked up to a UITableView control on the nib.
The AppDelegate looks like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let controller = TasksTableViewController(nibName: "TasksTableViewController", bundle: nil)
window?.rootViewController = controller
window?.makeKeyAndVisible()
return true
}
All I see is a black screen. There is no data binding to the UITableView control but I was hoping to see an empty uiTableview control and not the black screen. What am I doing wrong?
UPDATE:
Here is the code that got it working:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let controller = TasksTableViewController(nibName: "TasksTableViewController", bundle: nil)
if let window = self.window {
window.backgroundColor = UIColor.blueColor()
window.rootViewController = controller
window.makeKeyAndVisible()
}
return true
}
Try something of this sort:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let controller = TasksTableViewController(nibName: "TasksTableViewController", bundle: nil)
if let window = self.window{
window.rootViewController = controller
}
}
Put in break points to confirm that it was created properly before adding it to the window. Also, there isn't a reason to instantiate a new window upon launch so don't do that.. At this point double check that the VC is in fact displayed by putting a breakpoint in viewDidAppear. Consider making TasksTableViewController a subclass of UIViewController that has a tableview inside it as this is common practice. Subclassing UITableView is usually not recommended nor is using it as a ViewController. If you laid out a custom tableview in a xib that you want to use, you can instantiate one in view did load or init on the root view controller, add it as a subview, and set the frame to cover up the entire screen. I highly recommend this approach.
I have a ViewController called MyViewController and I want to load it programmatically without a stroyboard or xib.
As a follow up squestion, I would like to load a MyTableViewController inside a navigation controller. Again, I need to do it programmatically without using Storyboard or xib.
Please help.
I was able to solve my issue by removing the Storyboard and using the following code in AppDelegate
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
let myViewController: MyViewViewController? = MyViewViewController()
self.navigationController = UINavigationController(rootViewController: myViewController!)
self.window!.rootViewController = self.navigationController
return true
}
...
}