问题
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