Can an Objective-C object be its own delegate? Is

2019-05-14 00:48发布

问题:

I know it is possible, but is it really a good programming practice? The idea is to subclass UIAlertView and subscribe myself as my own delegate in order to be able to add buttons and block handlers. That way, when I get the alertView:clickedButtonAtIndex: I call the block that was passed on.

I hope it's understandable. Is it a good programming practice?

UPDATE: Here's my idea of what I was asking https://github.com/ianmurrays/IMAlertView. Any comments would be greatly appreciated.

回答1:

Yes you are right, any class or object can be a delegate of any other, even of itself. But I would say it is not a good practice. You can create other class and delegate it to other. It bifurcates the work implementation and readability is maintained. And in future you can update your code with less labor.



回答2:

You can create a category to add blocks behaviour to the UIAlertView. You can check this this.