3

Can anyone help me in preventing dismissal of alertview on its button click event??

I have a textview as a subview of my alertView and i dont want to dismiss it if textview's value is nil.

Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
  • You want an AlertView that can't be dismissed? I'm pretty sure that would go against the user interface guidelines: what are you using it for? – benwad Oct 09 '12 at 14:55
  • 1
    Try with creating your own alert view: http://iphonedevelopment.blogspot.it/2010/05/custom-alert-views.html – Marco Pace Oct 09 '12 at 15:00
  • @benwad yea actuly my alertview will have two buttons "Dismiss" and "Send" ,User can dismiss it anytime by clicking dismiss bt i dnt want aletview to get dissmissed if user clicks "Send" without entering anything in textfield which is subview of alertview... – Vishal Singh Oct 10 '12 at 07:10

3 Answers3

4

As this is very old question,but I got one solution and though of posting if any other developer need in near future.

Implement protocol methods in .h file

In order to respond to button taps in our UIAlertView, we will use the – alertView:clickedButtonAtIndex: protocol method as

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

}
//Now below code will check if uitextfield value.
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    NSString *inputText = [[alertView textFieldAtIndex:0] text];
    if( [inputText length] > 0)
    {
       //text field value is greater than zero ,then Done button will appear as blue,or else it will be blurred
        return YES;
    }
    else
    {
        return NO;
    }
}

Alternately, there is a much faster approach:

return [inputText length] ? YES : NO;

The does the same thing as the if statement does.

Jessica
  • 9,379
  • 14
  • 65
  • 136
Nilesh Kumar
  • 2,141
  • 3
  • 16
  • 20
  • I'm not really one for adding to old questions unless it significantly adds to the rest of the answers, and actually think this does so +1 Good answer, may I make one suggestion though reference the `UIAlertViewDelegate` documentation. – Popeye May 29 '14 at 07:34
3

That might be against HIG Guidelines to NOT to dismiss an UIAlertView.

WORKAROUND : I don't know what goes on in your app, but to acheive this thing, what you could do is dismiss the AlertView and then check if textView's text is set or not. If it is set to nil, then bring up the alertview again!

mayuur
  • 4,736
  • 4
  • 30
  • 65
  • I think this is a right way to implement. There's no reason to keep alertView. Even if you wanna keep it, just think about "re-show" it, by keeping a reference, then call [alertView show]. – samthui7 Aug 11 '15 at 03:52
1

i'm not sure you can.

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView

is the only option available on callBack. And there's still at least one active button.

If you really need that specific behavior, try reimplementing your own UIAlertView

Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46
  • It's not necessarily true that there is at least one active button -- it may violate the HIG, but if you supply nil for the cancel button, and just have one other button, you can have that one disabled until the text view has some text in it. – rdelmar Oct 09 '12 at 15:14