我有一个带UIPickerView的viewcontroller应该根据选择显示图像。我的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
答案 0 :(得分:0)
问题可能是您的streakView掩盖了选择器,因此您无法再进行选择。给它一个背景颜色,看看是不是真的。