0

Currently, I'm working on a project in which a view is dismissing itself.

While talking to another programmer on the team for this project, he said that it's fine (in that it behaves correctly), but that I really should be using a delegation pattern to dismiss a view.

I've been programming in Objective-C for awhile now, but am still trying to fully understand design patterns, such as delegation. Is it acceptable for a view to dismiss itself, or should I in-fact be using delegation? When is delegation not an acceptable design pattern for use?

1 Answer 1

3

Delegation in code is the same thing as it is in real life; you're handing a task off to someone or something else to execute. In user interfaces that have event handling, this is a good thing, as the code behind the UI is already complex enough, and delegation helps avoid a big ball of mud. As a matter of general practice, I push as much logic as I can to external code, rather than keeping it in the UI.

That said, having a form close/dismiss itself is not exactly rocket science, and doing so encapsulates the functionality of closing within the form itself, rather than having to involve another object just to close the form. That's extra complexity for no additional benefit, especially if that's the only thing you're delegating to the external object.

So I'd say if the form is closing due to some interaction in or on the form (say, the user clicking a close button, or a timer in the form), then just close it in the form's code, and be done with it. If the form is closing due to some external action, then let that external action close it.

1
  • That's really, really helpful, thank you Robert! Yes, the user is clicking a close button, which is why I thought the simplest solution would be to have the view dismiss itself, rather than call a delegate that would then dismiss the view...it seemed a bit foolish to do something complicated for the sake of being 'correct'. That said, the content that view displays is determined by what's stored in the delegate, hence my initial uncertainty.
    – narner
    Jan 30, 2015 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.