Search code examples
iosxcodeswiftuitableviewnsindexpath

Keep track of taps on a button?


Is there a way to keep count of how many times a user taps a button? i am hoping to perform a segue after the user taps a button so many times. As of right now I am having my backend keep track of how many taps.

@IBAction func nextbuttontest(sender: AnyObject) {

        let button = sender as! UIButton
        let view = button.superview!
        let cell = view.superview as! NewFeedControllerCell
        let indexPath = tableView.indexPathForCell(cell)
        let row = indexPath?.row

        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: row!+1, inSection: 0), 
                                         atScrollPosition: UITableViewScrollPosition.Top, 
                                         animated: false)   
    }

Solution

  • You can create integer variable inside you view controller, and use it as a counter -> that is, when user taps increment it and if you have enough taps perform a segue or anything else.

    For instance:

    var tapCounter = 0
    

    as a declaration in your view controller.

    func tapGestureForElement(gest:UIGestureRecognizer){
        tapCounter = tapCounter + 1
        if(tapCounter == 123){
            //performSegue
        }
    }
    

    EDIT::

    for the second part:

    if(row!+1 == yourArray.count){ // or == 10, as you mentioned in comments, but I think that is a bad practice
        //do nothing or do something, up to you :)
    {
    else{
        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: row!+1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
    }
    

    Edit 2:

    you can even do something better, you can check if you reached end of the rows in section:

    if( row!+1 == tableView. numberOfRowsInSection(indexPath.section)
    

    Last part is not tested, but that should be it.