UIAlertview not working fine in ios7 - iphone

Clicking on ok or cancel button in UIAlertview is not working fine. When I am clicking ok or cancel button, UIAlertview is not dismissed but if i clicking twice on ok or cancel button then and only then UIAlertview get dismissed.
This is my code.
alert = [[UIAlertView alloc] initWithTitle:#"Plese Enter comma seperated sets"
message:#"\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil] ;
CGRect rect = {12, 60, 260, 25};
dirField = [[UITextField alloc] initWithFrame:rect] ;
dirField.backgroundColor = [UIColor whiteColor];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
dirField.text = setString;
[dirField setText:#"test"];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];

Meghaji
I think there is problem in your method , I mean it is calling 2 times.
Thanks

Related

Adding TextView to UIAlertView

when I add textview on alertview, the scollview is shown on top and behind of it the text is drawn,how to overcome this problem. Here is the code
question updated:
If the set textView.editable = NO; it work's fine but in my case i need to type the text in textview.
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 50)];
[textView setText:#"lashdasjh asdasjdas asdlajsdl adsjajadsd aslj daj sdjasdjasjdlasjd as dlasj d"];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Type Your Message" message:#"\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Send",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
Resolved mySelf. Remove the #"\n\n\n\n\n\n" in message and add #"\n\n\n\n\n\n" in title
This doesn't work anymore on IOS7, but using https://github.com/wimagguc/ios-custom-alertview will help you get it done pretty well
replace your code with this code its work fine.
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Type Your Message" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Send",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];

How to add Cancel button between two other buttons (stacked) in UIAlertView (iOS)

I am trying to create a UIAlertView with three buttons (which will be stacked). I would like the Cancel button to be in the middle, between the two other buttons. I have tried setting the cancelButtonIndex to 1, but if there are two other buttons, it simply places them at indexes 0 and 1. I know I could just change the names of the buttons, but I want the darker blue formatting of the cancel button.
EDIT: **
Please note - I know how to get the three buttons with the titles in the correct order, but only if all three buttons essentially look like 'other' buttons; I want the cancel button to have the cancel button dark blue background so that it will look like a regular cancel button.
**
I've tried
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:button1Title,button2Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert show];
and
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:button2Title];
[alert show];
and
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:addButtonWithTitle:button1Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button2Title];
[alert show];
to no avail. Is it even possible to accomplish what I am trying to do?
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert show];
Might Help,
Cheers.
I have two ancillary points to this answer.
1) While, to the best of my knowledge, Apple has not rejected an app for reasonable modification of a UIAlertView; They have said that the view hierarchy of classes like UIAlertView should be considered private.
2) This question is a good example of why you should ask a question more about your end goal rather than the steps to get there. The only reason I know what this question is about is as a result of a comment left at my answer here.
Answer:
Because of your comment I know that you are looking to create a UIAlertView that has stacked buttons even when there are only 2 buttons.
I find the most logical place for code like this is in a category. Since generally the code needed to manipulate the alert-view needs to be around the show call, I created a category method I call instead of show and the method in turn calls show itself.
-(void)showWithButtonsStacked{
static NSString *tempButtonTitle = #"SomeUnlikelyToBeUsedTitle";
BOOL willAddFakeButton = (self.numberOfButtons == 2); // Button are only side by side when there's 2
if (willAddFakeButton){
self.clipsToBounds = YES;
[self addButtonWithTitle:tempButtonTitle]; // add temp button so the alertview will stack
}
BOOL hasCancelButton = (self.cancelButtonIndex != -1); // If there is a cancel button we don't want to cut it off
[self show];
if (willAddFakeButton){
UIButton *cancelButton = nil;
UIButton *tempButton = nil;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (hasCancelButton && [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){
cancelButton = button;
} else if ([button.titleLabel.text isEqualToString:tempButtonTitle]) {
tempButton = button;
}
}
}
if (hasCancelButton){ // move in cancel button
cancelButton.frame = tempButton.frame;
}
[tempButton removeFromSuperview];
// Find lowest button still visable.
CGRect lowestButtonFrame = CGRectZero;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (button.frame.origin.y > lowestButtonFrame.origin.y){
lowestButtonFrame = button.frame;
}
}
}
// determine new height of the alert view based on the lowest button frame
CGFloat newHeight = CGRectGetMaxY(lowestButtonFrame) + (lowestButtonFrame.origin.x * 1.5);
self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);
}
}
The way this method accomplishes it's goal is to add a temporary button to the alert-view to force the alert-view to stack the buttons, then it removes the temporary button and adjusts the height. Since it's a category method you use it simply by calling:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert showWithButtonsStacked];
This code results in an alert like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert setCancelButtonIndex:1]; // to make it look like cancel button
[alert show];
Set the cancel button to nil and just add it in the other buttons instead

No Keyboard with UIAlertViewStylePlainTextInput ?

I have a problem with the UIAlertViewStylePlainTextInput. I get my Alert with the textfield but there is no Keyboard coming up..? Here is my code:
UIAlertView *enter = [[UIAlertView alloc] initWithTitle:#"Highscores" message:#"Please Enter Your Name" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:#"Abbrechen" nil];
enter.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
[enter show];
I have tried without the
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
part but still there is no keyboard coming up.
(I've tested it on my device and on the simulator)
Do you have any ideas about that?
thank you in advance.
I had the problem because I was triggering the alert when a UITextField delegate method textFieldDidBeginEditing was called in my view and then I would fill this with the content of the Alert view text field. That meant that the UITextField in my view became first responder and I couldn't find a way to resign. So if you have the problem it is because something else is becoming first responder before the UIAlert is displayed. Think about how you trigger the alert.
I resigned to using a tap gesture on my UITextField which then triggers the UIAlertView with UIAlertViewStylePlainTextInput and it works fine.
Some code below:
-(void)addTextFieldToView{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textAlignment = UITextAlignmentCenter;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showAlertWithTextField:)];
[textField addGestureRecognizer:tapGestureRecognizer];
[self.view addSubview:textField];
}
-(void)showAlertWithTextField:(UITapGestureRecognizer *)gesture{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
}
Try below code:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Your name?"
message:nil
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
Works on iOS5
Might fix your problem as it solved mine:
[enter resignFirstResponder];

UIAlertView not displaying the keyboard

I have UIAlertView which is being displayed upon the load of a view.
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
av.alertViewStyle = UIAlertViewStyleSecureTextInput;
[av becomeFirstResponder];
[av show];
However the keyboard is not being display on the iPad or the simulator?
I have also tried
I just tried
[av becomeFirstResponder];
and also
UITextField *text = [alertView textFieldAtIndex:0];
[text becomeFirstResponder];
I just tried this piece of code and it logs that the textField is the first responder but still no keyboard.
if([[av textFieldAtIndex:0] isFirstResponder] == YES){
NSLog(#"av is the first responder.");
}
Making the alert into the first responder won't help. You need to make te text box inside the alert view into the first responder.
Edit:
You may need to call reloadInputViews (with or without the s, don't remember). Also double check that you're not changing the input views anywhere that might be breaking them.
Edit 2:
You might want to move the alert from viewDidLoad into viewDidAppear. I've seen problems with UI elements being updated/presented too early. This is one of those cases, I think.
To use
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
av.tag=1;
av.alertViewStyle = UIAlertViewStyleSecureTextInput;
[av show];
and use this method.
- (void)alertView:(UIAlertView *)alertViews clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertViews.tag==1)
{
[textfieldname becomeFirstResponder];
}
}
this works
self.alertView = [[UIAlertView alloc]initWithTitle:#"Login" message:#"Please enter you mobile number" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
self.alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView show];
but not this
UIAlertView* alertView = [[UIAlertView alloc] init];
[alertView initWithTitle:...]
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform Transform = CGAffineTransformMakeTranslation(0, 60);
[tf setBackgroundColor:[UIColor whiteColor]];
[av setTransform:Transform];
[av addSubview:tf];
[av show];
This works but you should be able to do this in IOS 5 using UIAlertViewStyleSecureTextInput??
I had the same issue, but my solution looks like it might not apply to original poster...
I had this code :
UIAlertView* alert = [[UIAlertView alloc] init];
[alert initWithTitle:...]
When run, no keyboard appeared.
I changed it to this, and keyboard now appears:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...];
Doing the init after alert was created seem to leave internal state of object as "this object doesn't need a keyboard!"

TextField Covering UIAlertView's Button

I am using a UIAlertView with three buttons: "Dismiss", "Submit Score" and #"View Leaderboard". The UIAlertView also contains a UITextField called username. At the moment the UITextField "username" is covering one of the buttons in the UIAlertView. I just wanted to know how I could stop the UITextField from covering one of the buttons, i.e move the buttons down.
Here is an image of what is happening:
screenshot http://img684.imageshack.us/img684/3055/screenshot20110108at191.png
And here is my code:
[username setBackgroundColor:[UIColor whiteColor]];
[username setBorderStyle:UITextBorderStyleRoundRect];
username.backgroundColor = [UIColor clearColor];
username.returnKeyType = UIReturnKeyDone;
username.keyboardAppearance = UIKeyboardAppearanceAlert;
username.placeholder = #"Enter your name here";
username = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
username.borderStyle = UITextBorderStyleRoundedRect;
[username resignFirstResponder];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congratulations"
message:[NSString stringWithFormat:#"You tapped %i times in %i seconds!\n", tapAmount, originalCountdownTime]
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"Submit To High Score Leaderboard", #"View Leaderboard", nil];
alert.tag = 01;
[alert addSubview:username];
[alert show];
[alert release];
I think you need to add some new lines and a final space at the end of the message string.
[NSString stringWithFormat:#"You tapped %i times in %i seconds!\n\n\n ", tapAmount, originalCountdownTime]
Here is a UIAlertView replacement class that supports user-input, custom width, and more:
https://github.com/TomSwift/TSAlertView
i had the same issue , your code has the following condition
[alert addSubview:username];
[alert show];
add the UITextfield after the [alert show]; as
[NSString stringWithFormat:#"You tapped %i times in %i seconds!\n\n\n ", tapAmount, originalCountdownTime];
[alert show];
[alert addSubview:username];
Thats all.working fine.
You shouldn't use a UIAlertView in this manner. It was never intended for this type of user interaction and view customization:
From the iOS Human Interface Guidelines:
You can specify the text of the required title and optional message, the number of buttons, and the button contents in an alert. You can’t customize the width or the background appearance of the alert view itself, or the alignment of the text (it’s center-aligned).
Perhaps consider building a custom view instead?