Adnan
Adnan

Reputation: 8729

Dismiss UIPickerView in iOS

I'm trying to dismiss the UIPickerView by tapping UIBarButtonItem. So, I made an action and inside that action I call [PickerView removeFromSuperview]. But, It's not working.

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];

}

Upvotes: 2

Views: 3810

Answers (3)

Aaron Wojnowski
Aaron Wojnowski

Reputation: 6480

How is your PickerView added to the view? Is it presented modally, added as a subview, etc?

In the case of the former, you would call -[dismissModalViewControllerAnimated:] in the class that called that picker or from within the picker its self.

In the case of the latter, your -[removeFromSuperview] method should work fine.

If it was pushed using a navigationController, you should be able to just call the -[popViewControllerAnimated:] method of that navigationController.

Upvotes: 0

Dcritelli
Dcritelli

Reputation: 820

I'm assuming from the comments you're using interface builder?

If that's the case, you need to make an outlet and connect it in IB.

-Create the outlet in your .h file

IBOutlet UIPickerView *picker;

-Connect the outlet in interface builder

-Dismiss the view

[picker removeFromSuperview];

Upvotes: 2

pbx
pbx

Reputation: 1137

Make a new iVar, synthesize it and assign it within the

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    self.yourPickerViewiVar = pickerView;
    return 1;
}

for example. Then you can use it within your IBAction and Xcode won't continue saying something like "Unknown".

- (IBAction)dissmissPicker:(id)sender {
    [self.yourPickerViewiVar removeFromSuperview];
}

Upvotes: 0

Related Questions