UIAlertView Button Clicked Methods - objective-c

In my email composer I would like for the result of 'cancelbuttonclicked' to close the MFMailComposerViewController. Can I implement within the switch statement or do those need to be separate methods. Also, I would like for the send button to send the message before dismissing.
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultSent:{
UIAlertView *messageSent = [[UIAlertView alloc] initWithTitle:#"Message Sent" message:#"Your message has been sent" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[messageSent show];
break;
}
case MFMailComposeResultSaved:{
UIAlertView *messageComposeResultSaved = [[UIAlertView alloc] initWithTitle:#"Message Saved" message:#"Your message has been saved" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[messageComposeResultSaved show];
break;
}
case MFMailComposeResultCancelled:{
UIAlertView *messageComposeResultCancelled = [[UIAlertView alloc] initWithTitle:#"Message Cancelled" message:#"Your message has been cancelled" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[messageComposeResultCancelled show];
break;}
case MFMailComposeResultFailed:{
UIAlertView *messageFailed = [[UIAlertView alloc]initWithTitle:#"Message Failed" message:#"Your message could not be sent" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[messageFailed show];
break;
}
}
}

Your code should work fine. There are no limitations on using UIAlertView inside a switch. However, to make it a bit less messy, I'd suggest rewriting it like this:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
NSString *alertTitle = nil;
NSString *alertMessage = nil;
switch (result) {
case MFMailComposeResultSent:{
alertTitle = #"Message Sent";
alertMessage = #"Your message has been sent";
break;
}
case MFMailComposeResultSaved:{
alertTitle = #"Message Saved";
alertMessage = #"Your message has been saved";
break;
}
case MFMailComposeResultCancelled:{
alertTitle = #"Message Cancelled";
alertMessage = #"Your message has been cancelled";
break;}
case MFMailComposeResultFailed:{
alertTitle = #"Message Failed";
alertMessage = #"Your message could not be sent";
break;
}
}
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
It lets you avoid multiple initWithTitle....
But there is another concern: UIAlertView is deprecated is iOS 8. You should use UIAlertController instead. This answer has an example of UIAlertController usage.

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

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.

Not receiving email sent using MessageUI framework

I am using the MessageUI framework to send an email but I never receive that email when sent.
I am importing #import <MessageUI/MessageUI.h>
and then I have the following code
- (void)emailFile
{
if(![MFMailComposeViewController canSendMail]) {
UIAlertView *cantSend = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Device not configured to send email" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[cantSend show];
[cantSend release];
} else {
MFMailComposeViewController *mailView = [[[MFMailComposeViewController alloc] init] autorelease];
mailView.mailComposeDelegate = self;
[mailView setToRecipients:[NSArray arrayWithObject:#"matthew.inman#cdl.co.uk"]];
[mailView setSubject:#"Test"];
[mailView setMessageBody:#"This is a text message" isHTML:NO];
[self presentModalViewController:mailView animated:YES];
}
}
and
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if(error) {
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:#"Mail Error" message:[error localizedDescription] delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[errorView show];
[errorView release];
} else {
switch (result) {
case MFMailComposeResultSent:
NSLog(#"Sent Mail");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail Saved");
break;
case MFMailComposeResultCancelled:
NSLog(#"Mail Cancelled");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail Failed");
break;
default:
break;
}
}
[controller dismissModalViewControllerAnimated:YES];
}
I get the message "Sent Mail" in the console but I like I said I never receive the email that I am sending.
I have gone through the apple documentation and can't find anything that helps can anyone else help me please. I'm I doing something wrong?
Make sure you are testing on a device, email will not be sent via the simulator.

Objective-c formatting style causes an error in a switch-case

I'm getting an error in my switch statement with some multi-line Objective-c code:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultFailed:
// NSLog(#"Mail Failed");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
default:
break;
}
}
If I uncomment the line with the NSLog, it works fine. What's causing this error? Is there any way to use this kind of formatting?
You should not declare a variable in a switch case unless you introduce a scope.
case MFMailComposeResultFailed: { // <--
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
} // <--
The actual error is because in the C standard (§6.8.1), a label can only be followed by a statement (NSLog(#"Mail Failed")), not a declaration (UIAlertView* alert = ...).
The issues is with how switch is defined. You can't have a variable declaration on the line following the case. You can fix it by wrapping the entire case in a new scope
case MFMailComposeResultFailed:
{
// NSLog(#"Mail Failed");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", #"OK")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}

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