UIPickerView selectRow inComponent无法正常工作

时间:2014-03-29 10:59:53

标签: ios uipickerview

我一直在研究多组件选择器,我想设置各种组件的默认值。代码如下所示。

#import "MathTablesSelectVC.h"

@interface MathTablesSelectVC ()

@property (weak, nonatomic) IBOutlet UIPickerView *problemDifficultyTypeTable;

@property NSArray *problemDifficulties;
@property NSArray *problemTypes;
@property NSArray *problemTables;

@end

@implementation MathTablesSelectVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"0");
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    NSLog(@"1");
    [super viewDidLoad];

    _problemDifficulties = @[@"Practice", @"Slow", @"Normal", @"Fast"];
    _problemTypes = @[@"Addition", @"Subtraction", @"Multiplication", @"Division", @"Remainders"];
    _problemTables = @[@" 0's", @" 1's", @" 2's", @" 3's", @" 4's", @" 5's", @" 6's", @" 7's", @" 8's", @" 9's", @"10's", @"11's", @"12's", @"All"];
}

-(void)viewWillAppear:(BOOL)animated {
    NSLog(@"2");
    [super viewWillAppear:animated];
    [_problemDifficultyTypeTable selectRow:1 inComponent:0 animated:NO];
    [_problemDifficultyTypeTable selectRow:0 inComponent:1 animated:NO];
    [_problemDifficultyTypeTable selectRow:1 inComponent:2 animated:NO];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    NSLog(@"3");

    return 3;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component     {
    NSLog(@"4");
    if(component == 0) {
        return 150.0;
    } else if(component == 1) {
        return 300.0;
    } else {
        return 75.0;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    NSLog(@"5");
    return 60.0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSLog(@"6");
    if(component == 0) {
        return [_problemDifficulties count];
    } else if(component == 1) {
        return [_problemTypes count];
    } else {
        return [_problemTables count];
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSLog(@"7");
    if(component == 0) {
        return [_problemDifficulties objectAtIndex:row];
    } else if(component == 1) {
        return [_problemTypes objectAtIndex:row];
    } else {
        return [_problemTables objectAtIndex:row];
    }
}

-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"8");
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"9");
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"10");
}

@end

现在这一切都很好地工作了一段时间然后它停止了工作:(

1 个答案:

答案 0 :(得分:0)

然后我花了几个小时来解决我做错了什么。

当我做一些重构时,我把答案填满了。我设法杀死了故事板和IBOutlet之间的联系。现在这个漂亮的小,非常小的小圆圈仍然在@property旁边,它不再有一个点。一旦我重新建立连接,事情就开始再次发生。

你还会注意到一大堆NSLog,这似乎是第一次调用的顺序。当我们遍历组件时,我们会得到一些重复的数字。

我打破的另一件事虽然我直接选择了代表团连接。