好吧,简短的回答是买一本书,因为我不知道你对 Objective C 了解多少:D
但这似乎是一个直截了当的问题,我可以就一种解决方法提供一些一般性的想法。
我将使用纯编码而不是 Interface Builder,但我建议使用 Interface Builder,因为它将接口设置与实际代码逻辑分开。
此代码假定使用自动引用计数 (ARC)
ViewController.h 文件
#import <UIKit/UIKit.h>
// --------------------------------------------------------------------------------
// The UIPickerViewDelegate is a protocol (kinda of like an abstract class in C++)
// you need to implement the methods (or functions in C++ terms) of the protocol
//
// The UIPickerViewDataSource also a protocol to tell your ViewController to expect
// a method that provides your UIPickerViews the data it needs to display.
// --------------------------------------------------------------------------------
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
// UIPickerView can have multiple components (multiple roller thing)
UIPickerView *myPicker;
// arrays to hold your data
NSArray *list1;
NSArray *list2;
// our button to do something when pressed
UIButton *btnCalculate;
}
@end
ViewController.m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
myPicker.showsSelectionIndicator = YES;
// ------------------------------------------------------------------
// these two lines are compulsory, it is saying the picker view's
// delegate and datasource is this view controller itself
// ------------------------------------------------------------------
myPicker.delegate = self;
myPicker.dataSource = self;
// ------------------------------------------------------------------
// Here we're specifying what we want to show in each row of the
// picker view. Each of these array will be loaded into each of
// the picker view components (see below)
// ------------------------------------------------------------------
// -------------------------------------
// | apple | option 1 |
// | banana | option 2 |
// | orange | option 3 |
// | mango | |
// | peach | |
// -------------------------------------
list1 = [[NSArray alloc] initWithObjects:@"apple", @"banana", @"orange", @"mango", @"peach", nil];
list2 = [[NSArray alloc] initWithObjects:@"option1", @"option2", @"option3", nil];
btnCalculate = [[UIButton alloc] initWithFrame:CGRectMake(120, 300, 100, 50)];
btnCalculate.backgroundColor = [UIColor blackColor];
[btnCalculate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnCalculate setTitle:@"Calculate" forState:UIControlStateNormal];
// this is how we add a callback method for the button tap event
[btnCalculate addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myPicker];
[self.view addSubview:btnCalculate];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// ------------------------------------------------------------
// Your UIPickerViewDataSource methods you need to implement
// ------------------------------------------------------------
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// we're telling the our picker we want 2 rollers in a single picker view
// like this:
// -------------------------------------
// | | |
// | | |
// | component 1 | component 2 |
// | | |
// | | |
// -------------------------------------
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
// -------------------------------------------------------------------------
// this is telling the picker how many rows to expect for each picker
// it must match up with our data source (which in this case is our
// list1 and list2).
// -------------------------------------------------------------------------
// -------------------------------------
// | | |
// | | |
// | 5 fruits | 3 options |
// | | |
// | | |
// -------------------------------------
if(component == 0)
{
return list1.count;
}
else
{
return list2.count;
}
}
// ------------------------------------------------------------
// Your UIPickerViewDelegate methods you need to implement
// ------------------------------------------------------------
// ------------------------------------------------------------
// This method will return the actual text into the picker view
// components.
//
// Note: This is for basic UIPickerView, if you want more fancy
// looking rows, you need to implement the other delegate method
// instead of this one.
// ------------------------------------------------------------
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
return [list1 objectAtIndex:row];
}
else
{
return [list2 objectAtIndex:row];
}
}
// ------------------------------------------------------------
// Your callback method for when user taps on your button
// ------------------------------------------------------------
-(void)buttonTapped:(id)sender
{
int fruitRow = [myPicker selectedRowInComponent:0];
int optionRow = [myPicker selectedRowInComponent:1];
// This will print the result into Xcode's console window
NSLog(@"Fruit chosen: %@", [list1 objectAtIndex:fruitRow]);
NSLog(@"Option chosen: %@", [list2 objectAtIndex:optionRow]);
// This will show a popup alert view with same information
NSString *allInformation = [[NSString alloc] initWithFormat:@"%@\n%@",
[list1 objectAtIndex:fruitRow], [list2 objectAtIndex:optionRow]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information"
message:allInformation
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
@end
最后结果
你应该得到这样的结果:
从那里你可以修改方法-(void)buttonTapped:(id)sender
来做任何你喜欢的事情。
希望有帮助。