在Xcode 4.5.2&amp ;;下编译时,UIPickerView无法自动选择最后一行。 iOS 6 [英] UIPickerView can't autoselect last row when compiled under Xcode 4.5.2 & iOS 6

查看:142
本文介绍了在Xcode 4.5.2&amp ;;下编译时,UIPickerView无法自动选择最后一行。 iOS 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图诊断我的应用无法使用在旧版XCode下成功运行的代码自动预选UIPickerView的最后一行。我认为这是Xcode而不是iOS 6中的一个错误,因为我在iOS 6设备上运行的旧应用程序运行正常,但重新编译Xcode 4.5.2下的代码行为不正常。我已经整理了以下示例来演示问题,但在我提交错误报告之前,我希望此论坛上的其他人能够确定问题是否与我的代码有关,或者确实是Xcode / iOS中的错误。



我创建了一个单一的视图应用程序,并使用导航控制器和两个IBOutlet设置故事板,一个到UILabel,我在那里显示所选行,一个到UIPickerView。 / p>

以下是我的自定义视图控制器的头文件:

 # import< UIKit / UIKit.h> 

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

这是我的自定义视图控制器的实现文件:

  #importDisposableViewController.h

const NSInteger MAX_VALUE = 10;

@interface DisposableViewController()

@property(弱,非原子)IBOutlet UIPickerView * pickerView;
@property(弱,非原子)IBOutlet UILabel * selectedValueLabel;

@end


@implementation DisposableViewController

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


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


// **
// ** UIPickerViewDataSource方法
// **

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


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)组件
{
返回MAX_VALUE + 1;
}


// **
// ** UIPickerViewDelegate方法
// **

- (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

这是显示问题的屏幕截图:





请注意,我的代码尝试在viewWillAppear:方法中自动选择UIPickerView中的最后一行,但是当程序运行时,值标签在调用[self]时将行作为10 .pickerView selectedRowInComponent:0],但是UIPickerView本身在视觉上看起来已经选择了9.我相信这是一个错误,但是想要我如何解决这个问题的其他意见或建议。感谢您的关注。

解决方案

此错误是由故事板中的Use Autolayout选项引起的,但我找到了一个解决方法允许我继续使用自动布局:



如果我在视图控制器的viewDidAppear中调用[pickerView selectRow:inComponent:]方法而不是在viewWillAppear:中,然后UIPickerView正确调整以选择正确的行,尽管当选择器从错误的选择旋转到正确的选择时,选择暂时可见。


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:

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.

解决方案

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.

这篇关于在Xcode 4.5.2&amp ;;下编译时,UIPickerView无法自动选择最后一行。 iOS 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆