Change View with NavigationViewController

42 Views Asked by At

all this is probably a trivial question, but I have not found a solution to it. I am making an app for Iphone using Swift.

I have a tableview with some strings and if I press a button I want to navigate back to the previous view directly. However, the code after my call

navigationController?.popViewControllerAnimated(true)

is always run, but I want the current activity to stop and go back to the previous view.

The code looks like:

    @IBAction func DeletePressed(sender: UIButton) {
    let deleteIndices = getIndexToDelete()
    navigationController?.popViewControllerAnimated(true)

    print("After navigationController")
    for index in deleteIndices{
        results?.ListResults[yearShownIndex].months[monthShownIndex].day[dayShownIndex].results.removeAtIndex(index)
    }
    if (results?.ListResults[yearShownIndex].months[monthShownIndex].day[dayShownIndex].results.count == 0){
        results?.ListResults[yearShownIndex].months[monthShownIndex].day.removeAtIndex(dayShownIndex)
    }
    if (results?.ListResults[yearShownIndex].months[monthShownIndex].day.count == 0){
        results?.ListResults[yearShownIndex].months.removeAtIndex(monthShownIndex)
    }
    if (results?.ListResults[yearShownIndex].months.count == 0){
        results?.ListResults.removeAtIndex(monthShownIndex)
    }
    loadView()
}

"After navigationController" is always displayed.

In android you would start a new activity by creating intents to get the desired behaviour, but how does it work on Iphone?

My problem is that I want to be able to go back directly when navigationController.popViewControllerAnimated is called. This is just a toy example to understand how it works so that I can use it in the if-clauses later.

2

There are 2 best solutions below

8
André Slotta On BEST ANSWER

you could simply add a return statement after you pop the viewcontroller:

@IBAction func DeletePressed(sender: UIButton) {
    let deleteIndices = getIndexToDelete()
    navigationController?.popViewControllerAnimated(true)
    return;
    [...]
1
proxi cod On

if you don't wants to execute code after "print("After navigationController")" then remove that code

or it is not possible to remove then toggle it when DeletePressed called