I have a really simple question here that I cannot manage to solve. This code works perfectly fine, leading me to the fifth view controller.
#objc func OnbtnPlusTouched()
{
let uivc = storyboard!.instantiateViewController(withIdentifier: "FifthViewController")
navigationController!.pushViewController(uivc, animated: true)
}
However, I want to pass the data locationName from this viewcontroller to the next one. Thus I used this code segment:
#objc func OnbtnPlusTouched()
{
let vc = FifthViewController(nibName: "FifthViewController", bundle: nil)
vc.locationName = locationName
navigationController?.pushViewController(vc, animated: true)
}
But now I am getting this error as soon as I enter this function in my app:
libc++abi.dylib: terminating with uncaught exception of type
NSException and Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Could not load NIB in
bundle: 'NSBundle
I would appreciate any help, thank you!
Try this instead
#objc func OnbtnPlusTouched() {
guard let uivc = storyboard!.instantiateViewController(withIdentifier: "FifthViewController") as? FifthViewController else {
return
}
uivc.locationName = locationName
navigationController!.pushViewController(uivc, animated: true)
}
Related
I am trying to navigate from one Storyboard to another Storyboard. Here is my code -
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "detail") as! detailsArticle
controller.jsonData = json2["post"]
self.navigationController!.pushViewController(controller, animated: true)
However, when I try to navigate after clicking the button which runs the above code, I get exception error:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file
How do I solve this?
There are 2 places you could be getting the crash.
The identifier of the detailsArticle is not set as "detail".
The controller you're trying to push onto is not embedded in navigationController.
Try running and check the console to see the debug messages I've added:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: "detail") as? detailsArticle else { print("detail vc id not set"); return }
controller.jsonData = json2["post"]
guard let navigationController = navigationController else { print("this vc is not embedded in navigationController"); return }
navigationController.pushViewController(controller, animated: true)
From the comments it's the 2nd one causing the issue. To fix it here are the steps:
Go to <StoryboardName>.storyboard where the current UIViewController sub-class is located and select the current UIViewController scene.
From the Menu bar choose Editor and Embedd In and select Navigation Controller.
Hi I'm kind of new to Swift and I can't figure this out. I am trying to create a sign out button that would take user to the login page. I used the following two methods but the first one doesn't do anything and the second one is throwing Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value and it refers to the code with the customButton:
//this one doesn't do anything
#objc func SignOut(){
let vc = self.storyboard?.instantiateViewController(identifier: "LoginViewController") as! LoginViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = vc
}
//this one is throwing an error
let vc = CustomViewController()
self.present(vc, animated: true, completion: nil)
//the Fatal error refers to this code
override func viewDidLoad() {
super.viewDidLoad()
self.customButton.addTarget(self, action: #selector(customButtonPressed), for: .touchUpInside)
}
Also, I was wondering if AppDelegate is the right approach or if I should use SceneDelegate. Any help would be greatly appreciated.
Try to do this
#objc func SignOut(){
let vc = self.storyboard?.instantiateViewController(identifier: "LoginViewController") as! LoginViewController
self.view.window?.rootViewController = vc
}
let vc = self.storyboard?.instantiateViewController(identifier: "LoginViewController") as! LoginViewController
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
Please make sure the identifier is matching the one inside storyboard for this viewController
I have a view HostViewController in Host.storyboard and, in storyboard I am able to segue to AttendDetailViewController in Main.storyboard. However, I want to do this programmatically as follows:
private func attendDetailViewControllerSegue(event: CAEvent) {
let vc = AttendDetailViewController(nibName: "AttendDetailViewController", bundle: nil)
vc.event = event
navigationController?.pushViewController(vc, animated: true)
}
However, when I run this I get the following error:
'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle ... with name 'AttendDetailViewController''
I have tried every solution on the internet for this problem, and I feel it might have something to do with the Views being in different storyboards.
Any thoughts on how to do this segue without an exception?
You have to get the storyboard from the bundle:
var hostStoryboard = UIStoryboard(name: "Host", bundle: Bundle.main)
Then, instantiate and present the view controller:
let someViewController = hostStoryboard?.instantiateViewController(withIdentifier: "AttendDetailViewController") as? AttendDetailViewController
self.navigationController?.present(attendDetailViewController!, animated: true)
Don't forget to set the AttendDetailViewController's storyboard identifier in the storyboard.
I also recommend you safely unwrap attendDetailViewController before using it:
let someViewController = hostStoryboard?.instantiateViewController(withIdentifier: "AttendDetailViewController") as? AttendDetailViewController
if let vc = attendDetailViewController {
self.navigationController?.present(vc, animated: true)
}
1- to load from xib ( used when there is AttendDetailViewController.xib file in your project )
let vc = AttendDetailViewController(nibName: "AttendDetailViewController", bundle: nil)
2- to load from storyboard ( used when the vc is inside a storyboard )
let vc = UIStoryboard(name: "Host", bundle: nil)!.instantiateViewController(withIdentifier: "vciD") as? AttendDetailViewController
3- load programmatically ( used when the vc layout is created programmatically )
let vc = AttendDetailViewController()
I am trying to call a delegate function in Swift 3.0. While calling i am getting an error which states "fatal error: unexpectedly found nil while unwrapping an Optional value". I have gone through giving static values, dynamic values no use.
protocol getServiceDelegate{
func getService(_ ServiceTitle: String)
}
And i call this delegate in following manner.
self.delegate.getService("hello")
What i am doing here wrong. Below which i set the delegate.
func textServiceTapped(img: AnyObject)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ProviderMoreDetailsVC") as! ProviderMoreDetailsViewController
vc.delegate = self
navigationController?.pushViewController(vc,
animated: true)
}
func getService(_ ServiceTitle: String){
textService.text = ServiceTitle
}
I resolved the error by using below code
var delegate: getServiceDelegate?
sampleDelegateString = "hello"
self.delegate?.getService(sampleDelegateString!)
I'm trying to set up a share button from a time I previously coded it. I must be missing something, but I'm not sure what. Can you help? This is on a different view controller than the main one.
import UIKit
class MoreViewController: UIViewController, UITabBarDelegate, UITextFieldDelegate {
#IBAction func shareKnockingBuddy(_ sender: AnyObject) {
let messageToSend = "Here is a message. \(doorsKnocked)"
let vc = UIActivityViewController(activityItems: [messageToSend], applicationActivities: nil)
self.present(vc, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Here's my error: On the App delegate it's "Thread 1: signal SIGABRT". On the message thingy on the bottom, it says "* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController shareKnockingBuddy:]: unrecognized selector sent to instance 0x7fe282c02eb0'
* First throw call stack:"
Notice the error is about UIViewController and not your MoreViewController class. This means you did not set th proper class name for the view controller in your storyboard. Change it to MoreViewController.