Instantiate view controller from storyboard in second project in workspace - ios

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?!

Related

Swift Segue Exception

I have a problem with the Segue. All segues have an identifier.
I change from one view to the other by writing this:
self.performSegue (withIdentifier: "ready", sender: self)
This would work only if I open the app 5-10 times (always different) does the app crash with the following error message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<MyApp.4ViewController: 0x1180c7000>) has no segue with identifier' select ''
I do not understand why it does not work. The Segue with the identifier "select" is only between VC 1 and 3 but the Segue between VC 3 and Tabbar Controller (VC 4) has the identifier "ready".
func readyToGo() {
UserDefaults.standard.setValue(check, forKeyPath: "go")
UserDefaults.standard.synchronize()
self.performSegue(withIdentifier: "ready", sender: self)
}
Thank you very much
From your log you perform a segue at VC named 4ViewController with identifier select check your code , despite you say it's between 1 & 3 , check if make the segue back from vc4 to vc1 , may in viewDidAppear or when VC 4 overrided methods

Thread 1: Signal SIGABRT when trying to switch view controllers

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController")
self.present(homeViewController, animated: true, completion: nil)
My code complies fine, however when I hit the third line in that sequence my whole application crashes and I get a Thread 1: Signal SIGABRT on that line and this is what appears in the console:
libc++abi.dylib: terminating with uncaught exception of type NSException
This only happens when I try to switch view controllers programmatically. I can switch view controllers normally by connecting buttons to view controllers in the storyboard, but I cannot successfully transition view controllers without having the user hit a button. Does anyone know how to fix this error, or an easier way to transition between view controllers.
Edit: I have also tried this:
let nextViewController = mainViewController(nibName: "MainViewController", bundle: nil)
self.present(nextViewController, animated: true)
Which also did not work but still compiled just fine.
Edit 2:
Before the main error message I get this warning in the console:
objc[45953]: Class VCWeakObjectHolder is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AVConference.framework/Frameworks/ViceroyTrace.framework/ViceroyTrace (0x127e2d4d0) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AVConference.framework/AVConference (0x126f7be38). One of the two will be used. Which one is undefined.
Make sure that you have set the "Storyboard ID" in your for the view controller in your storyboard.

mapKit: performing a segue after user selects an annotation marker

I'm having trouble performing a segue from a mapKit annotation to an other view controller on another storyboard.
I have a storyboard with a full screen map view and i want to perform a segue to the other storyboard containing the PanelViewController.
After adding a MKAnnotation to the map using map.addAnnotation(), i use mapView(_:didSelect:) to get the currently selected marker. I know i have to perform the segue here but i don't know how.
I guess i have 2 options, but none of them work:
present(PanelViewController, animated: true, completion: nil)
This gives me an error: "Cannot convert value of type 'PanelViewController.Type' to expected argument type 'UIViewController'"
performSegue(withIdentifier: "showPanel", sender: view)
This crashes the app with an uncaught exception error: "Receiver (<myApp.MapViewController: 0x109200f80>) has no segue with identifier 'showPanel"
Ofcourse this is true since i don't have a segue with this identifier on this view controller. I don't know how i would create one since it's just a full screen mapKit view and xcode doesn't allow me to ctr+drag from the mapKit view to the other view controler.
Any ideas?
This
present(PanelViewController, animated: true, completion: nil)
should be instance , create storyborad id named nextID for the VC PanelViewController in IB
let stoBr = UIStoryboard(name: "nameOfSB", bundle: nil)
let next = stoBr.instantiateViewController(withIdentifier: "nextID") as! PanelViewController
// here configuew next properties
present(next, animated: true, completion: nil)
and this
performSegue(withIdentifier: "showPanel", sender: view)
The current VC you do segue from must have segue named showPanel to destination VC

Swift 3 - loading multiple ViewControllers at launch

I am working on making a tabbed app. It has a TabBarController and 4 ViewControllers attached to it.
By default, at launch only FirstViewController is loaded. I would like to load both FirstViewController and SecondViewController at start before switching to the second view via tab menu.
What i tried so far is that I created custom MyTabBarController class and tried to use
var sv = SecondViewController()
sv.loadView()
in ViewDidLoad(), but it caused a fatal error during loading, because (my guess) mapView element from storyboard was not loaded.
What is the correct way to simultaneously load the two viewControllers which use storyboard elements? All my other tries have not been successful so far.
Add in your main view controller
var secondViewController:UIViewController!
And in your viewDidLoad:
secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController
That's it. When you want to present it, use:
self.present(secondViewController, animated: true, completion: nil)

can not instantiate a view controller, swift, ios

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?

Resources