How to validate and restrict input to UITextField inside UIAlertView in iOS - ios

I am using the UIAlertView to show the text field over a popup. But the problem I am facing is that I am not able to validate and restrict the entries in UITextField over a popup, i.e. I want to accept only 3 numeric values.
Here i am providing the block of code that I have implemented.
popup = [[UIAlertView alloc] initWithTitle:#"Please enter 3 numeric values"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
txtFld = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[txtFld setBackgroundColor:[UIColor whiteColor]];
[txtFld setKeyboardType:UIKeyboardTypeNumberPad];
[popup addSubview:txtFld];
[popup show];
[txtFld becomeFirstResponder];
So can anyone help me out to resolve this issue? Thanks in advance.

here I can see that you have not set textfield.delegate=self , please check it in your code

It is risky to manually add subviews to UIAlertView or any other view you do not control as its hierarchy is private and subject to change. If you're targeting iOS 5 or above, you can use one of the UIAlertView styles that supports text entry and validation such as UIAlertViewStylePlainTextInput. The documentation for UIAlertView provides a list of styles and this tutorial explains their use in more detail.

Related

iOS UIAlertView with photo

I wanted to use UIAlertView to show photo. I´m using code as below for showing the alert, however it doesn't work. It shows the title, some space and button OK nothing more. I have no idea if I´m doing something wrong, because I´m new in iOS.
-(IBAction)ButtonPressed:(id)sender
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"titel"
message:nil
delegate:nil //or self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
[imageView setImage:#"dog.png"];
[av setValue:imageView forKey:#"accessoryView"];
[av show];
}
#Popeye is correct what he said : UIAlertView is meant to be used as-is and you shouldn't be messing with the view hierarchy this will get your app rejected from the Apple App review process. Please read Apple documentation regarding UIAlertViews. So you should NOT be using setValue: forKey:#"accessoryView" and/or addSubview:. Specific section below
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.
Solutions
1) Use 3 party such as CNPPopupController and many other available check this link
OR
2) create of your own.

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];

Accessibility: On updating UIAlertView's message, the text that Voice over reads does not change?

Please refer the code below:
UIAlertView *progressAlertView = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
progressAlertView.message=#"Hello, I am the new one";
Voice over always reads "Message" and never reads the new message string set in the second line "Hello, I am the new one". There's no way to change this accessibility label of the bodyTextLabel control/subView of UIAlertView.
Any idea how to make UIAlertView change its accessibility labels after its alloc'ed?
Thanks,
-Shilpi
You can use accessibilityTraits as UIAccessibilityTraitUpdatesFrequently to read updated message.
it works like:
alertObj.accessibilityTraits = UIAccessibilityTraitUpdatesFrequently;
Thanks
use this way for set message of UIAlertView
UIAlertView *progressAlertView = [[UIAlertView alloc]
initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:nil];
[alertView setMessage:#"Hello, I am the new one"];
[alertView show];
For More About UIAlertView Refer this
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
You can change the subviews of UIAlertView to change single attributes like the accessibility label:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test"
message:#"Message"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
// In a standard UIAlertView there are 2 UILabels and some buttons
// The UILabel with the message is always the 2nd object in the array
NSArray *subviews = alert.subviews;
UILabel *messageLabel = [subviews objectAtIndex:1];
[messageLabel setAccessibilityLabel:#"Some other text"];
With this code you only change the accessibility label, so that VoiceOver reads another text but the old text is shown.
In your case you should set the accessibility label to the same as you want to set the message of the UIAlertView.

Customizing the buttons on a UIAlertView

This is the current way i'm customizing my buttons:
UIAlertView *av = [[UIAlertView alloc] init];
[av addButtonWithTitle:#""];
UIButton *yesButton = [av.subviews lastObject];
[av show];
[yesButton setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
The problem with this is that the original view is still visible around the image I set for the button. It doesn't completely encapsulate the image. Here's an example of what I have so far:
https://www.dropbox.com/s/htb9pfihwmel5oo/testimage.png
Is there anyway to make it so that the image completely takes up the entire button?
If you wan't something that's not as robust as Sly Raskal's answer and just want a quick hack, that works similar to your existing code. You could do something like this..Of course you can change the new buttons frame to match how you want it. Also, you would have to define the UIAlertView as a variable to so you have a reference to it, and dismiss it in your someAction: method.
UIAlertView *av = [[UIAlertView alloc] init];
[av addButtonWithTitle:#""];
UIButton *yesButton = [av.subviews lastObject];
[yesButton setHidden:YES];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[av addSubview:button];
[av show];
[button setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[button addTarget:av action:#selector(someAction:) forControlEvents:[yesButton allControlEvents]];
[button setFrame:yesButton.frame];
If in this case all you are doing is styling the buttons to look different, but in a simple fashion, I would follow this tutorial to accomplish the effect you are looking for:
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/
If the application you are creating is destined for the App Store, I just want to also mention that whatever changes you implement, be sure it doesn't use any private API's or goes against anything documented in the HIG. Either of these circumstances is surely going to red flag your app for rejection.

Show an image in the alert body of a local notification

I am using Local Notifications, but I want to put an image in the message body of the alert. How can I do that?
I also searched for the same question. And found that we can't customize the UILocalNotification, so I handled this in application:didReceiveLocalNotification: by showing custom UIAlertView.
This should work. Give it a whirl:
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
UIImage *smiley= [UIImage imageNamed:#"smiley.png"];
[imageView setImage:smiley];
[smiley release];
[successAlert addSubview:imageView];
[imageView release];
[successAlert show];
[successAlert release];
Good luck,
Aurum Aquila
If you are using simple images like question mark, notification, etc. I suggest Ext JS which has a Icon type alert.
You can add your images as well.

Resources