How I can use performSegue in swift 3.0? - swift

#IBOutlet weak var viewHistory: UIView!
#IBOutlet weak var viewGoal: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tapHistory = UITapGestureRecognizer(target: self, action: #selector(goHistory))
tapHistory.numberOfTapsRequired = 1
tapHistory.numberOfTouchesRequired = 1
tapHistory.isEnabled = true
viewHistory.addGestureRecognizer(tapHistory)
// Do any additional setup after loading the view.
}
#objc func goHistory(){
self.performSegue(withIdentifier: "Segue_History", sender: self)
}
In StoryBoard A, I set the Triggered Segues to MyPage->History.
So I set the segue identifier "Segue_History"
When I tap the view in MyPage, goHistory function call performSegue.
But it occurs error like this.
2018-11-02 16:43:45.875605+0900
testApp[1729:436205]
-[testApp.VC_History tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x106a83d40
2018-11-02 16:43:45.891627+0900
testApp[1729:436205] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[testApp.VC_History tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x106a83d40'
I think I set the identifier at segue, but it is not.
Do you have any idea of it?

Related

Unrecognized selector sent to instance for Bar Button

I'm making a Twitter app on Xcode and have encountered a frustrating error.
I'm tryna make the Tweet bar button present modally.
When I run the code it crashes if I click "Tweet"
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Twitter.TweetViewController _finishDecodingLayoutGuideConnections:]: unrecognized selector sent to instance 0x7fb1a25512c0'
*** First throw call stack:
libc++abi.dylib: terminating with uncaught exception of type NSException
import UIKit
class TweetViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBOutlet weak var tweetTextView: UITextView!
#IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
#IBAction func Tweet(_ sender: Any) {
if (!tweetTextView.text.isEmpty) {
TwitterAPICaller.client?.postTweet(tweetString: tweetTextView.text, success:{ self.dismiss(animated: true, completion: nil)
}, failure: { (error) in
print("Error posting tweet \(error)")
self.dismiss(animated: true, completion: nil)
})
} else {
self.dismiss(animated: true, completion: nil)
}
}
I looked up people with similar issues but could not find an solution that helped.
Verify the IBOutlets and IBActions are properly connected. These crash happens when the XIB and Class files are not properly connected.
Try to remove the connections and add it again. Also rename the property names if the error persist.

Swift: addGestureRecognizer for UIView class

I use this code:
class StarClass: UIView {
#IBOutlet weak var bgStar: UIView!
class func createMyClassView() -> StarClass {
let myClassNib = UINib(nibName: "Star", bundle: nil)
let nW = myClassNib.instantiate(withOwner: nil, options: nil)[0] as! StarClass
nW.bgStar.layer.cornerRadius = 15
nW.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleStar(sender:))))
return nW
}
#objc func handleStar(sender: UITapGestureRecognizer) {
print("iosLog STAR")
}
}
After run, if I clicked on the view, I get error below:
2018-07-10 11:11:50.496349+0430 Ma[23098:89853] +[Ma.StarClass
handleStarWithSender:]: unrecognized selector sent to class
0x10ef5cac0 2018-07-10 11:11:50.513392+0430 Ma[23098:89853] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '+[Ma.StarClass
handleStarWithSender:]: unrecognized selector sent to class
0x10ef5cac0'
* First throw call stack:...
There seems to be a problem with the line :
nW.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleStar(sender:))))
Here, inside a class function, self refers to the Class type, not its object. But you are adding the gesture recogniser on an object. The solution here would be to do this:
nW.addGestureRecognizer(UITapGestureRecognizer(target: nW, action: #selector(handleStar(sender:))))

Error in Swift when using UIImageView with selector #objc C function when reading sender.tag

An image sends an action to an #objc C function when pressed:
let imageView = UIImageView()
imageView.tag = 3
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ItemAction)))
imageView.image = UIImage(named: (itemDictionary[character.Equipment[imageView.tag]]!.image))
overlayView.addSubview(imageView)
this is the function that should be called:
#objc func ItemAction(sender: UIImageView!) {
print(sender.tag)
print("Item pressed from sender ")
}
the function runs, but when it comes to printing the sender tag I get an error message and the program quits:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]:
unrecognized selector sent to instance 0x600002b42a00'
The whole setup with a UIButton works.
How can I read the tag of an UIImageView in an external function?
here you go
#objc func itemAction(_ sender: UITapGestureRecognizer){
if let tag = sender.view?.tag{
print("ImageView tag \(tag)")
}
}
in your function change your method signature to
#objc func ItemAction(_ sender: UITapGestureRecognizer)

Unrecognized selector error on swipe left gesture swift

I am attempting to create a simple swift left segue to the next view controller, however I am getting an unrecognized selector error. This the code failing:
override func viewDidLoad() {
super.viewDidLoad()
//below creates the instances for swiping to change screens
//only added right here because it's the main screen
var swipeLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipe:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
func swipe(Sender: UISwipeGestureRecognizer!) {
print("swiped left")
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("OnDeck") as! OnDeck
self.presentViewController(vc, animated: true, completion: nil)
}
}
This is the error I am getting:
2017-06-07 19:07:00.990 Test[65722:3911717] -[Test.GameView swipe:]: unrecognized selector sent to instance 0x7fc2d310f700
2017-06-07 19:07:01.000 Test[65722:3911717] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test.GameView swipe:]: unrecognized selector sent to instance 0x7fc2d310f700'
In your case for Swift 3 the selector should look like this:
#selector(swipe(Sender:))
You can use the #selector notation to get compile-time checks.
For Swift 2 the selector would be:
#selector(swipe(_:))
You need to try it like that :
let swiftLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.swipe(Sender:)))
swiftLeft.direction = .left
self.view.addGestureRecognizer(swiftLeft)
func swipe(Sender: UISwipeGestureRecognizer!) {
print("swiped left")
}
If you are working on Swift 2, you need to add the selector like that
selector(self.swipe(_:))

Swift - Trying to set up a share button

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.