I have the below table view but I want to be able to deselect the target cell when I click off to the side onto my view controller. I checked about using the deselect row at index path but not sure if theres a way to trigger that by clicking off to the side.
]
When you click off your table view, some event handler must be triggered in order for you to do a row deselection. So if the rest of your view controller is a static without user interaction, you can simply place a transparent UIView over the rest of your view controller and do deselect like this:
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.checkAction(sender:)))
self.YourTransparentView.addGestureRecognizer(gesture)
func checkAction(sender : UITapGestureRecognizer) {
if let indexPath = self.tableView.indexPathForSelectedRow{
self.tableView.deselectRow(at: indexPath, animated: true)
}
}
Related
To build context, my app has a stack of cards (similar to Tinder) that flip and each could contain a lot of text on one side that require a UITextView for scrolling. It's basically a flashcard app, so once the user is done looking at the card, they swipe it away to view the next one.
I'm trying to make it so the user can tap anywhere on the card to flip it, so I added a clear button that fills up the card. My problem is that when the button is at the front, it blocks the text view from being scrollable, and when the button is at the back, the text view blocks it from being pressed.
Is there a way around this somehow to allow for both functionalities?
You can create a UITapGestureRecognizer, like so:
let gesture = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
addGestureRecognizer(gesture)
And the function to trigger:
#objc func tap(gesture: UITapGestureRecognizer) {
print("Tap!")
}
Maybe you can add a UITapGestureRecognizer to the text view itself. Something like:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(_:)))
textView.addGestureRecognizer(tapGesture)
#objc func didTap(_ gesture: UITapGestureRecognizer) {
// Handle the tap
}
did you try a tableView or a collectionView, you can customize your cell to look like a card then when a user clicks the cell it will flip or present a popup view, thats easier.
I would like to perform a segue to another view once a map view is tapped like when you check a location and tap on map on instagram, or like in foursquare.
I could not connect map view itself to another view directly like we do in buttons, and I don't want to perform segue when callout button is tapped.
I found a workaround by placing an invisible button into mapView, and performing segue when the button is tapped(user thinks mapView is tapped)
But I would like to know if there is a better solution for it, without a button.
You can use UITapGestureRecognizer
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapHandler(_:)))
mapView.addGestureRecognizer(gestureRecognizer)
#objc func tapHandler(_ gestureReconizer: UIGestureRecognizer) {
// let location = gestureReconizer.location(in: mapView)
// coordinatte of taplocation
// let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
// **you can navigate or perform Your Actions there**
}
I'm trying to pass a trigger between 2 view controller but i can't find something that works ...
I have the main controller with a NSTableView, source linked, View Base.
main Controller class :
class ViewController: NSViewController {
I have an array defined in Global variables.
on the viewDidLoad i add 2 elements in my array, then i use setDelegate and setDataSource.
It works fine.
myLib.myCoins = fillCoinsTable()
print ("My lib has \(myLib.myCoins.count) objects ")
mainCoinTable.setDelegate(self)
mainCoinTable.setDataSource(self)
I have a segue from the WINDOWS CONTROLLER to my second view Controller.
(It's a ToolBar button). The segue is "Sheet" kind.
This second controller allows me to add an element in my global variable arrays, with a button "Save" .
Code on the second controller
#IBAction func addCoinButton(sender: AnyObject) {
//We add the coin
let newCoin:coin = coin(n: textName.stringValue)
newCoin.Year = Int.init(textYear.stringValue)
myLib.myCoins.append(newCoin)
self.dismissController(self)
}
If i add a button on the main controller, to reloadData on the TableView, it works fine. The third element is added.
but i would like that to be automatic ....
I tried segue, but mine is not between view controller but with the windows controller.
Can you please help ?
Thanks,
Nicolas
I think, you want to add data from oneViewController and without pressing button, you want after navigation or pressing back button, you want to reload automatically tableview to show updated results, right.
If I'm not wrong then, this solution will work for you.
Follow this below steps:
Step 1 :
Add this code in your view controller, from which you want to add data in array or in database, instead of button click.
override func viewDidLoad()
{
super.viewDidLoad()
let backItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack")
self.navigationItem.leftBarButtonItem = backItem
}
func goBack()
{
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
self.navigationController?.popViewControllerAnimated(true)
}
Step 2:
Now its final step.
Now add this below code, to your result view controller, where you want to automatically update the result.
func loadList(notification: NSNotification){
self.TableView.reloadData()
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
}
Now implement this code-stuff in your coding. Hope it works for you.
If you want the table to reload automatically when the View appears add this to viewWillAppear
self.tableView.performSelectorOnMainThread(#selector(UITableView.reloadData), withObject: nil, waitUntilDone: true)
In my UIViewController, I have an UITableView which contains different cells with saved contents by using the NSUserDefaults. I also have a custom UIBarButtonItem which causes the UITableView to enter and leave editing mode.
This all works, but the problem is when I tap the round red button and on the right side of the cell appears the delete button, the delete button doesn't work... I can tap on it as much as I want but the cell does not get deleted.
#IBAction func EditListButton(sender: UIBarButtonItem) {
if ShoppingListTable.editing {
ShoppingListTable.setEditing(false, animated: true)
self.EditListButton.title = "Edit"
} else {
ShoppingListTable.setEditing(true, animated: true)
self.EditListButton.title = "Done"
}
}
Use UITableViewDataSource's tableView:commitEditingStyle:forRowAtIndexPath: to detect a tap on the delete button.
Then in the implementation delete the row from your data source (NSUserDefaults in your case).
So in your tableView's dataSource (ie. in the class you implemented numberOfSectionsInTableView: for example), add your custom implementation of tableView:commitEditingStyle:forRowAtIndexPath: where you delete the underlying data.
I have UIButton attached to navigationBar programatically:
catButton.addTarget(self, action: "oc:", forControlEvents: .TouchUpInside)
var catBarButton:UIBarButtonItem = UIBarButtonItem(customView: catButton)
self.navigationItem.setRightBarButtonItem(catBarButton, animated: false)
when function oc() is triggered I want to popover view controller
let segue = UIStoryboardSegue(identifier: "oc", source: self, destination: MDComDBCatsTVC())
prepareForSegue(segue, sender: but)
but this doesn't open MDComDBCatsTVC.. How to do it programatically because I can't drag from button in my storyboard because my button is added programatically
I can't drag from button in my storyboard because my button is added programatically
You can create the segue in the storyboard by dragging from the view controller icon to the target view controller. This segue can then be used programmatically.
Click on the segue to assign an identifier in the Attributes inspector panel. The segue can then be accessed programmatically using this identifier.
Xcode will insist that popover segues have an anchor. The error message is “Popover segue with no anchor”. This can be resolved in the Attributes inspector - the Anchor property. Drag from the circle to your view.
In the action for your programmatically created button you will then be able to access the segue using eg:
func buttonAction(sender:UIButton!)
{
performSegueWithIdentifier("popoversegue", sender: self)
}
( ... or more likely something more robust using if let etc)
you can use dynamic popup views like that
let vc = storyboard.instantiateViewControllerWithIdentifier("myPopupView") as! myPopupViewViewController
self.presentViewController(vc, animated: true, completion: nil)
If you want to do this via storyboard connect your view controllers in storyboard with segue (drag from first view controller to second one, it doesn't mean to drag from button only) name that segue "oc" then in button action perform segue like following
performSegueWithIdentifier("oc", sender: but)