toggleEditingMode method


#1

Hi please help me to clarify the following code.

-(IBAction)toggleEditingMode:(id)sender
{
    //if you are currently in editing mode...
    if(self.isEditing){
        //change text of button to inform user of state
        [sender setTitle:@"Edit" forState:UIControlStateNormal];
        
        //turn off editing mode
        [self setEditing:NO animated:YES];
    }
    else{
        //change text of button to inform user of state
        [sender setTitle:@"Done" forState:UIControlStateNormal];
        
        //enter editing mode
        [self setEditing:YES animated:YES];
    }
    
}

From my understanding, i have no idea why “self.isEditing” is TRUE it should be editing mode, but why it want to display the text “Edit” and turn off the editing mode?
I suppose when “self.isEditing” is TRUE, the view should be currently in edit mode and hence displaying text “Done” and enable editing mode?

Please explain I’m confuse by the convention of the code.


#2

Almost.

Remember that the method name is -toggleEditingMode:. A toggle just flips back and forth between two states. So to “toggle editing mode” means if the app is already in editing mode, turn it off; if the app is not in editing mode, turn it on.

Thus, if self.isEditing is YES (true), the view is already in editing mode, so the button the user just hit was labelled “Done”. So we need to get out of editing mode and set the label to “Edit”. Perhaps it would make more sense to flip the order around:

-(IBAction)toggleEditingMode:(id)sender
{
    if(self.isEditing) {
        
        //they're already in edit mode, so turn it off
        [self setEditing:NO animated:YES];

        //...and change text of button to let user know they can turn it back on
        [sender setTitle:@"Edit" forState:UIControlStateNormal];
    }
    else {

        //they're not yet in edit mode, so turn it on
        [self setEditing:YES animated:YES];

        //...and change text of button to let user know they can turn it back off
        [sender setTitle:@"Done" forState:UIControlStateNormal];        
    }   
}

#3

[code]

-(IBAction)toggleEditingMode:(id)sender
{
if(self.isEditing) { //already in editing mode

    //they're already in edit mode, so turn it off
    [self setEditing:NO animated:YES]; //this make sense.

    //...and change text of button to let user know they can turn it back on
    [sender setTitle:@"Edit" forState:UIControlStateNormal]; //I dont understand this...
}
else { //user not in edit mode

    //they're not yet in edit mode, so turn it on
    [self setEditing:YES animated:YES]; //this make sense

    //...and change text of button to let user know they can turn it back off
    [sender setTitle:@"Done" forState:UIControlStateNormal];     //i dont understand this   
}   

}[/code]


#4

Let’s look at just part of the code:

    if(self.isEditing) { //already in editing mode
        
        //they're already in edit mode, so turn it off
        [self setEditing:NO animated:YES]; //this make sense.

        //...and change text of button to let user know they can turn it back on
        [sender setTitle:@"Edit" forState:UIControlStateNormal]; //I dont understand this...
    }

If the body of that ‘if’ is entered, it’s because they were already in editing mode (which you understand), and want to get out. The button they hit just now to get OUT of edit mode was labelled “Done”.

When we flip the switch to turn off editing mode (setEditing:NO), we can’t leave that button on the screen saying “Done”! (Since the user would not longer be in edit mode, they would think, “Done with WHAT?!”)

So instead, we reuse that same button to allow them to get back IN to editing mode. But we’d be run out of town if hitting a button that said “Done” really got us back into editing mode. That’s not what we expect from iOS apps, and we’re sure not going to do that to others.

Thus, we change the label on the button to say “Edit”, to let them know that if they hit the button again, they’ll get back into edit mode.

The “if( self.isEditing )” is checking how things are now; everything in the body of the ‘if’ is setting how things will be when we finish.


#5

I was puzzled in exactly the same way as the original poster on this thread and I am glad that he posted his question.

I eventually convinced myself of the logic by first realizing that this method is only called once the the user clicks the button.

Therefore:

  1. when the app starts, it’s not in editing mode.
  2. the user clicks the ‘edit’ button and the toggleEditingMode is called
  3. within the toggleEditingMode, only the ‘else’ condition is met
    3.a. so the edit mode is turned on
    3.b. and the label is updated to ‘Done’ so that ‘Done’ button is available to click when user is done editing.

MY PROBLEM is that instead of showing ‘Done’, I am getting ‘…’ ???


#6

The text is getting cut off because the width of the button’s text label is too short and is being replaced by “…”. In Interface Builder make the “Edit” button width a bit wider and it will display properly.