Cannot find (secondary) storyboard named Test.storyboard - ios

I would like to load a secondary storyboard Test.storyboard (next to Main.storyboard) in my app like so:
let storyboard = UIStoryboard(name: "Test.storyboard", bundle: nil)
Test.storyboard resides inside a group Test, it is marked for localization as Base (hence its Location Relative to Group is reported as Base.lproj/Test.storyboard) and it also marked for target membership in my app target. When I run the app on the simulator, I can confirm that a file Base.lproj/Test.storyboardc exists inside the app's main bundle.
Yet the above code produces this error:
Could not find a storyboard named 'Test.storyboard' in bundle ...
What is the right way to successfully package and load a secondary storyboard like mine?

You don't need to load storyboard with .storyboard extension .use like this
let storyboard = UIStoryboard(name: "Test", bundle: nil)

Related

Instantiate Storyboard from local Swift Package

I try to instatiat a ViewController vom a local SPM Package but I always get the error "Could not find a storyboard".
let myViewController = UIStoryboard(
name: "MyStoryboard",
bundle: Bundle(
url: Bundle.main.url(
forResource: "myModule_myModule",
withExtension: "bundle"
)!
)
).instantiateViewController(withIdentifier: "MyViewController")
This code works when using it as an remote package, but not when dragging it to Xcode and use it local.
Thanks Bernhard
Unlike Remote Packages, When adding a Local one Xcode doesn't prompt you to add it to a target. Hence you need to add it manually in your Target's General settings, under Frameworks, Libraries, and Embedded Content

UnitTest in swift | IOS Storyboard | Failed to cast viewcontroller class defined in objective c

So I am trying to write down some test cases for one of my controller class,
My viewcontroller is defined in objective c.
I am writing test cases in swift.
I have included my viewcontroller class in both my main target and my test target.
I have also added the storybaord in both test and main target.
In my test I am trying to load the view controller from story board, below is the code (this code is working fine in my normal project from swift class)
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.init(for: self.classForCoder))
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController")
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
controller = viewController as! LoginViewController
print output
LoginView Controller
__ABT_LoginViewController
Last line of my code is getting failedi.e
controller = viewController as! LoginViewController
error I am getting below
Could not cast value of type '__ABT_LoginViewController' (0x600000053b30) to 'LoginViewController' (0x11e88de30).
Need your help guys
Updated
So above issue resolved after following the answer by #dashandrest, Now I am facing new issue, I have created a TestAppdelegate file for test cases, in my TestAppDelegate class I have this statement import "App-swift.h now after compiling the whole code it is giving an error inside App-swift.h file mentioning cannot import module AppName, My main target module name is AppName whereas test target module name is AppNameTest,
#import "App-swift.h"
Now I am getting compiling error inside App-swift.h file.
#if defined(__has_feature) && __has_feature(modules)
#import ObjectiveC;
#import AppName; ///this line generate compile errror, module AppName not found
#import XCTest;
#endif
Updated
So there were multiple issues in my app which I resolved, definitely #DashAndRest input help me in identifying those, below are the things I correct in order to make things work.
Remove all code files from test target except the test cases.
Make sure that I have separate Module name for my main target and test target.
Make sure that My Pods file does have installing the libraries for my test target also.
Write test cases in Swift for the safe side so that I don't need to include Appname-swift.h in test cases.
Use #testable import AppModuleName (as suggested by #DashAndRest)
This is not required:
3 I have included my viewcontroller class in both my main target and my test target.
4 I have also added the storybaord in both test and main target.
Try unchecking Test target for both view controller and storyboard.
And then in your test file just include main target as:
#testable import Your-Main-Target-Name
Update
Still App-swift.h is confusing.
But if you want to test LoginViewController, here is the solution:
Add Bridging Header to your main AppName target and import all the objective-c classes to it, like:
#ifndef AppName_Bridging_Header_h
#define AppName_Bridging_Header_h
#import "LoginViewController.h"
#endif
After this, your LoginViewController class will be available for all other swift classes to use, including test target.
Add new file like LoginVCTests.swift in test target and add following code to it-
import XCTest
#testable import AppName
class LoginVCTests: XCTestCase {
func testSomeLogic() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as! LoginViewController
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
//test on viewController
}
}
Hope this will solve your problem!

Add Playground to a Storyboard project: Could not find a storyboard ... in bundle NSBundle

I have a working iOS project with Storyboards. Then I added a playground file to it with following content:
The
import UIKit
import PlaygroundSupport
import SpriteKit
var str = "Hello, playground"
let gameViewController = GameViewController.loadFromStoryboard() // <<<<- Fails
PlaygroundPage.current.liveView = gameViewController
I am getting following error at line, where I loadFromStoryboard():
NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle
What should I fix?
There are tonnes of questions on stackoverflow with the above title/contents however, each has a very specific scenario. I have gone through many of them over past few days and I think that my scenario is distinct due to use of Playground hence I am posting again.
If this is in an existing project, you can instantiate the storyboard by using your full bundle identifier, as demonstrated here:
let myBundle = Bundle(identifier: "com.asad.MyProgram")
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: myBundle)
Please note that this does require that the playground is in the same workspace as the project with that particular bundle identifier.

Could not load NIB in bundle :NSBundle

I am trying to load a nib file from MainViewController but i get an exception which i mentioned below. I am not getting this exception regularly (1 out of fifty times). Check the exception below
Exception :
Could not load NIB in bundle: 'NSBundle </private/var/mobile/Containers/Bundle/Application/38ABE26C-F87E-49F1-9F29-E46E7ACDBD54/E2_IOS_App.app> (loaded)' with name 'NetWorkError'
the code i use to present this nib file is
NetWorkError *error=[[NetWorkError alloc]initWithNibName:#"NetWorkError" bundle:nil];
error.errorMessage=[details serverDownMessage]; // passing some message before presenting .
[self presentViewController:error animated:YES completion:nil];
i am using an iPad running with iOS 7.
People with similar issues where asked to check the nib name , check if the .xib is added to target, check if the .xib is added to copy bundle resource.But every thing is correct for me. what else might be the issue ??.
Have you tried NSStringFromClass([NetWorkError class]) ?
Also you can try to exclude and add file to your project again / clean project / delete app from simulator.

Unknown class in interface builder file yet class exists

This question has been asked before but I can't see any cause relevant to my issue.
I've created a very simple view controller and trying to load it but getting an unknown class MapViewController in interface builder file error. Here are some relevant parts:
class MapViewController: UIViewController {
...
}
...
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mapViewController = storyboard.instantiateViewControllerWithIdentifier("MapViewControllerID") as MapViewController
The MapViewController.swift file is included in the Compile Sources.
The storyboard has the default name of Main.storyboard
Have exited Xcode, rebuilt, deleted the app on the device.
The problem still persists, any suggestions?
(Using Xcode beta 4)
My problem was I had accidentally selected a module under the class entry for custom class in IB. Once I deselected that everything worked.
How I solved this was to
1) Delete the derived data
2) Rename the class to MyMapViewController
3) Delete derived data again
4) Rename the class back to MapViewController

Resources