Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Self-Destructing Message iPhone App Capturing Photo and Video Using UIImagePickerController Resizing an Image and Getting Ready to Send

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

UIAlertView delegate: nil vs self

I have been following the self-destructing message app and have noticed in this video that the delegate for UIAlertView was set to self instead of nil like the usual sign-in/sign-up pages. What's the difference and how does it affect the program? Why was self not used for sign-up & sign-in etc?

I tried looking it up in the iOS library but the definition is kinda vague.. "The receiver’s delegate or nil if it doesn’t have a delegate." : /

Will be very grateful to anyone that can help explain this to me. : )

2 Answers

Rutger Farry
Rutger Farry
10,722 Points

Setting another class as your delegate means that you are handing some control over to the delegate. The delegate can implement methods that have been declared in the other class (called protocols) that allow it to make decisions about what goes on in that class.

In this case, setting UIAlertView's delegate will allow the chosen delegate to customize UIAlertView's buttons, text, and functionality when a button is pressed. (for example if your app is trying to connect to the internet, but airplane mode is on, it could implement a button that would redirect you to settings to turn airplane mode off).

The instructor might be planning to implement custom functionality for this alert view down the road, and that is why he set the delegate as self instead of nil. Or maybe he's not, in which case its delegate might as well be nil, leaving it with UIAlertView's default behavior.

Here's some more information about delegates

...and here's a list of the methods you can implement for UIAlertView

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

Thanks for this answer. I now understand the delegate more clearly. : )

Patrick Cooney
Patrick Cooney
12,216 Points

I don't remember the exact code as it was quite a while ago that I completed this project. In general though, when you send something self you're sending it the instance of itself. When you send it nil you're saying, there's nothing here. Basically nil allows you to provide a parameter that will have no effect on the execution of the method.

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

Thanks for this answer, I kind of get you but still can't really visualize what situations I would need to use self against nil. Do you think you can provide any examples as to when they really matter?