iOS中

2012-05-31 51 views
2

辭退UIPickerView我想通過敲擊的UIBarButtonItem駁回UIPickerView。所以,我做了一個動作,並在該動作中調用[PickerView removeFromSuperview]。但是,它不工作。iOS中

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

} 
+0

是pickerView有效伊娃出口出口?只是認爲你可能會誤入思想pickerView是由於你使用的委託方法和它明確提到的地方而自動生成的。 – pbx

+0

其實當我使用pickerview時,它會顯示一個錯誤。我也嘗試過[(UIPickerView *)pickerView)]。但是,沒有任何工作。 – Adnan

回答

1

我假設你從使用界面生成器的評論?

如果是這樣的話,你需要做一個出口,它在IB連接。

- 創建您的.h文件中

IBOutlet UIPickerView *picker; 

- 連接界面生成器

-Dismiss認爲

[picker removeFromSuperview]; 
+0

謝謝。有用:-) – Adnan

0

如何你PickerView添加到視圖?它是模態顯示,添加爲子視圖等?

在前者的情況下,你會打電話-[dismissModalViewControllerAnimated:]在調用該選擇器或從選擇器內自身的類。

在後一種情況下,你的-[removeFromSuperview]方法應該能正常運行。

如果有人使用navigationController推,你應該能夠只是調用 - 這navigationController的[popViewControllerAnimated:]方法。

+0

我從接口設計對象列表中添加pickerview。 – Adnan

0

創建一個新的伊娃,合成它和內

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

例如分配給它。然後你可以在你的IBAction中使用它,Xcode不會繼續說「未知」之類的東西。

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

這不是工作:-) – Adnan

+0

它只有當你委託或視圖的數據源(不知道上面這種方法屬於此刻)連接到您的控制器對象的工作。 – pbx