UIAlertView with textField - objective-c

I experiment at the moment with some alertViews. Now try to set an AlertView with two textField`s. When I click "done" it should show a Log.
At the moment, the code looks like that:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Titel" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:#"Cancel", nil];
The TextField should have placeholder. How do I set it up?

Try this: it should work:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Your_Title" message:#"Your_message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
// Alert style customization
[[av textFieldAtIndex:1] setSecureTextEntry:NO];
[[av textFieldAtIndex:0] setPlaceholder:#"First_Placeholder"];
[[av textFieldAtIndex:1] setPlaceholder:#"Second_Placeholder"];
[av show];
You can access the values of the text fields on the alertView:clickedButtonAtIndex: of UIAlertViewDelegate.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"1 %#", [alertView textFieldAtIndex:0].text);
NSLog(#"2 %#", [alertView textFieldAtIndex:1].text);
}

Related

multiple alert view in one view controller (tag doesn't work)

I have read some methods using alert view .tag, but that doesn't work.
The first alert:
-(void)addNewTask
{
UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:#"New Task"
message:nil
delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
alert1.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert1 show];
}
The Second Alert
-(void)changeTime:(int)value atRow:(int)p
{
TaskData *data = [tasks objectAtIndex:p];
NSLog([NSString stringWithFormat:#"%d %d",data.time,value]);
int time = data.time ;
time += value;
data.time = time;
NSLog([NSString stringWithFormat:#"%d %d",data.time,value]);
[self saveData:tasks];
[self.Taskview reloadData];
if(time>=5&&(data.leveljudge!=1))
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Congratulation!!!"
message:#"You have achieve Senior Level. "
delegate:nil
cancelButtonTitle:#"Cancel" otherButtonTitles:#"YES",nil];
data.leveljudge=1;
[alert2 show];
}
Delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *name = [alertView buttonTitleAtIndex:buttonIndex];
if ([name isEqualToString:#"OK"])
{
UITextField *tf=[alertView textFieldAtIndex:0];
NSString *name = tf.text;
TaskData *newTask = [[TaskData alloc] init];
newTask.TaskName = name;
newTask.time = 0;
newTask.leveljudge=0;
[tasks addObject:newTask];
[self saveData:tasks];
[self.Taskview reloadData];
}
else if ([name isEqualToString:#"YES"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://d.blog.xuite.net/d/1/5/4/12103250/blog_1606564/txt/53796893/2.jpg"]];
}
}
The problem is the delegate only work for the first alert.
My app looks like: https://www.youtube.com/watch?v=WxlVKk0CTiQ
-(IBAction)flipAction:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is My Button" message:#" Hi" delegate:self cancelButtonTitle:#"OK " otherButtonTitles:#"Cancel", nil];
alert.tag=1;
[alert show];
[alert release];
}
-(IBAction)flipAction123:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is a flipAction123" message:#"this is message..." delegate:self cancelButtonTitle:#"OK " otherButtonTitles:#"Cancel", nil];
alert.tag=2;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
if (alertView.tag==1)
{
if (buttonIndex==0)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is another alert button" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
else
{
NSLog(#"paresh here..");
}
}
else if(alertView.tag==2)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"This is another alert alertView.tag ==2" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
}
}
For alert2, Delegate is not called because you have not set the delegate.
Try making the following changes.
Alert1
UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:#"New Task"
message:nil
delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
alert1.alertViewStyle = UIAlertViewStylePlainTextInput;
alert1.tag = 101; //Add a Tag to the alert
[alert1 show];
Alert2
Alert 2 delegate is not set properly
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Congratulation!!!"
message:#"You have achieve Senior Level. "
delegate:self //Set your delegate
cancelButtonTitle:#"Cancel" otherButtonTitles:#"YES",nil];
alert2.tag = 102; //Add a different tag value to the second alert
data.leveljudge=1;
[alert2 show];
Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 101)
{
//Handle Alert 1
}
else if(alertView.tag ==102)
{
//Handle Alert 2
}
}
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Congratulation!!!"
message:#"You have achieve Senior Level. "
delegate:self //here should be self, not nil
cancelButtonTitle:#"Cancel" otherButtonTitles:#"YES",nil];
data.leveljudge=1;
[alert2 show];

UIAlertView alertView.cancelButtonIndex = -1; Not Working

I am trying to create an AlertView which does not have a cancel button.
This is the code I have tried:
alertView.cancelButtonIndex = -1;
It does not seem to work with iOS 7.
Any ideas?
Thanks,
1) With using this code pressing "Do something button" can do some action but will not lead to alertView hiding
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title" message:#"The message." delegate:self cancelButtonTitle:nil otherButtonTitles: #"Do something", nil] show];
#pragma mark - UIAlertViewDelegate
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return NO;
}
2) With using this code there will be no buttons on the alertView(I think this is what you actually need)
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title" message:#"The message." delegate:self cancelButtonTitle:nil otherButtonTitles:nil] show];

Objective C dismissViewControllerAnimated move last view down

I have two ViewController
VC1 adding VC2 as an subview to itselfe.
VC2 has a UIButton, which is calling the follow Code:
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:#"BlaBla."]];
NSArray *toRecipients = [NSArray arrayWithObjects:#"blabla#gmail.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = [NSString stringWithFormat:#"Testtext1"];
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Fehler" message:#"Ihr Gerät unterstützt die gewünschte Funktion nicht" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
After the Mail is done it calls the following:
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result)
{
case MFMailComposeResultCancelled:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Vorgang abgebrochen, es wurde keine E-Mail versendet."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
case MFMailComposeResultSaved:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Ihre E-Mail wurde gespeichert."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
case MFMailComposeResultSent:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Ihre E-Mail wird versendet."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
case MFMailComposeResultFailed:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"Vorgang abgebrochen, die E-Mail konnte nicht gesendet werden."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
default:
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hinweis"
message:#"E-Mail wurde nicht gesendet."
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[alert show];
}
break;
}
[self dismissViewControllerAnimated:YES completion:nil];}
All works fine, but all UIButtons etc in VC2 are now moved, I guess, 20 pixels down.
I cant Imagin why, I guess the problem is I adding a ViewController as Subview to an other ViewController and calling there the methode. But I cant imagin how to solve the issue.
I had similar problem but parent view was moved 20 px up.
Solution was based on changing "Wants full screen". I had to set this values to YES but not to all child view controllers.

clickedButtonAtIndex: not working

Hi guys i have this i IBAction linked to a button:
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
}
and a clickedButtonIndex to auto run but somehow it doesn't load SecondViewController:
#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
SecondViewController *sampleView = [[SecondController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
else{
// Cancel prompt
}
}
Am i missing something here?
If you don’t give the alert view some button titles, there won’t be any buttons to tap on and that delegate method won’t get called.
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
}
Your code doesn't show a button
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:**nil** otherButtonTitles:**nil**];
You pass nil as cancelButtonTitle AND nil as otherbuttonTitles, you should at least have one button title set.

otherButtonTitles in UIAlertView

I was just curious about how can i attach some different task to the otherButtonTitle of UIAlertView.
The cancel button automatically take you out of the application but if i want to attach a different task with the otherButtonTitle ,what should i do?
Thanks,
UIAlertView delegate "didDismissWithButtonIndex" get called every time u click any button.
Try this:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:messageString
delegate:self
cancelButtonTitle:#"Back"
otherButtonTitles:#"Reply",#"Delete",nil];
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
NSLog(#"Reply");
UIAlertView *myalert = [[UIAlertView alloc] initWithTitle:#"Button Clicked" message:#"U clicked Reply " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[myalert show];
[myalert release];
}
if (buttonIndex == 2)
{
NSLog(#"Delete");
UIAlertView *myalert = [[UIAlertView alloc] initWithTitle:#"Button Clicked" message:#"U clicked Delete " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[myalert show];
[myalert release];
}
}