我在同一个视图控制器下的一个视图上设置了两个UIPickerViews
。选择器似乎按预期填充了值。我也有每个选择器的标签。标签随每个值更改而更新,但标签上显示的值不正确。总是在10点之间。代码如下。
接口:
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
@interface TransactionsViewController : UIViewController {
//Deposit
NSMutableArray *depositArray;
IBOutlet UIPickerView *depositPicker;
IBOutlet UILabel *depositLabel;
//Send
NSMutableArray *sendArray;
IBOutlet UIPickerView *sendPicker;
IBOutlet UILabel *sendLabel;
}
@property (nonatomic, retain) NSMutableArray *depositArray;
@property (nonatomic, retain) IBOutlet UIPickerView *depositPicker;
@property (nonatomic, retain) NSMutableArray *sendArray;
@property (nonatomic, retain) IBOutlet UIPickerView *sendPicker;
@property (nonatomic, retain) IBOutlet UILabel *depositLabel;
@property (nonatomic, retain) IBOutlet UILabel *sendLabel;
@end
实现:
#import "TransactionsViewController.h"
@implementation TransactionsViewController
@synthesize depositArray;
@synthesize depositPicker;
@synthesize sendArray;
@synthesize sendPicker;
@synthesize depositLabel;
@synthesize sendLabel;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *dollarsArray = [[NSMutableArray alloc] init];
for (int i = 5; i <= 100; i+=5)
{
NSString *item = [[NSString alloc] initWithFormat:@"%i", i];
[dollarsArray addObject:item];
[item release];
}
self.depositArray = dollarsArray;
self.sendArray = dollarsArray;
[dollarsArray release];
}
// Pickers
#pragma mark - Pickers
#pragma mark Pickers Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(pickerView == depositPicker) {
switch(component) {
case 0:
return 1;
break;
case 1:
return [depositArray count];
break;
default:
break;
}
}
else if(pickerView == sendPicker) {
switch(component) {
case 0:
return 1;
break;
case 1:
return [sendArray count];
break;
default:
break;
}
}
return 0;
}
-(CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component {
switch (component) {
case 0:
return 50;
break;
case 1:
return 100;
break;
default:
break;
}
return 0;
}
#pragma mark Pickers Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(pickerView == depositPicker) {
switch(component) {
case 0:
return @"$";
break;
case 1:
depositLabel.text=[depositArray objectAtIndex:row];
return [depositArray objectAtIndex:row];
[depositLabel release];
break;
default:
break;
}
}
else if(pickerView == sendPicker) {
switch(component) {
case 0:
return @"$";
break;
case 1:
return [sendArray objectAtIndex:row];
break;
default:
break;
}
}
return 0;
}
//Pickers End
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[depositArray release];
[send release];
[super dealloc];
}
@end
尚未设置诸如sendLabel之类的所有内容。如您所见,depositLabel会自动更新所选行,但值不会更新为正确的值。我不确定这是我的标签或选择器本身的实现问题。
我很欣赏一些投入。感谢。
答案 0 :(得分:1)
titleForRow方法就是你应该返回行的文本的地方。在你的titleForRow中,在那里设置标签文本是没有意义的(我认为你也不应该发布它)。 titleForRow末尾的“return 0”也可能导致问题。
要处理选择器中的实际选择,请使用didSelectRow方法。