UIPickerView 的 ViewController 在选择后冻结

Posted

技术标签:

【中文标题】UIPickerView 的 ViewController 在选择后冻结【英文标题】:ViewController for UIPickerView freezes after selection 【发布时间】:2013-11-03 00:53:11 【问题描述】:

我有一个带有 UIPickerView 的视图控制器,它应该根据选择显示图像。我的pickerview工作正常,图像看起来很好,但在第一次选择后它就冻结了。

我有什么遗漏吗?这是我第一次使用选择器视图。

感谢您的帮助!

.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) 
    // Custom initialization

return self;


- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view.

self.array  = [[NSArray alloc] initWithObjects:@"Assault",@"Support",@"Specialist", nil];

self.view.backgroundColor = [UIColor colorWithRed:.16545 green:.16545 blue:.16545 alpha:1];




- (void)didReceiveMemoryWarning

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.


// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

return 1;


// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:       (NSInteger)component
return [self.array count];


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component

return [self.array objectAtIndex:row];


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

UIView *streakView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[self.view addSubview:streakView];

NSLog(@"Selected Row %d", row);
switch(row)


    case 0: 
        UIButton *button1 = [UIButton buttonWithType: UIButtonTypeCustom];
        //[button1 addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
        UIImage *btnImage = [UIImage imageNamed:@"1.png"];
        [button1 setImage:btnImage forState:UIControlStateNormal];
        button1.frame = CGRectMake(83, 275, 75, 75);
        [streakView addSubview:button1];

        UIButton *button2 = [UIButton buttonWithType: UIButtonTypeCustom];
        //[button1 addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
        UIImage *btnImage2 = [UIImage imageNamed:@"2.png"];
        [button2 setImage:btnImage2 forState:UIControlStateNormal];
        button2.frame = CGRectMake(163, 275, 75, 75);
        [streakView addSubview:button2];


        UIButton *button3 = [UIButton buttonWithType: UIButtonTypeCustom];
        //[button1 addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
        UIImage *btnImage3 = [UIImage imageNamed:@"2.png"];
        [button3 setImage:btnImage3 forState:UIControlStateNormal];
        button3.frame = CGRectMake(243, 275, 75, 75);
        [streakView addSubview:button3];

        break;
    

    case 1:
        self.package.text = @"Green #00FF00";
        self.package.textColor = [UIColor colorWithRed:0.0f/255.0f green: 255.0f/255.0f blue:0.0f/255.0f alpha:255.0f/255.0f];
        break;
    case 2:
        self.package.text = @"Orange #FF681F";
        self.package.textColor = [UIColor colorWithRed:205.0f/255.0f green:   140.0f/255.0f blue:31.0f/255.0f alpha:255.0f/255.0f];
        break;



@end

【问题讨论】:

【参考方案1】:

问题可能是您的 streakView 覆盖了选择器,因此您无法再进行选择。给它一个背景色,看看是不是真的。

【讨论】:

哦!就是这样!我觉得好傻。非常感谢!

以上是关于UIPickerView 的 ViewController 在选择后冻结的主要内容,如果未能解决你的问题,请参考以下文章

为啥 UIPickerView 是半透明的

如何从另一个 UIPickerview 更改 UIPickerView 的字符串?

UIPickerView 选择和隐藏

如果 UIPickerView 未触及,则 UIPickerView 选择第 0 行

点击 uipickerview 时键盘不退出

如何检测 UIPickerView 上的变化?