0
我有一個viewcontroller與UIPickerView應該顯示圖像取決於選擇。我的選擇器視圖可以正常工作,並且圖像顯示正常,但會在首次選擇後凍結。ViewController for UIPickerView凍結後選擇
有什麼我失蹤了嗎?這是我第一次使用選取器視圖。
感謝您的幫助!
.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
哦!就是這樣!我感到很傻。非常感謝! – Seslyn