I am new to Objective C and Swift. Here I am having an exception as 'NSInternalInconsistencyException' in my sample program. I do the following steps and this is my first program in Swift and I am not familiar in Objective C.
Create an empty application name as SampleTesting
Created a CheckingController.xib
Created a AppDelegate.swift
Created a CheckingController.swift
CheckingController.xib:
I have created a Sample Button.
AppDelegate.swift:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
var mainController: CheckingController? = CheckingController(nibName: "CheckingController", bundle: nil)
self.navigationController = UINavigationController(rootViewController: mainController)
self.window!.rootViewController = self.navigationController
return true
}
func applicationWillResignActive(application: UIApplication) {
}
CheckingController.swift:
import Foundation
import UIKit
class CheckingController: UIViewController{
init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Need some guidance on what could be going wrong here and how to resolve it.
Thanks.
I followed the below steps and i fixed the error:
Open the XIB file causing problems
Click on file’s owner icon on the left bar (top one, looks like a yellow outlined box)
If you don’t see the right-hand sidebar, click on the third icon above “view” in your toolbar. This will show the right-hand sidebar
In the right-hand sidebar, click on the third tab–the one that looks a bit like a newspaper
Under “Custom Class” at the top, make sure Class is the name of the ViewController that should correspond to this view. If not, enter it In the right-hand sidebar, click on the last tab–the one that looks like a circle with an arrow in it
You should see “outlets” with “view” under it. Drag the circle next to it over to the “view” icon on the left bar (bottom one, looks like a white square with a thick gray outline
Save the xib and re-run
Refered here
Related
So I've created a new iOS single view application with no core data, no test and then I deleted Main.storyboard and the launch screen storyboard.
I want to do as much in code as I can so I can understand what I'm doing (since I'm just learning iOS programming). The thing is, that I just cant fill the whole iPhone X screen with a background image. I've tried this first using storyboards and had no problem whatsoever. Am I missing something?
// AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainController = MainController()
window?.rootViewController = mainController
window?.makeKeyAndVisible()
return true
}
}
// MainController.swift
import UIKit
class MainController: UIViewController {
let background: UIImageView = {
let image = UIImageView(image: #imageLiteral(resourceName: "bg"))
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
}
extension MainController {
override func viewDidLoad() {
super.viewDidLoad()
setupBackground()
}
}
extension MainController {
func setupBackground() {
view.addSubview(background)
NSLayoutConstraint.activate([
background.leadingAnchor.constraint(equalTo: view.leadingAnchor),
background.trailingAnchor.constraint(equalTo: view.trailingAnchor),
background.topAnchor.constraint(equalTo: view.topAnchor),
background.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}
To summarize the comments above, you need to use a launch storyboard in order for iOS to give you a UIScreen that covers the device's full bounds.
If you don't already have a launch storyboard, create one in Xcode 11 via File > New > File... > Launch Screen. Then make sure to select that launch storyboard under your app target > General > Launch Screen File.
since I've been unable to do even one succesfull merge with storyboard, I'm trying to switch to xib files. I'm trying to set one xib file as initialViewController as you do in storyboard, so first I've disabled that property in storyboard. Following some examples from google, I've made an extension that is supposed to return a xib file:
extension UIViewController {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIViewController? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIViewController
}
and then called that class function from the app delegate and try to load the xib file as root view controller like this:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let mainView = UIViewController.loadFromNibNamed("MainView")
//window?.addSubview(mainView!)
self.window?.rootViewController = mainView
return true
}
My xib file name is MainView.xib, I have a UIView and a MainViewController on it, MainViewController inherits from ViewController and it's still empty:
Whatever I try I allways get the same error message:
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
You still have the project set up to use the Main.storyboard file. In the project / general tab, you need to clear this section:
If you do this you will also need to create the UIWindow yourself and set the app delegate's window property, as this is not created if you don't use a storyboard. See How to create an Empty Application in Xcode 6 without Storyboard for details.
What's the absolute bare minimum code to create a project that creates a UIView and shows it on the screen of an iOS device?
Without Storyboards, templates or any other contrivance. Just the barest presentation of a UIView.
I appreciate the templates are there to make things "easier", but for the sake of comprehending how everything works together, I'd like to try to conceive the frameworks and their relationships with the OS in the barest, truly code based form.
i.e. without Storyboards and all their processes. Just purely in code.
I've tried using google to find something with someone discussing iOS in an holistic manner, so as to view Views with clarity... but it's all about "ease" rather than understanding, so far as I can see.
With Swift.
// All puns were non-intential byproducts of irritation.
EDIT:::
How do I even start a new Project without a View Controller, Storyboard etc? There seems to be only storyboard based templates, and no way to start a "blank" project.
barest minimum would be this, just place this in your app delegate, delete the other .swift files, and this should be the barest you need:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
rootViewController = UINavigationController(rootViewController: ViewController())
self.window!.rootViewController = rootViewController
self.window!.makeKeyAndVisible()
return true
}
}
this requires Zero storyboards, BUT if you want add a VIEW to all of this and have control over that uiview, then do this:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var firstVC = UIViewController()
var firstVCView = UIView()
firstVCView.backgroundColor = UIColor.redColor()
firstVC.view = firstVCView
rootViewController = UINavigationController(rootViewController: firstVC) self.window!.rootViewController = rootViewController
self.window!.makeKeyAndVisible()
return true
}
}
i can show you from the start, ill start a brand new project and strip it:
here's how:
select sinlge view application project
go into info.plist file, delete these two entries:
Then, delete the storyboard, and the nib.
Keep the "ViewController.swift" file
place this in your AppDelegate.swift:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
rootViewController = UINavigationController(rootViewController: ViewController())
self.window!.rootViewController = rootViewController
self.window!.makeKeyAndVisible()
return true
}
}
then, do this, click no the button "use asset catalog"
the click on the pop up box that you want to use an asset catalog, then make sure this field is blank, where it says "launch screen file"
compile and run your project
A completely empty application with an app delegate, in didFinishLaunchingWithOptions this will present a view:
self.window.rootViewController = [UIViewController new];
there is a view in UIViewController
I'm trying to instantiate a UINavigationController with a rootViewController in my AppDelegate. I've looked on this website, but all the examples were from Objective-C or used storyboards (which I'm trying to get away from). 'HomeScreenController` is the root view of the application.
Edit: this shows up in the console
2015-07-18 14:42:25.376 FastFactsSwift[4749:343495] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main'
- perhaps the designated entry point is not set?
How do I fix this?
The following code results in just a black screen:
AppDelegate.swift:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window?.rootViewController = UINavigationController(rootViewController: HomeScreenController())
return true
}
...
HomeScreenController.swift:
import UIKit
class HomeScreenController: UIViewController, UISearchBarDelegate, UITableViewDelegate{
override func viewDidLoad(){
super.viewDidLoad()
var width = self.view.viewWidth
var height = self.view.viewHeight
//Add stuff to the view
}
...
Why is HomeScreenController showing up as a black screen?
The problem was not configuring AppDelegate to not use a storyboard.
How do I create a new Swift project without using Storyboards?
How can I present a view controller from my AppDelegate and have a Navigation bar added to that view with a back button to the previous view? I need to do this programmatically from my AppDelegate. Currently I can push a controller from there, but it doesn't act like a segue. It doesn't add a nav bar with a back button. Now I know I should be able to add one myself, but when I do it gets hidden. Currently I'm using pushViewController(), but I imagine that's not the best way to do it.
I had something that I think is similar, if not the same:
HIGH LEVEL VIEW
The general composition of my App (thus far, and specific to the issue at hand - note: details about classes provided for context, not required for resolution) is as follows:
UIViewController (ViewController.swift) embedded in a UINavigationController
Buttons on UIViewController segue to a view with a custom class:
ExistingLocationViewController - subclass of:
UITableViewController
One of the buttons (Add New Location) in the UINavigationController's Toolbar segues to view with another custom class:
NewLocationViewController - subclass of:
UIViewController
CLLocationManagerDelegate
UITextFieldDelegate
There are a number of other items here, but I believe the above is sufficient as the foundation for the issue at hand
RESOLUTION
In order to preserve the navigation-bar (and tool-bar) going both forward and back - I have the following code in my custom classes (note: the following is Swift-3 code, you may have to adjust for Swift-2):
override func viewDidLoad() {
super.viewDidLoad()
//...
navigationController?.isNavigationBarHidden = false
navigationController?.isToolbarHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated) // #=# not sure if this is needed
navigationController?.isNavigationBarHidden = false
navigationController?.isToolbarHidden = false
}
You could actually omit the last two lines in viewWillDisappear, or perhaps even omit the entire override function
The net result (for me) was as depicted below:
Hope that helps.
If you want add a NavigationController in appDelegate you can do it like this,in this way,your viewcontroller is load from storyboard
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let vc = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("vc") as! ViewController
let nav = UINavigationController(rootViewController: vc)
self.window?.rootViewController = nav
self.window?.backgroundColor = UIColor.whiteColor()
self.window?.makeKeyAndVisible()
return true
}