-1

How can I gain access to a UIAlertView button's titleLabel property? I've tried the following code:

for (UIView *view in  action.subviews)
    {
        if([view isKindOfClass:[NSClassFromString(@"UIAlertButton") class]])
        {
            //Do something with title label font
        }
    }
nr5
  • 3,803
  • 7
  • 38
  • 68

1 Answers1

4

try like this ,you can change the view.tag based on your requirement

 - (void)willPresentAlertView:(UIAlertView *)alertView{
       for(UIView *view in alertView.subviews)
        if(([view isKindOfClass:[UIButton class]]) && (view.tag ==1)){
            UIButton *btn = (UIButton *)view;
            btn.titleLabel.text=@"your title";

        }
}
Balu
  • 8,440
  • 2
  • 20
  • 41
  • Two things: 1) Change `&` to `&&`. 2) This is not a good idea. Digging into the private subview structure of a standard UI widget is far from ideal. A future iOS update could cause this code to fail. A better solution would be to find a custom `UIAlertView` replacement that supports the customization you need. – rmaddy Aug 24 '13 at 05:37
  • @maddy I have neer understood this argument. If a new OS version is released, apps have to be retested anyway and adjustments might be required (just think of device rotation, device unique ID, root view controller etc). This is just one more (little) adjustment. – Krumelur Aug 24 '13 at 08:46
  • 1
    I Asked how to change the UIActionsheets button font (not the cancel button). I can change the title font and cancel button font. But the rest of the buttons are of type UIAlertButton. Question got edited I dont know why ! – nr5 Aug 24 '13 at 10:28
  • @Krumelur It's your choice but I see no reason to deliberately introduce fragile code into an app. The code could break from iOS 6.1.4 to 6.1.5 for example. Do you test every line of code for every single iOS update? Why make more work for yourself? Why risk a problem when a proper solution now saves lots of work in the future. Just some thoughts. – rmaddy Aug 24 '13 at 18:16