Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access UIButton in UIAlertView

I'd like to selectively enable the 'save' button on my UIAlertView so that you can't save a file with no name. I can listen to text changed events for the attached UITextView (the style is UIAlertViewStylePlainText), but I can't see how to access the buttons so that I can do the enable/disable. I've tried iterating through [alertView subviews], but there is only the label in there (no buttons). Where do I need to look to directly access the UIButtons attached to a UIAlertView?

like image 325
James Edge Avatar asked Jan 20 '26 01:01

James Edge


1 Answers

Simple, just implement the UIAlertViewDelegate in your class and utilize the alertViewShouldEnableFirstOtherButton: delegate method. You can use this to check the length of the text field, and enable the button accordingly...

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return [[[alertView textFieldAtIndex:0] text] length] > 0;
}

Make sure you set your view controller to conform to this delegate in your interface using < UIAlertViewDelegate > and to set this class as the alerts delegate upon instantiation.

like image 104
Mick MacCallum Avatar answered Jan 22 '26 16:01

Mick MacCallum