How do I add custom buttons to UIAlertView?

advertisements

How do I add custom buttons to UIAlertView ??


If you only want to customize the button's names, then you can use the "otherButtonTitles" section of the following initializer:

– initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

(You can also add buttons with a custom name using the - (NSInteger)addButtonWithTitle:(NSString *)title instance method).

However, if you want to customize more than just the title of the button, then you need to add a subview of your custom button, as show in madhu's sample code.

You will also need to set the frame of your newButton object so as it's positioned where you want it inside the UIAlertView instance. Because the UIAlertView isn't particularly designed for nesting subviews within it (this is a common thing for developers to want to do and struggle with), you may encounter some strange effects that requires fiddling around with your view hierarchy in order to solve. I'm sure there are plenty questions out there that describe some of these strange effects, and I myself have asked about at least one of them!