I have tableview has multiple headers one of this header has UIView, and UIView Contains
1- UIImageView
2- UIlabel
After I have added UITapGesture in the UIImageView, and when I press this image will navigate to another screen
--My problem is
when I add UITapGesture to imageView , and try to press it application crash with this message
***** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView showHint:]: unrecognized selector sent to instance 0x1693efb30'**
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let tap = UITapGestureRecognizer(target: view, action: #selector(showHint))
hintImageView.isUserInteractionEnabled = true
hintImageView.addGestureRecognizer(tap)
}
#objc func showHint(_ sender: UITapGestureRecognizer) {
print("hi")
}
Related
I'm very new to coding so I really have no idea what's going on.
I've tried pasting the error into this website and looked around for people's responses but they either slightly altered from my error code or their explanation was too convoluted for me.
Code:
import UIKit
class BasicsListScreen: UIViewController {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension BasicsListScreen: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return basics.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell
cell?.verbLabel.text = basics[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "ConjViewController") as? ConjViewController
vc?.pastMe = mePast[indexPath.row]
vc?.pastYou = youPast[indexPath.row]
vc?.pastHe = hePast[indexPath.row]
vc?.pastShe = shePast[indexPath.row]
vc?.pastWe = wePast[indexPath.row]
vc?.pastYall = yallPast[indexPath.row]
vc?.pastThey = theyPast[indexPath.row]
vc?.pastPassive = passivePast[indexPath.row]
vc?.presentMe = mePresent[indexPath.row]
vc?.presentYou = youPresent[indexPath.row]
vc?.presentHe = hePresent[indexPath.row]
vc?.presentShe = shePresent[indexPath.row]
vc?.presentWe = wePresent[indexPath.row]
vc?.presentYall = yallPresent[indexPath.row]
vc?.presentThey = theyPresent[indexPath.row]
vc?.presentPassive = passivePresent[indexPath.row]
self.navigationController?.pushViewController(vc!, animated: true)
}
}
28784:1039713] Unknown class BasicsListScreen in Interface Builder
file. 2019-07-10 13:30:01.795516+0100 App[28784:1039713] *
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[< UIViewController 0x7fcc87619000>
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key tableView.'
* First throw call stack: (
Attach your tableView IBOutlet in code to your UITableView in Interface Builder.
Start Developing iOS Apps (Swift): Connect the UI to Code
Take a look at this page Terminating app due to uncaught exception 'NSUnknownKeyException' : iOS app crash. There may be a broken connection to a referenced outlet. The page above shows you what that looks like.
Another thing to look at is to make sure your table view is connected to the right class as Daniel Storm said. Open your storyboard and click on the view that you think is connected BasicsListScreen. Open the "Inspector" by clicking the button in the far upper right of Xcode. Then click "Identity Inspector" which is the middle button. It should show BasicsListScreen in the "Custom Class". Make sure that is right.
The error message sounds like a broken connection in BasicMathLevelOne.xib. It is the result from KVC trying to set a value on your InheritController for the key "you" but the class has no KVC compliant accessor (any more?).
To find the exact spot where the error occurs set an exception breakpoint in Xcode (press Command-6, click the "+" in lower left corner, chose "Add Exception Breakpoint"). Running the app in the debugger should make it stop at the place where the error occurs.
I've encountered a problem while creating an iOS app. That's weird because I though it would be very easy to do.
I've got a TableView and editActionsForRowAt which returns one action. Clicking on it should navigate to my second ViewController which is loaded from .xib file. I know that this function is executed when clicking the button but the VC does not open.
MainViewController.swift
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let action = UITableViewRowAction(style: .normal, title: "more") { (uiTableViewRowAction, indexPath) in
let detailsVC = DetailsViewController(nibName: "DetailsViewController", bundle: nil)
self.navigationController?.pushViewController(detailsVC, animated: true)
}
return [action]
}
DetailsViewController.swift (it's empty for now)
import UIKit
class DetailsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
I've called this pushViewController method multiple of times in the past so it makes me wonder why it doesn't show up. What's the problem?
edit:
present doesn't work either.
present(detailsVC, animated: true, completion: nil)
returns
TableView[2646:125041] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "DetailsViewController" nib but the view outlet was not set.'
Don't know what was a problem but I deleted the DetailsVC .swift and .xib and created new one and above code works fine...
I am new in iOS development, use xcode 8.2.1 & swift 3. I take uitableview in view controller, in uitableviewcell I take uiimageview when i tap on it app is crash and same issue is occur when I take button and perform button action .
The error is:
sampleToRunBuild[3752:1620857] -[sampleToRunBuild.TapViewController
TappedOnImage:]: unrecognized selector sent to instance 0x141e07bc0
2017-04-13 18:18:00.531126 sampleToRunBuild[3752:1620857] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason:
'-[sampleToRunBuild.TapViewController TappedOnImage:]: unrecognized
selector sent to instance 0x141e07bc0'
* First throw call stack: (0x18343d1b8 0x181e7455c 0x183444268 0x183441270 0x18333a80c 0x1898b3f80 0x1898b7688 0x18947e73c
0x18931d0f0 0x1898a7680 0x1898a71e0 0x1898a649c 0x18931b30c
0x1892ebda0 0x189ad575c 0x189acf130 0x1833eab5c 0x1833ea4a4
0x1833e80a4 0x1833162b8 0x184dca198 0x1893567fc 0x189351534
0x100065f30 0x1822f95b8) libc++abi.dylib: terminating with uncaught
exception of type NSException
my code is :
import UIKit
class TapViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell")
let img: UIImageView = cell?.viewWithTag(1) as! UIImageView
let img2: UIImageView = cell?.viewWithTag(2) as! UIImageView
img.tag = indexPath.row
img2.tag = indexPath.row
img.isUserInteractionEnabled = true
img2.isUserInteractionEnabled = true
let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:")))
tapped.numberOfTapsRequired = 1
tapped.delegate = self
img.addGestureRecognizer(tapped)
let tapped1:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:")))
tapped1.numberOfTapsRequired = 1
tapped1.delegate = self
img.addGestureRecognizer(tapped1)
return cell!
}
func TappedOnImage(sender:UITapGestureRecognizer){
print("tap on imageview")
}
}
Thanks in advance...
add UITapGestureRecognizer like below code
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView
// Your action
}
you need to use
#selector(TapViewController.imageTapped(sender:))
If you are using swift 3, don't use Selector() anymore, use the #selector syntax instead.
e.g.
let tapped1:UITapGestureRecognizer =
UITapGestureRecognizer(target: self, action: #selector(TappedOnImage))
The reason why your code did not work is that you forgot to add the argument label sender to selector string. That's one of the disadvantages of using the old syntax - it does not tell you the mistake at compile time. With the new syntax, there will be a compiler error if you wrote the selector incorrectly, and you don't even need to care about argument labels, you just need the name.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ExerciseMenuCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ExerciseOptionTableViewCell
let currentWorkout = workouts[indexPath.row]
cell.nameLabel!.text = currentWorkout.name
cell.photoImageView.image = currentWorkout.filename
cell.startWorkout.tag = indexPath.row
cell.startWorkout.addTarget(self, action:Selector("workoutAction:"), forControlEvents: .TouchUpInside)
cell.infoWorkout.tag = indexPath.row
cell.infoWorkout.addTarget(self, action:Selector("infoAction:"), forControlEvents: .TouchUpInside)
return cell
}
Both startWorkout and infoWorkout cause the application to crash with the error message 'unrecognised selector sent to instance'.
Example of code within the button actions. I am trying to return the indexPath of the button so I can then act on that.
#IBAction func workoutAction(sender: AnyObject) {
let buttonTag = sender.tag
print(buttonTag)
}
exact error message:
016-06-17 18:34:30.722 Exercises[4711:245683] - [Exercises.ExerciseMenu beginWorkout:]: unrecognized selector sent to instance 0x7fb47874a4b0
2016-06-17 18:34:30.727 Exercises[4711:245683] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Exercises.ExerciseMenu beginWorkout:]: unrecognized selector sent to instance 0x7fb47874a4b0'
A button within a custom cell cannot call an action that is in the housing view controller. You need to:
1) Move the #IBaction function to the custom cell class
2) Remove the add target code from 'cellFromRowAtIndexPath' and either write that in your custom cell(if you do this you don't need to write #IBAction) or create the connection from the button in storyboard to the #IBAction function
3) Create a delegate for your custom cell
Custom UITableViewCell delegate pattern in Swift
4) Call the delegate from your custom cell for the function you implement in the view controller
**Don't for get you need cell.delegate = self or it will crash when the delegate gets called
ex:
CustomCell.swift
protocol CustomCellDelegate {
func pressedButton()
}
class CustomCell: UITableViewCell {
var delegate: CustomCellDelegate!
#IBAction func buttonPressed(sender: UIButton) {
delegate.pressedButton()
}
}
ViewController.swift
class CustomClass: UIViewController, CustomCellDelegate {
func pressedButton() {
// Perform segue here
}
}
I've got a UITableViewCell that contains a button, I handle this button click event follow this link Get button click inside UI table view cell,
Here is my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.directCommentButton.tag = indexPath.row;
cell.directCommentButton.addTarget(self, action: "directCommentButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func directCommentButtonClicked(sender: AnyObject) {
// directComment = 1
println("DIRECT COMMENT")
}
but there is an error directCommentButtonClicked]: unrecognized selector sent to instance 0x7f9e50590f10' and app has crashed. When I remove the sender: AnyObject, the error disappear, but I want to get the sender, how can I do that. Tks in advance
Just pass your action selector this way:
cell.directCommentButton.addTarget(self, action: "directCommentButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
you have to add : because your function accepts an argument.