Avatar of pzozulka
pzozulka

asked on 

iOS: Firing a segue programmatically

I am creating an iOS app in Swift. I have a table view with two cells, and want to navigate to another view controller when the user clicks on one of the cells. In storyboard, to create a new segue, I control + dragged from Prototype Cell to the destination view controller. This automatically created the segue, and everything is working fine.

The problem is that I have two cells in the table (Repeat and Snooze). When the user clicks Repeat, it should take them to the Repeat View Controller, when the user clicks the Snooze cell, it should take them to the Snooze View Controller. So to handle this, in addition to the above mentioned "automatic" segue, I am also manually handling firing of Segue in didSelectRowAtIndexPath().
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if (indexPath.item == 0)
        {
            performSegueWithIdentifier("repeatDaysSegue", sender: indexPath.item)
        }
        else if (indexPath.item == 1)
        {
            performSegueWithIdentifier("snoozeSegue", sender: indexPath.item)
        }
    }

Open in new window

It seems to be working OK, but now when the user clicks on the Repeat cell, you can see two Segues happen in the UI. Also, prepareForSegue() gets fired twice. First time the sender is a UITableViewCell, second time sender is Int.

The class code is below, what am I doing wrong?

class AddEditAlarmViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

	let array = ["Repeat", "Snooze"]

	override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("settingsCustomCell")! as UITableViewCell
        cell.textLabel?.text = array[indexPath.item]
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if (indexPath.item == 0)
        {
            performSegueWithIdentifier("repeatDaysSegue", sender: indexPath.item)
        }
        else if (indexPath.item == 1)
        {
            performSegueWithIdentifier("snoozeSegue", sender: indexPath.item)
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "repeatDaysSegue")
        {
            let cellItem = sender as! Int
            if (array[cellItem] == "Repeat")
            {
                let repeatDaysView = segue.destinationViewController as! RepeatDaysTableViewController
                repeatDaysView.alarm = currentAlarm
            }
        }
    }
}

Open in new window

iOSSwift Programming

Avatar of undefined
Last Comment
pzozulka

8/22/2022 - Mon