Retrieving row index from UIPickerView and using it in a if statement

假如想象 提交于 2019-12-12 02:38:10

问题


Again forgive me if im not being clear, I have only started iOS dev yesterday.

So I have a application that is going to send information to specific email address. I have included a pickerview and populated it with an array of information, 5 or 6 different categories. What i want to do is to be able to change the recipient's of the email based on what category is selected in the pickerview.

So far I have but selectedRowInComponent doesn't seem to work.

- (IBAction)sendFinalItem:(UIButton *)sender {


NSLog(@"send button pressed");


if ([self.pickerView selectedRowInComponent:(0)])
{

    MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc] init];
    [mailcontroller setMailComposeDelegate:self]; 
    NSString *email =@"k_scully@hotmail.co.uk";

    NSArray *emailArray = [[NSArray alloc] initWithObjects:email, nil]; 
    [mailcontroller  setToRecipients:emailArray]; 
    [mailcontroller setSubject:@"[Urgent]Potential Job, iPhone snapped"];

    [self presentViewController:mailcontroller animated:YES completion:nil];
    [mailcontroller setMessageBody:notesTextView.text isHTML:NO];
}

回答1:


selectedRowInComponent: returns the index of the selection made by the user. You can use that value to check which e-mail address you need to use.

NSInteger selectedRow  = [self.pickerView selectedRowInComponent:0];
if (selectedRow == 0)
{
    // E-mail person 1
}
else if (selectedRow == 1)
{
    // E-mail person 2
}
// etc.


来源:https://stackoverflow.com/questions/19322719/retrieving-row-index-from-uipickerview-and-using-it-in-a-if-statement

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