iOS : How to attach a multiple attachment file in one button using pickerview method?

情到浓时终转凉″ 提交于 2019-12-25 08:13:15

问题


I have a multiple file to attach inside the picker view. When user select that picker view item, they can click email button to attach the chosen file. How do I do so in my picker view?

Here is my sample code.

M File :

-(void)pickerViewEmail:(UIPickerView *)pickerViewEmail didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{


    if ([[musicList objectAtIndex:row] isEqual:@"m1"])
    {

        MFMailComposeViewController *pickerEmail = [[MFMailComposeViewController alloc] init];
        pickerEmail.mailComposeDelegate = self;

        NSString *path = [[NSBundle mainBundle] pathForResource:@"m1" ofType:@"mp3"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        [pickerEmail addAttachmentData:myData mimeType:@"audio/mp3" fileName:@"m1"];

        [pickerEmail setSubject:@"Hello!"];

        // Set up recipients
        NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"]; 
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 
        NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

        [pickerEmail setToRecipients:toRecipients];
        [pickerEmail setCcRecipients:ccRecipients]; 
        [pickerEmail setBccRecipients:bccRecipients];

        // Fill out the email body text
        NSString *emailBody = @"Hello";
        [pickerEmail setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:pickerEmail animated:YES];
        [pickerEmail release];

    }

Email Button: How do i start from here.

-(IBAction)showEmail
{

    if ([MFMailComposeViewController canSendMail])
    {
                 [self pickerEmail]; I have a yellow error when i call this. What is the right solution?

    }

    else
    {

    }


}

回答1:


When user selects rows in your pickerviews, you save row titles to some common variables using

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

You can use one pickerView delegate method for all your pickerViews. To determine, which pickerView is selected you should retrieve sender.

Then in your showEmail method you just use that saved variables.

Sample code. Just bind 3 different delegates to 3 sliders in IB:

-(IBAction)slider1Changed:(id)sender {
    UISlider *slider = (UISlider *) sender;
    int progressAsInt =(int)(slider.value + 0.5f);
    NSString *newText =[[NSString alloc]
                        initWithFormat:@"%d",progressAsInt];
    label1.text = newText;
    NSString *imgFileName = [NSString stringWithFormat:@"gold%i.png", progressAsInt];
    image1.image = [UIImage imageNamed:imgFileName];

    [newText release];
}

-(IBAction)slider2Changed:(id)sender {
    UISlider *slider = (UISlider *) sender;
    int progressAsInt =(int)(slider.value + 0.5f);
    NSString *newText =[[NSString alloc]
                        initWithFormat:@"%d",progressAsInt];
    label2.text = newText;
    NSString *imgFileName = [NSString stringWithFormat:@"gold%i.png", progressAsInt];
    image2.image = [UIImage imageNamed:imgFileName];
    [newText release];
}

-(IBAction)slider3Changed:(id)sender {
    UISlider *slider = (UISlider *) sender;
    int progressAsInt =(int)(slider.value + 0.5f);
    NSString *newText =[[NSString alloc]
                        initWithFormat:@"%d",progressAsInt];
    label3.text = newText;
    NSString *imgFileName = [NSString stringWithFormat:@"gold%i.png", progressAsInt];
    image3.image = [UIImage imageNamed:imgFileName];
    [newText release];
}


来源:https://stackoverflow.com/questions/8755403/ios-how-to-attach-a-multiple-attachment-file-in-one-button-using-pickerview-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!