UIAlertView grey out button - ios

I have a UIAlertView with multiple buttons. Is it possible to grey out and disable a button? I want the button to be visible but clear that it can't be pushed. Any suggestions are appreciated.

Make sure that you have enabled the current VC as implementing the <UIAlertViewDelegate> protocol, and then in your VC you could do the following:
- (void)viewDidAppear:(BOOL)animated {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!" message:#"This is an alert view" delegate:nil cancelButtonTitle:#"Cancel!" otherButtonTitles:#"Off", #"On", nil];
alert.delegate = self;
[alert show];
}
/* UIAlertViewDelegate methods */
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return NO;
}
// Other UIAlertViewDelegate methods...
Now why you would want to show a UIAlertView with a button which didn't have any functionality is a whole different question... :)

I hear of lots of people subclassing UIAlertView, but then I read this comment on Apple's UIAlertView Class Reference page:
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
i.e. people should not be attempting to modify the elements or behavior of UIAlertView. It's likely that behavior can change in later versions of iOS (e.g. iOS 8 or iOS 7.1 or whatever), breaking their various modifications to UIAlertView.
Anyways, to answer your question: why not try creating your own UIView that you can add as UIAlertView-like subview on top of any of your views? That way, you'd have easy control over both the buttons and their behavior.

Related

UIAlertView crashes when button tapped: how to solve?

I have a class that I instance to show an alert view like this:
- (void)showAlert
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Do you want to try again?"
message:nil
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
[alertView show];
}
}
I need self to be the delegate because I need alertView:didDismissWithButtonIndex: to be called to perform some actions when the user taps the alert view's button. This usually works well, but from time to time, I get this crash:
SIGSEGV
UIKit-[UIAlertView(Private) modalItem:shouldDismissForButtonAtIndex:]
I guess this is because the delegate, for any reason, was released, right? Or is this because what was released was the alert view? How could I solve this? I need the alert view to have a delegate, and I've reading several related posts and I couldn't find an answer that fits my scenario.
I'm testing in iOS 7.0, I don`t know if that could have to do with the issue.
Thanks in advance
It seems that you tap alert when its delegate is released:
delegate:self
It happens because UIAlertView delegate property is of assign type (not weak!).
So your delegate potentially can point to released object.
Solution:
in dealloc method you need to clear delegate for your alertView
- (void)dealloc
{
_alertView.delegate = nil;
}
But before you need to make iVar _alertView and use it for your alertViews
- (void)showAlert
{
_alertView = ...;
[_alertView show];
}
Update your code as follows:
- (void)showAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Do you want to try again?"
message:nil
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
[alertView show];
}
Its due to you are missing the nil for otherButtonTitles part.
Missing sentinel in method dispatch warning will be shown if you didn't add nil.
That's because the alertView's delegate object released while clicking the button. I think it's a bug of SDK:
#property(nonatomic,assign) id /*<UIAlertViewDelegate>*/ delegate; // weak reference
should be:
#property(nonatomic, weak) id /*<UIAlertViewDelegate>*/ delegate; // weak reference
To fix the issue:
add a weak delegate for UIAlertView using association.
swizzle the init, setDelegate: delegate methods, set alertView delegate to self, set step 1 weak delegate with the param delegate.
implement all delegate methods, deliver the methods using the weak delegate.

How do I add `accessibilityLabel` to `UIAlertView` buttons?

How do I add accessibilityLabel to UIAlertView buttons?
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Announcement"
message: #"message!"
delegate: nil
cancelButtonTitle: #"cancelButton"
otherButtonTitles: #"otherButton"];
[alert show];
According to Apple's documentation (search for 'Making Alert Views Accessible'), AlertViews are 'accessible by default'. This, and the fact that the buttons aren't editable, means that you probably shouldn't try changing the accessibilityLabels yourself. By default they use the button's title and the word 'button', which should be fine.
Accessibility for alert views pertains to the alert title, alert message, and button titles. If VoiceOver is activated, it speaks the word “alert” when an alert is shown, then speaks its title followed by its message if set. As the user taps a button, VoiceOver speaks its title and the word “button.” As the user taps a text field, VoiceOver speaks its value and “text field” or “secure text field.”
The only way you can do this is finding the UIButtons within the UIAlertView subviews:
for (id button in self.alertView.subviews){
if ([button isKindOfClass:[UIButton class]]){
((UIButton *)button).accessibilityLabel = #"your custom text";
}
}
However this is the only way to do it because there is not a public API to access these UIButtons and this is because Apple doesn't want you to access them. Accessing internal views of the UIAlertView class is something which Apple does not allow and it is likely that will make your app rejected during the App Store review process.
If you really need to have UIButtons with a custom accessibilityLabel you should look into designing a custom alert view instead of using the Apple UIAlertView class.
Late, but maybe useful to someone.
You can get access to the alert buttons using their index. For instance you can find the second button in the screen like the following way in Objective C:
- (void)getAlertActionButton:(XCUIApplication *)app {
// any code
XCUIElement *secondAlertButton = [[[app.alerts otherElements] buttons] elementBoundByIndex:1];
// any code
}
To find your alert window hierarchy use debug mode.

UIAlertView not dismissing?

I have a few ViewController subclasses inside a UINavigation controller. I have successfully used UIAlertViews elsewhere in the application, and I know how to set the delegate and include the correct delegate methods, etc.
In a ViewController with a UITableView, I have implemented a 'pull to refresh' with a UIRefreshControl. I have a separate class to manage the downloading and parsing of some XML data, and in the event of a connection error, I post a notification. The view controller containing the table view observes this notification and runs a method where I build and display an alert:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Connection Error" message:[[notification userInfo] objectForKey:#"error"] delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show];
The alert displays correctly, but the cancelButton is unresponsive - there is no way to dismiss the alert! Putting similar code (identical, but without the notification's userinfo) in the VC's viewDidLoad method creates an alert that behaves normally.
Is the refresh gesture hogging first responder or something? I have tried [alertView becomeFirstResponder]. I would be grateful for any advice…
Update: screenshot included… is this the right info? (can't embed this image for lack of reputation) http://i.stack.imgur.com/4CGqS.png
Edit
It seems like you have a deadlock or your thread is stuck waiting. You should look at your code and see what causes this.
Original answer which lead to update in OP
Make sure the alert is shown on the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
//Open alert here
});
This isn't a solution per se, but I might try two quick things as you troubleshoot:
1) Hardcode some text in the UIAlert, rather than passing in the notification object. See if there is any change in behavior.
2) Try adding another button to the alert and an accompanying method to catch it. So you'll see if the delegate is getting ny buttons messages at all.
Try adding a tag to the alertView
alertView.tag = 0;
Then create the method alertView:clickedButtonAtIndex: in the view controller.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 0) {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
}

iOS UIAlertview clear text and disable OK

I'm a bit new to iOS development, and right now am working on some simple UI-related stuff. I have a UIAlertView that I'm using at one point to allow the user to enter some text, with simple Cancel and OK buttons. The OK button should be disabled if the text field is blank.
I added to my UIAlertViewDelegate an alertViewShouldEnableFirstOtherButton function, so the OK button would disable when there's no text, and I also set the UIAlertView's UITextField to have clearOnBeginEditing true, so the previous text would be gone every time I displayed the alert. Each of these things works perfectly on their own. Unfortunately, it seems like the AlertView is checking whether or not to enable the OK button before the text field is cleared, so when they're put together it comes up enabled. Below should be about the minimal code needed to reproduce.
-(void)viewDidLoad
{
textEntryBox = [[UIAlertView alloc] initWithTitle:#"Name" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[textEntryBox setAlertViewStyle:UIAlertViewStylePlainTextInput];
[textEntryBox textFieldAtIndex:0].clearsOnBeginEditing = YES;
}
-(IBAction)functionTriggeredByOtherLogic
{
[textEntryBox show];
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if(alertView == textEntryBox)
{
if([[alertView textFieldAtIndex:0].text length] > 0)
{
return YES;
}
else
{
return NO;
}
}
return YES;
}
So, ultimately, my question is this: am I doing something completely against the natural iOS way of doing things here? Is there a better way to do this? Should I just ignore the clearsOnBeginEditing property of the UITextField, and manually clear the Text property before showing the UIAlertView?
Try to set the textfield delegate to self
[[textEntryBox textFieldAtIndex:0] setDelegate:self]
and implement this method :
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setText:#""];
}
I'm also having a UIAlertView with a textField to fill-in in my app, and it works for me
Using an alert view for this is probably a bit much. It might be easier if you use the master-detail paradigm and just push a new view controller where you can enter your values.

Ask user opinion, with custom UIAlertView, what's the limit to be approved?

I'm creating an app, and at one moment i want to ask the user opinion with a custom UIAlertView. After a lot of researches, and reading about this subject, i'm a little confused about some things...
What objects (UITextfields, UIImages...) could we add to an UIAlertView ?
Because i found this :
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
But this could have been accepted for example :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"msg" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[alertView addSubview:txtField];
[alertView show];
[alertView release];
(See this link for more informations)
So for example, if i don't want the blue-box with message, and title parameters, could i only do this code :
UIAlertView *alert = [[UIAlertView alloc] init];
[alert addSubview:a_UIButton];
[alert addSubview:a_UIImageview];
[alert show];
So with this sort of code i could get the advantage (if it's work !) of put in pause mode all the app', and all would be custom. But is it possible ? Could it be reject ?
Thanks a lot !
Instead of trying to customize "UIAlertView", why not just create your own custom AlertView (from "UIView") and add that as a subview when you want to display it?
We don't know if (or how) Apple might change the internal architecture of UIAlertView in future versions of iOS (e.g. iOS 7) that would break all the various customizations done to it in all the apps that have gotten away with it so far. That's why Apple put up that warning in their documentation.
I would recommend instead that you make a custom UIView, and just display that with a similar animation to the UIAlertView. Then, you can add whatever UI elements you'd like. If the docs say the view hierarchy is private, I'd leave it alone.
If all you want to do is add a text field to the alert view, that functionality is supported in iOS 5 and later using:
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];

Resources