iOS:如何使用pickerview方法在一个按钮中附加多个附件文件?

时间:2012-01-06 08:52:52

标签: iphone ios xcode ios4 uipickerview

我有一个要在选择器视图中附加的多个文件。当用户选择该选择器视图项时,他们可以单击电子邮件按钮以附加所选文件。我如何在我的选择器视图中这样做?

这是我的示例代码。

M档案:

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

    }

电子邮件按钮:我如何从这里开始。

-(IBAction)showEmail
{

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

    }

    else
    {

    }


}

1 个答案:

答案 0 :(得分:1)

当用户在选择器视图中选择行时,会将行标题保存为一些常见变量 使用

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

您可以为所有pickerView使用一个pickerView委托方法。要确定选择了哪个pickerView,您应该检索发件人。

然后在showEmail方法中,您只需使用已保存的变量。

示例代码。只需将3个不同的代表绑定到IB中的3个滑块:

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