UIBarButtonItemをタップしてUIPickerViewを閉じようとしています。そこで、アクションを作成し、そのアクション内で[PickerViewremoveFromSuperview]を呼び出します。しかし、それは機能していません。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController< UIPickerViewDelegate,UIPickerViewDataSource>
- (IBAction)dissmissPicker:(id)sender;
@property (nonatomic,strong) NSArray *ColorName;
@end
ViewController.m:
#import "ViewController.h"
@implementation ViewController
@synthesize ColorName;
// 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 [ColorName count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [ColorName objectAtIndex:row];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ColorName = [[NSArray alloc]initWithObjects:@"Red",@"White",@"Yellow",@"Green",@"Blue",@"Black",@"Brown",@"Cyan",nil];
// [pickerview removeFromSuperview];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
UIColor *newColor;
switch (row) {
case 0:
newColor = [UIColor redColor];
break;
case 1:
newColor = [UIColor whiteColor];
break;
case 2:
newColor = [UIColor yellowColor];
break;
case 3:
newColor = [UIColor greenColor];
break;
case 4:
newColor = [UIColor blueColor];
break;
case 5:
newColor = [UIColor blackColor];
break;
case 6:
newColor = [UIColor brownColor];
break;
case 7:
newColor =[UIColor cyanColor];
break;
default:
newColor = [UIColor redColor];
break;
}
self.view.backgroundColor = newColor;
}
- (void)viewDidUnload
{
// [self setShowColor:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)dissmissPicker:(id)sender {
[pickerView removeFromSuperview];
}