Tuesday, April 12, 2011

How to retrieve data from modal view

I have CardFormViewController and PickerViewController. CardFormViewController needs to ask the user for credit card type, such as "Visa" and "MasterCard". In order to ask the user I want to use a UIPickerView. However, the UIPickerView is TOO big to stick on a form with other elements.

The solution I use is to stick the UIPickerView onto it's own View (hence PickerViewController) and open it up modally via
    pickerView = [[PickerViewController alloc] autorelease];
    pickerView.mode = CardType;
    pickerView.view.backgroundColor = [UIColor clearColor];
    pickerView.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    [self presentModalViewController:pickerView animated:YES];
I'm sure you have already done this considering you googled how to retrieve the data from it. So here's how to retrieve the data:

1. In CardFormViewController create a property, such as a UILabel. (don't forget to synthesize, release, link in IB :))

2. Make sure you have PickerViewController set as a UIPickerViewDelegate like @interface PickerViewController : UIViewController <UIPickerViewDelegate>. Also make sure you have implemented the required protocol (see UIPickerView.h or documentation on Apple's site for that if you need help)

3. Implement the UIPickerViewDelegate method - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component





{
//IF YOU ARENT using NavigationController as your Root Controller, uncomment this code and comment out the other code
/*
    CardFormViewController *vc = (CardFormViewController*)self.parentViewController;
    vc.lblTest.text = [cardTypeArray objectAtIndex:row];



*/
//IF YOU ARE using NavigationController as your Root Controller, use code below
    UINavigationController *nc = (UINavigationController*)self.parentViewController;
   
   CardFormViewController *vc = (CardFormViewController*)[nc.viewControllers objectAtIndex:(nc.viewControllers.count - 1)];
    vc.lblTest.text = [cardTypeArray objectAtIndex:row];

}

No comments:

Post a Comment

There was an error in this gadget