【问题标题】:Multiple download buttons多个下载按钮
【发布时间】:2013-03-14 23:52:16
【问题描述】:

我正在创建一个应用程序,一旦导航到页面并选择了音调,用户就可以从各个页面下载声音文件并将其通过电子邮件发送给自己。

我想知道是否有人知道如何连接每个页面上的下载按钮并将其连接到邮件编辑器。我已经在我的联系页面上找到了邮件编写器,但是当我尝试将邮件编写器连接到每个页面上的下载按钮时没有任何反应,我尝试使用标签,但仍然丢失任何帮助将不胜感激。在此先感谢

【问题讨论】:

    标签: objective-c ios6 xcode4.3


    【解决方案1】:

    我假设下载按钮是您的按钮。而且我假设每个“页面”都是不同的视图控制器?

    如果您想真正高效,您可以创建一个单独的类来保存您的邮件编写器逻辑。

    为了快速修复,您可以将按钮连接到视图控制器中的 IBAction,然后将邮件编写器逻辑放在那里或从那里调用它的方法。

    这是一个简单的例子:

    - (IBAction)sendProblemReport:(UIButton*)sender {  //note: connect your button here 
    
        UIButton *button;
        if ([sender isKindOfClass:[UIButton class]]) {
           button = sender;
        }
        else {
           return;  //leave if not a button
        }
    
           //note: if you do it this way be sure to set tag values in interface builder on your button.  The other way is to have an action for each button.
    
           switch (button.tag) {
           case 1:
             NSURL  *fileURL1 = [[NSURL alloc] initFileURLWithPath:@"YOUR FILE PATH HERE"];
             NSData *soundfile = [[NSData alloc] initWithContentsOfURL:fileURL1];
             NSString *fileTitle = @"YOUR Nice File Name";  //sound.mp3
             [self showDiagMailSheetAttachingSoundFile:soundFile usingFileName:filename];
            break; 
            }
     }
    
    - (void)showDiagMailSheetAttachingSoundFile:(NSData*)soundFile usingFileName:(NSString*)fileName {
    
        MFMailComposeViewController *diagMail = [[MFMailComposeViewController alloc]init];
        [diagMail setMailComposeDelegate:self];
    
            if ([MFMailComposeViewController canSendMail]) {
            [diagMail setToRecipients:@[@"fromaddress@gmail.com"]];
            [diagMail setCcRecipients:nil];
            [diagMail setBccRecipients:nil];
            [diagMail setSubject:@"subject message"];        
            [diagMail setMessageBody:nil isHTML:NO];
    
            [diagMail addAttachmentData:soundFile file mimeType:@"audio/mpeg" fileName:fileName];  //note: not sure of the mime type you need.
    
             diagMail.modalPresentationStyle = UIModalPresentationCurrentContext;
             [self presentViewController:diagMail animated:YES completion:nil];
         }
      }
      - (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    
        // Notifies users about errors associated with the interface
        switch (result)
        {
            case MFMailComposeResultCancelled:
            // @"Result: Mail sending canceled";
            break;
        case MFMailComposeResultSaved:
            // @"Result: Mail saved";
            break;
        case MFMailComposeResultSent:
            // @"Result: Mail sent";
            break;
        case MFMailComposeResultFailed:
            // @"Result: Mail sending failed";
            break;
        default:
            // @"Result: Mail not sent";
            break;
        }
        [self dismissViewControllerAnimated:YES completion:^{
          [self cancel:self];
        }];
     }
    

    注意:要将它放在一个单独的子类中,您可以使用该类简单地返回一个“MFMailComposeViewController”,然后将它呈现在每个视图控制器中。这种方法效率更高,并且允许您在多个位置调用相同的代码。

    好的,希望对您有所帮助。如果不告诉我。

    【讨论】:

    • 这样我还可以通过“下载”按钮将每个声音文件附加到每个页面,这样当点击它时,邮件作曲家会提供附加的声音文件
    • 是的,应该。我将编辑答案并包含附件代码。
    • 当您说连接按钮时,您的意思是通过 cntrl 拖动来连接它,因为如果是这样,它将无法连接,您是否有我可以联系您的电子邮件,因为这样会容易得多,如果不是没问题
    • 是的,ctrl 从您的视图控制器中的按钮拖入您的 .m 子类文件。当对话框弹出时确保它说动作(不是出口)。现在,当您按下按钮时,它将执行该方法中的代码 - 实际上,按下按钮并弹出邮件编辑器。希望有帮助
    • 我尝试从我的视图控制器中进行 ctrl 拖动,但没有出现任何窗口,它只是拖过来,没有任何反应,我做错了什么
    猜你喜欢
    • 2017-07-10
    • 2017-12-23
    • 2015-01-12
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多