Thread 1: Signal SIGABRT when trying to switch view controllers - ios

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.

Related

Swift pushViewController() not working but present() is

I have an app that has two storyboards: One for onboarding which displays a PageViewController with info about the app and the main view which shows displays a webview of the actual content.
I am trying to build in a sing out function to remove the data and bring them back to the Onboarding screen from where they will authenticate again. I have tried the following code after signing out but am not getting results.
let storyBoard: UIStoryboard = UIStoryboard(name: "Onboarding", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "PageControlViewController")
self.navigationController?.pushViewController(newViewController, animated: true);
This code does nothing and the app continues as normal. Replacing the pushViewController() with present() does work but by showing a modal over the existing view.
self.present(newViewController, animated: true, completion: nil)
My intention is to completely destroy the Main view and let the Onboarding view initiate a new version of the Main view.
As your code rightly shows, .pushViewController is meaningful only if self.navigationController is not nil. But in your case, self.navigationController is nil — your view controller is not in a navigation interface. Hence pushing is impossible and nothing happens.

While dismissing from one storyboard to other story board, its dismissing very lately in swift

I am using two storyboard files due to two people working in same project, to avoid conflicts while merging storyboard files, we are using two storyboard files.
But, the issue is while navigating from 1 storyboard file to other its working fine, but while dismissing 1 storyboard to other, its taking time and very lately its dismissing.
For loading time from 1 to another storyboard
let mainStoryboard: UIStoryboard = UIStoryboard(name: "ListViewController", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "ListViewController") as! ListViewController
self.present(viewController, animated:true, completion: nil)
I am using following code for dismissing storyboard
self.dismiss(animated: false, completion: nil)
Any suggestions to avoid this issue.
I fixed this issue, its happening due to previous viewcontroller doing calling webservice, so its takin few seconds time to call data, due to that, its taking time to dismissing.

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)

View Controller TDD

I am trying to add some unit tests to my project to test view controllers. However I seem to be having problems with seemingly simple things. I have created a sample project which I will refer to. https://github.com/pangers/ViewControllerTesting
The sample contains a UINavigationController as the initial view controller. The root view controller of the UINavigationController is FirstViewController. There is a button on FirstViewController that segues to SecondViewController. In SecondViewController there is an empty textfield.
The two tests I am trying to add are:
1) Check button title in FirstViewController is "Next Screen".
2) Check textfield in SecondViewController is empty, "".
I have heard reports of adding your swift files to both the main target and the test target is not good practice. But rather it is better to make whatever you want to access in your tests public and import the main target into the tests. So that is what I have done. (I have also set the "Defines Module" for the main target to YES as that is what I have read in a few articles aswell).
In FirstViewControllerTests I have instantiated the first view controller with the following:
var viewController: FirstViewController!
override func setUp() {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
let navigationController = storyboard.instantiateInitialViewController() as UINavigationController
viewController = navigationController.topViewController as FirstViewController
viewController.viewDidLoad()
}
And I have added the test:
func testCheckButtonHasTextNextScreen() {
XCTAssertEqual(viewController.button.currentTitle!, "Next Screen", "Button should say Next Screen")
}
Similarly, for SecondViewControllerTest, I have set it up using:
var secondViewController:SecondViewController!
override func setUp() {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
let navigationController = storyboard.instantiateInitialViewController() as UINavigationController
let firstviewController = navigationController.topViewController as FirstViewController
firstviewController.performSegueWithIdentifier("FirstToSecond", sender: nil)
secondViewController = navigationController.topViewController as SecondViewController
secondViewController.viewDidLoad()
}
And the test:
func testTextFieldIsBlank() {
XCTAssertEqual(secondViewController.textField.text, "", "Nothing in textfield")
}
They both fail and I am not too sure as to why. My suspicion is that the way I am instantiating the view controllers is not correct. Is the best way to instantiate the view controllers is to use the storyboard (just like it would if it were to run in real life)? Or is it acceptable to be instantiated via:
var viewController = FirstViewController()
What are you guys' experience with TDD and view controllers in swift?
I am using Swift with XCode 6.1.1.
Thanks in advance.
Solved
Ok after considering the answers from modocache and Mike Taverne, I've found my solution and I've learnt a few things which I will write down below.
1) I made anything class/method/variable that I want to test public. I do not need to add the swift files to the test target.
2) I only needed to set "Defines Module" for the "Main" target (as opposed to the "Test" target or the entire project)
3) When instantiating the storyboard, the bundle should be set to nil rather than NSBundle(forClass: self.dynamicType), otherwise tests will fail.
4) As modocache stated, it is good to give your view controller's a StoryboardID and instantiate them like so:
viewController = storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController
However, instantiating the view controller like this ONLY instantiates the view controller alone, and not any navigation controllers that it may be embedded in. That means, attempting to do
XCTAssertFalse(viewController.navigationController!.navigationBarHidden, "Bar should show by default")
will result in a nil exception. I confirmed this with
XCTAssertNil(viewController.navigationController?, "navigation controller doesn't exist")
which resulted in a successful test.
Since I wanted to check the state of the navigation bar in FirstViewController, you must instantiate the view controller like so:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyboard.instantiateInitialViewController() as UINavigationController
viewController = navigationController.topViewController as FirstViewController
Now performing the test
XCTAssertFalse(viewController.navigationController!.navigationBarHidden, "nav bar should be showing by default")
results in a successful test.
5) let _ = viewController.view does indeed trigger viewDidLoad() which was confirmed by a test
6) let _ = viewController.view does not trigger viewWillAppear(), and I presume anything afterwards aswell. viewController.viewWillAppear(false/true) needs to be called manually to trigger it (Confirmed by a test).
Hopefully this will be of help to people. I will push the updated project to GitHub (link above) if anyone would like to play around with it.
Update #2
After all the above, I still could not figure out how to transition from the first view controller to the second view controller (so that I may test navigation bar properties in SecondViewControllerTests.swift). I tried
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nc = storyboard.instantiateInitialViewController() as UINavigationController
let firstVC = nc.topViewController as FirstViewController
firstVC.performSegueWithIdentifier("FirstToSecond", sender: nil)
secondVC = nc.topViewController as SecondViewController
which caused an error.
I also tried
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nc = storyboard.instantiateInitialViewController() as UINavigationController
let firstVC = nc.topViewController as FirstViewController
firstVC.toSecondVCButton.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
secondVC = nc.topViewController as SecondViewController
which did not work.
I eventually tried
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nc = storyboard.instantiateInitialViewController() as UINavigationController
vc = storyboard.instantiateViewControllerWithIdentifier("Second") as SecondViewController
nc.pushViewController(vc, animated: false)
let _ = vc.view
vc.viewWillAppear(false)
which worked perfectly with my tests (allowed me to access navigation bar properties)!
I agree with #MikeTaverne's answer: I prefer accessing -[UIViewController view] in order to trigger -[UIViewController viewDidLoad], rather than calling it directly. See if the test failures for FirstViewController go away once you use this instead:
viewController = navigationController.topViewController as FirstViewController
let _ = viewController.view
I'd also recommend giving both view controllers identifiers in your storyboard. This will allow you to instantiate them directly, without accessing them via UINavigationController:
var secondViewController: SecondViewController!
override func setUp() {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
secondViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController")
as SecondViewController
let _ = secondViewController.view
}
Check out my talk on testing UIViewController at Brooklyn Swift for details: https://vimeo.com/115671189#t=37m50s (my presentation begins around the 37'50" mark).
I've begun unit testing view controllers recently, and it poses some unique challenges.
One challenge is getting the view to load. Looking at your set up for FirstViewController, you are trying to do this with viewController.viewDidLoad().
My suggestion is to replace that line with this:
let dummy = viewController.view
Accessing the .view property will force the view to load. This will trigger the .viewDidLoad in your ViewController, so don't call that method explicitly in your test.
This approach is considered hacky by some people, but it is simple and effective. (See Clean way to force view to load subviews early)
As an aside, I am finding the best way to test view controllers is to move as much code out of the view controllers as possible into other classes that are more easily tested.
If your view controller is defined in a storyboard, then you need to instantiate it that way for your outlets to be set up properly. Trying to initialize it like an ordinary class won't work.

dismissViewControllerAnimated Crashes EXC

I've been trying to present and dismiss a view controller programmatically and I keep running into EXC issues.
What is the proper way to present and dismiss a view controller other than using:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Popup") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
and self.dismissViewControllerAnimated(true, completion: nil)
?
So far the app crashes when i run the function: dismissViewControllerAnimated
Please help, thanks!
Apparently, I had a NSNotification listener in the ViewController which caused the crash.
What I did was take it out and re-did the ViewController to populate the View Controller via a method rather than use a method linked to a NSNotification Listener.

Resources