I am trying to instantiate a view controller from the nib file like following :
class TNAChallengerHandler:ChallengeHandler {
var controller : ViewController
// Default initializer
init(realm iRealm: String, controller iController : ViewController) {
println("Default initializers")
self.controller = iController;
super.init(realm: iRealm)
}
// Convience initializer
convenience init() {
println("Convience initializers")
let vc = ViewController(nibName: "ViewController", bundle: nil)
self.init(realm: "SingleStepAuthRealm", controller:vc)
}
Executing the codes and I am getting
iOS_SingleBasedAdapterAuthentication[40828:5708548] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/tonytran/Library/Developer/CoreSimulator/Devices/6B797313-635F-4ABD-B3C6-A1D9DF3F1E85/
data/Containers/Bundle/Application/FDC3FD2B-6E26-4654-AC0F-ED3F44DDC5F6/iOS_SingleBasedAdapterAuthentication.app> (loaded)' with name 'ViewController''
I double check in the storyboard, the custom class of the class is already changed to ViewController like below and the name is as same as ViewController.swift
Any thoughts about this. All comments are welcomed here.
You don't have a nib file named ViewController.
You have a storyboard. A storyboard is compiled into a set of nib files, but you don't access those nib files directly. Instead, you need to give your view controller a Storyboard ID. Then you can ask the storyboard to instantiate that view controller.
Let's say you use “GetData” as the Storyboard ID:
and I assume your storyboard is named Main.storyboard. Then you can instantiate the view controller like this:
convenience init() {
println("Convenience initializers")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("GetData")
self.init(realm: "SingleStepAuthRealm", controller: vc)
}
That said, I'm not sure it makes a lot of sense to instantiate a view controller by name when that view controller is also used as the root view controller of a UINavigationController. Are you sure you want to instantiate a new view controller here?
Related
I want to make view controller at run time with the nib name like this,
But this is being crashed.
Error
[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageViewApp.
let vc = UIViewController(nibName: nibName, bundle: nil)
When I try with the static values like this
let vc = MyViewController(nibName: "MyViewController", bundle: nil), it is working.
This is the IBOutlet in MyViewController
I googled it but haven't found enough solution please let me know the solution.
You're initializing a UIViewController and UIViewController has no imageViewApp property. You need to initialize your own subclass where you define that property, as you do in your second example.
Try something like this:
let vc = MyViewController(nibName: nibName, bundle: nil)
If your nib file has the same name as your class, you don't have to specify it:
let vc = MyViewController(nibName: nil, bundle: nil)
Remove imageViewApp property IBOutlet from that view controller and give that ImageView a tag and access it with the tag.
I am getting this error:
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier NavigationNodeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I am using two cells with the same class "NavigationItemCell" and two identifiers "NavigationNodeCell" and "NavigationLinkCell".
And I create a cell with e.g.:
let cell: NavigationItemCell = self.tableView.dequeueReusableCellWithIdentifier("NavigationNodeCell", forIndexPath: indexPath) as! NavigationItemCell
This problem has been there before me, e.g. Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
As I understand (e.g. the anser of Sebastian Borggrewe) it should be enough to register a UITableViewCell in the story board with its custom class and an identifier.
That's exactly what I did and I am still getting this error.
I tried to use two different classes but it did not solve the error.
I also tried to register my cells with nib but there I am running into other issues (label is nil).
I've found that this problem probably occures because I instanciate a new table view controller programmatically.
var newController = MyTableViewController()
Does it mean I will have to register nibs anyway? Is it a problem that I will have to register the same class twice? And how do I avoid the problem of the nil label?
You should create an instance of your view controller with storyboard ID.
Replace the line var newController = MyTableViewController() with the following code
let storyboard = UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: nil)
let newController = storyboard.instantiateViewController(withIdentifier: "VIEWCONTROLLER_ID_FROM_STORYBOARD")
Edit:
It's also possible to use:
self.storyboard!.instantiateViewControllerWithIdentifier("VIEWCONTROLLER_ID_FROM_STORYBOARD")
if the view controller is instantiated from the storyboard, and second view controller is in the same storyboard
If you create your table view controller with an initializer (instead of storybaord.instantiateViewController(withIdentifier:)), then your table view controller will not know about the cells you put in the storyboard. If you want to use an initializer, then you will need to manually register the custom cell class in your controller's viewDidLoad.
class MyTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad
tableView.register(NavigationNodeCell.self, forCellReuseIdentifier: "NavigationNodeCell")
}
}
I created a workspace with 2 projects in it, let's call them A and B, with bundle identifiers com.domain.a and com.domain.b respectively. Each of the projects has a story board called Main.storyboard in them. I have a button in the storyboard of project A and I want to instantiate a view controller from the storyboard in project B with the following code:
#IBAction func clicked(sender: AnyObject) {
let bundle = NSBundle(identifier: "com.domain.b")
let storyBoard = UIStoryboard(name: "Main", bundle: bundle)
let vc = storyBoard.instantiateViewControllerWithIdentifier("aVC")
self.presentViewController(vc, animated: true, completion: {})
}
I also have set the storyboard ID for the view controller in project B:
But when I build and run the project I get this exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x7f8578cd34d0>) doesn't contain a view controller with identifier 'aVC''
NOTE: I used breakpoints and found out that the variable bundle in my code in nil. why?!
I'm approaching to swift in this days and I've a question.
What if I have to create programmatically a new UIViewController?
Easy with an empty/new view controller:
var controller: UIViewController = UIViewController()
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)
Now, I have a xib file that I would like to load on the controller:
var controller: UIViewController = UIViewController(nibName: "FeedDetail", bundle: nil)
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)
This crash because:
'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FeedDetail" nib but the view outlet was not set.'
I've already read this post and I can't understand what's wrong!
Sry I taken the answer from here, you missed something like (You have no view in xib so add one and then do this):
if you are using Xib follow this
follo following steps
1) open your xib file then right click on files owner and drag to your first view
2) then bind that view with outlet of "view"
hope you will get it...
Specifically , i want to check the IBOutlets , but by using
var vc = storyboard.instantiateViewControllerWithIdentifier("VCID") as UIViewController
the IBOutlets are nil .
The view controller's view property has to be accessed before the view is loaded and the outlets connected.
let view = vc.view
Will do it, or you can present the view controller on screen (this will also cause viewWill/DidAppear to be called) by setting it as the window's root view controller.
we have to add bundle as NSBundle(forClass: self.dynamicType) instead of nil .
func testClientsStoryBoard(){
let storyboard = UIStoryboard(name: "abc", bundle: NSBundle(forClass: self.dynamicType))
var vc = storyboard.instantiateViewControllerWithIdentifier("abcVC") as abcViewController
vc.loadView()
XCTAssertNotNil(vc.outletName,"Not Nil")
}