unrecognized selector sent to instance 0x7ff0ece07a40" ERROR on xcode - swift

for actionButton in actionButtons {
if actionButton.currentTitle != (String(rightAnswerPlacement))
{
actionButton.isHidden = true // true to hide the button
}
}
}
#IBOutlet weak var hintButton: UIButton!
//Label for qs
#IBOutlet weak var label: UILabel!
//button for choices
#IBOutlet var actionButtons: [UIButton]!
#IBAction func Action(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
}
else
{
print("Wrong")
let alert = UIAlertController(title: "That is incorrect", message: "Try again!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
currentQuestion -= 1
//how to record which wrong answer the user is pressing
//let WrongAnswer = sender.tag.titleLabel?.text
}
Hi, this is a snippet of code for a simple quiz app and I am trying to code for a hint button which when pressed makes one of the choices for every qs in the quiz disappear. My code had no errors until I ran it and saw the error mentioned above in the App.Delegate.swift.The exact error message is 'Thread 1: Exception: "-[Aphasia_app.ViewController Hint:]: unrecognized selector sent to instance 0x7ff0ece07a40" ' . I am quite sure the problem is with my code and not with any connections in the storyboard. Thanks for the help, been stuck at this for two days now...

I am quite sure the problem is with my code and not with any connections in the storyboard.
No, the problem is indeed a connection. According to a suggestion in your previous question you renamed Hint to hintButton which does not rename the connection automatically.
In the storyboard delete the dead Hint connection and reconnect it to hintButton.

Related

I am not sure why I keep getting the error Value of type '(AnyObject) -> ()' has no member 'currentTitle'

#IBAction func Hint(_ sender: UIButton) {
if Action.currentTitle != Int(rightAnswerPlacement) // this is the line where I keep getting error I posted above
{
Action.isHidden = true // true to hide the button
}
}
#IBOutlet weak var Hint: UIButton!
//Label for qs
#IBOutlet weak var label: UILabel!
//button for choices
#IBOutlet weak var Action: UIButton!
#IBAction func Action(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
}
else
{
print("Wrong")
let alert = UIAlertController(title: "That is incorrect", message: "Try again!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
currentQuestion -= 1
//how to record which wrong answer the user is pressing
//let WrongAnswer = sender.tag.titleLabel?.text
}
This is a code for a basic quiz app on Xcode, what I am trying to do with my first few lines of code is trying to make an option disappear once I click another button
Reason for the error is that both your button and a function are named Action. In the line that gives you error, compiler thinks you are referring to the function (which is of type (AnyObject) -> ()) instead of the button. Rename either your function, or your button.
By the way, there are couple of things you should improve in your code:
by convention, names of types are uppercased, while names of instances should be lowercased
use more descriptive names for your variables and functions - i.e. hintButton instead of Hint, actionButton instead of Action for UIButton and actionButtonTapped for func

Attempt to present <UIAlertController> on whose view is not in the window hierarchy

I've been trying to present a UIAlertController when user entered wrong password for their account, the UIAlertController is located in one separate file inside a Model group which I extend the UIViewController class to add this alert functionality to it. I also has another file inside my model group namely LogIn which I wrote all the logic behind the login process so that I can call it to my LogInVC. However, I got an error of "Attempt to present on whose view is not in the window hierarchy!" whenever the function get call inside my LogInVC. I'm trying to make my project in MVC and I know what caused this error but I just don't know how to fix it. May anyone tell me how to fix this problem?
Alert
import Foundation
import UIKit
extension UIViewController {
//MARK: - Not Enough Information Alert
func notEnoughInfo(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
//MARK: - Incorrect Username and Password
func wrongInfo(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "Try again", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
LogIn
import Foundation
import Firebase
class LogIn: UIViewController{
let db = Firestore.firestore()
//MARK: - userValidation()
func userValidation(Username:String, Password:String){
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
if let snapShotDocument = querySnapshot?.documents {
for doc in snapShotDocument {
let data = doc.data()
if let username = data[C.username] as? String, let password = data[C.password] as? String {
if Username == username, Password == password {
print("Log in Successfully")
}
else {
self.wrongInfo(title: "Incorrect password", message: "Try again please")
}
}
}
}
}
}
}
}
LogInVC
import UIKit
import Firebase
class LogInVC: UIViewController {
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var logInBtn: UIButton!
let db = Firestore.firestore()
let logIn = LogIn()
override func viewDidLoad() {
super.viewDidLoad()
//logInBtn.layer.cornerRadius = logInBtn.frame.height/5
}
#IBAction func logInBtn(_ sender: UIButton) {
if let username = emailTextField.text, let password = passwordTextField.text{
if username.isEmpty || password.isEmpty{
notEnoughInfo(title: "Not enough information", message: "Please fill in all the necessary information.")
}else{
logIn.userValidation(Username: username, Password: password) //here is where problem occured
//move to another viewcontroller
}
}
}
#IBAction func signUpBtn(_ sender: UIButton) {
let push = storyboard?.instantiateViewController(withIdentifier: C.signUpVC) as! SignUpVC
push.modalPresentationStyle = .fullScreen
present(push, animated: true, completion: nil)
}
} //ends of class
You need to first dismiss the current present alert or present controller. currently you are trying to present controller over a controller that's why it shows this error. Don't present . remove this line from self.wrongInfo(title: "Incorrect password", message: "Try again please") from LogIn.
try this and you can comment again if there is anything regarding this.

I get Thread 1: signal SIGABRT and my IOS App crashes

I am working on an IOS App on Swift and its connected to Firebase. Once I reach a specific page in the run mode, my app crashes and I get this error message without any details. Please help
I do not have any errors with my Outlets and I have tried debugging with break points but it still takes my to the AppDelegate with this error without details. I am clueless. Before it used to work the code should be fine and data were being inputted to the DB but something happened since I have created the login page.
// LoginViewController.swift
// Together
import UIKit
import Firebase
class LoginViewController: UIViewController {
#IBOutlet weak var usernameTxt: UITextField!
#IBOutlet weak var passTxt: UITextField!
#IBAction func userLoginBtn(_ sender: Any) {
var pass = passTxt.text
Auth.auth().signIn(withEmail: usernameTxt.text!, password: passTxt.text!){
(user, error) in
if error == nil{
self.performSegue(withIdentifier: "loginToHome", sender: self)}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
I GOT THIS ISSUE SORTED FINALLY and The mistake was very simple. If anyone is facing the same issue and you are sure you don't have any issues with your outlets, go the to Identity inspector and check the Class of each ViewController. Make sure not more than 1 ViewController is connected to the same Class, especially if the class had code for a specific function. This was the reason why my App crashes and takes me to the AppDelegate class. The code of the last page I reach before the crash did not have errors, but the only cause was having it connected to a Class used by another ViewController.

How to fix this error ' this class is not key value coding-compliant for the key containerView.'?

When the user what to sign up or login it has error and get this error in the output "this class is not key value coding-compliant for the key containerView."
this is the login code also signup has the same error and l'm sure it's a right codes.
import UIKit
import Firebase
class LoginViewController: UIViewController {
#IBOutlet var Aemeil: UITextField!
#IBOutlet var Apassword: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func login (_ sender : Any){
guard let email = Aemeil.text, let password = Apassword.text
else {
print("Form is not valid")
return
}
Auth.auth().signIn(withEmail: email , password: password , completion: { (user,error) in
if error == nil{
let vc = self.storyboard?.instantiateViewController(withIdentifier: "AHome");
self.present(vc!, animated: true, completion: nil);
print("Home page open")
}
else{
let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Yes", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
}
You have created a binding from a storyboard element, to an element in the corresponding swift file, with the name containerView. After that you have deleted the containerView variable in your swift file, but the binding still exists in the storyboard.
To solve this problem go to your storyboard, click on the view controller, like below:
After that, click on the connection inspector, to see all bindings of your viewcontroller and delete the binding from the containerView:
I have faced the similar problem, I created a segmentview under which I've kept a containerView and removed the default segue that came along with that container view. After that I renamed the container view controller name and connected as show segue. This was the problem which caused me to face this issue. When I changed the show segue to Embed segue the problem resolved.

Swift SIGABRT Error? But I resolved it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have yet another error, its the common swift error
Thread 1: signal SIGABRT
I know how to fix this error. Here is my recent question.
Can't resolve error: Thread 1: signal SIGABRT in swift XCODE
But now I am making a new project, and I got this error! So I tried to fix it, I thought I got it - but I didn't.. I have checked all my outlets and everything, But there is no trace of where this is coming from...
Here is my code:
import UIKit
class ViewController: UIViewController {
//Variables
var tempature:Int = 50
var battery:Int = 100
var monsterCount:Int = 0
//Outlets
#IBOutlet weak var BatteryLevel: UILabel!
#IBOutlet weak var TempDisplay: UILabel!
//The spy camera
#IBOutlet weak var monsterImageDisplay: UIImageView?
//The background image
#IBOutlet weak var backgroundImageShow: UIImageView!
//Functions
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
monsterImageDisplay?.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//The interval to keep things going
var helloWorldTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: Selector(("spawnMonstor")), userInfo: nil, repeats: true)
func spawnMonstor() {
monsterImageDisplay?.isHidden = false
monsterCount += 1
if monsterCount >= 3 {
let randomNumberBackgroundImage = arc4random_uniform(10)
if randomNumberBackgroundImage >= 5 {
backgroundImageShow.image = UIImage(named: "main-room-jumpTop")
} else if randomNumberBackgroundImage <= 5 {
backgroundImageShow.image = UIImage(named: "main-room-jumpBottom")
}
} else if monsterCount >= 5 {
let alert = UIAlertController(title: "GAME OVER", message: "Sorry! You died...", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Okay..", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
//The shock rooms button
#IBAction func ShockRooms(_ sender: Any) { monsterImageDisplay?.isHidden = true
}
}
Check that your UIImage names are the EXACT same names as the ones in your xcassets folder.
Instead of using the helloWorldTimer, write this at the top of your class, underneath your outlets:
var helloWorldTimer = Timer()
And then inside of your viewDidLoad, add:
helloWorldTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: Selector(("spawnMonstor")), userInfo: nil, repeats: true)