Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

xcode4.5 - UIPickerView can't autoselect last row when compiled under Xcode 4.5.2 & iOS 6

I have been beating my head against the wall trying to diagnose my app's inability to automatically preselect the last row of a UIPickerView using code that successfully worked under older versions of XCode. I think this is a bug in Xcode rather than iOS 6 because my old app running on an iOS 6 device works properly, but recompiling the code under Xcode 4.5.2 does not behave properly. I have put together the following sample to demonstrate the problem, but before I submit a bug report, I would like opinions from others on this forum to determine if the problem is with my code or is indeed a bug in Xcode/iOS.

I created a single view app and setup the storyboard with a navigation controller, and two IBOutlets, one to a UILabel where I display the selected row, and one to a UIPickerView.

Here is the header file for my custom view controller:

#import <UIKit/UIKit.h>

@interface DisposableViewController : UIViewController
    <UIPickerViewDataSource, UIPickerViewDelegate>
@end

Here is the implementation file for my custom view controller:

#import "DisposableViewController.h"

const NSInteger MAX_VALUE = 10;

@interface DisposableViewController ()

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *selectedValueLabel;

@end


@implementation DisposableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self.pickerView reloadAllComponents];
    [self.pickerView selectRow:MAX_VALUE inComponent:0 animated:NO];
    self.selectedValueLabel.text = [NSString stringWithFormat:@"%d", [self.pickerView selectedRowInComponent:0]];
}


//**
//** UIPickerViewDataSource methods
//**

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return MAX_VALUE + 1;
}


//**
//** UIPickerViewDelegate methods
//**

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%d", row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.selectedValueLabel.text = [NSString stringWithFormat:@"%d", row];
}


@end

Here is a screenshot demonstrating the problem:

Screenshot

Note that my code tries to autoselect the last row in the UIPickerView in the viewWillAppear: method, but when the program runs, the value label gets the row as 10 when calling [self.pickerView selectedRowInComponent:0], but the UIPickerView itself visually appears to have selected 9. I believe this is a bug, but would like other opinions or suggestions for how I can resolve this problem. Thanks for your attention.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This bug is caused by the Use Autolayout option in the storyboard, but I have found a workaround that allows me to continue using auto layout:

If I call the [pickerView selectRow:inComponent:] method in the viewDidAppear: of my view controller instead of in the viewWillAppear:, then the UIPickerView correctly adjusts to select the correct row though the selection is momentarily visible as the picker rotates from the wrong selection to the correct one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...