如何更改UIpickerView上的元素按下按钮或按另一个按钮

问题描述:

我有一个应用程序,在主视图中有3个按钮,2个按钮必须显示一个UIPickerView用于选择事件(其中之一)和时间选择(第二个)。如何更改UIpickerView上的元素按下按钮或按另一个按钮

UIPickerViewDelegate只让我实施一次的方法,那么,我该如何改变UIPickerView的内容呢。

这里是MI的.m代码:

#import "Home_ViewController.h" 

    @interface Home_ViewController() 

@end 

@implementation Home_ViewController 

@synthesize index,arrayHoras,arrayRutas,pickerView, rutaId, horas, botonHoras, botonRuta, hora; 

- (IBAction)selectRuta:(id)sender { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view addSubview:pickerView]; 
    [UIView commitAnimations]; 
    [pickerView setHidden:NO]; 
} 


- (IBAction)horasBoton:(id)sender { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view addSubview:pickerView]; 
    [UIView commitAnimations]; 
    [pickerView setHidden:NO]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    hora = 1; 
    arrayRutas = [[NSMutableArray alloc] init]; 
    arrayHoras = [[NSMutableArray alloc] initWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", nil]; 

    sqlite3 *turutaDB; 
    NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *databasePath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"turuta.db"]; 

    NSLog(@"path de la bbdd: %@", databasePath); 
    if(sqlite3_open([databasePath UTF8String], &turutaDB)==SQLITE_OK) { 
     NSLog(@"2 Base de datos creada y abierta con exito"); 
    } else { 
     NSLog(@"Ha fallado la apertura de la bbdd"); 
    } 

    sqlite3_stmt *sentenciaRutas; 
    NSString *querySQLrutas = @"SELECT id,nombre FROM rutas"; 

    if(sqlite3_prepare_v2(turutaDB, [querySQLrutas UTF8String], -1, &sentenciaRutas, NULL)==SQLITE_OK){ 
     NSLog(@"Consulta preparada ok"); 
    } else { 
     NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB)); 
    } 


    while (sqlite3_step(sentenciaRutas) == SQLITE_ROW) { 
     NSString *selectRutas = [[NSString alloc] initWithUTF8String: 
           (const char *) sqlite3_column_text(sentenciaRutas, 1)]; 

     [arrayRutas addObject:selectRutas]; 


    } 



    sqlite3_finalize(sentenciaRutas); 

    // [self.tabBarController.tabBar setHidden:YES]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 

    return 1; 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 

    if(!botonHoras.isTouchInside) { 
     return [arrayHoras count]; 
    } else { 
     return [arrayRutas count]; 
    } 

} 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    if(!botonHoras.isTouchInside) { 
     return [arrayHoras objectAtIndex:row]; 
    } else { 
     return [arrayRutas objectAtIndex:row]; 
    } 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    if (!botonHoras.isTouchInside) { 
     horas= [[arrayHoras objectAtIndex:row] intValue]; 
     botonHoras.titleLabel.text = [NSString stringWithFormat:@"%d Horas", horas]; 
     [self.pickerView removeFromSuperview]; 

    } else { 


     //GMSPolylineOptions *optionsLine = [GMSPolylineOptions options]; 
     GMSMutablePath *ruta = [GMSMutablePath path]; 


     sqlite3 *turutaDB; 
     NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *databasePath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"turuta.db"]; 

     NSLog(@"path de la bbdd: %@", databasePath); 
     if(sqlite3_open([databasePath UTF8String], &turutaDB)==SQLITE_OK) { 
      NSLog(@"2 Base de datos creada y abierta con exito"); 
     } else { 
      NSLog(@"Ha fallado la apertura de la bbdd"); 
     } 

     sqlite3_stmt *sentenciaId; 
     NSString *querySQLid = [NSString stringWithFormat: @"SELECT id FROM rutas WHERE nombre = '%@'", [arrayRutas objectAtIndex:row]]; 
     if(sqlite3_prepare_v2(turutaDB, [querySQLid UTF8String], -1, &sentenciaId, NULL)==SQLITE_OK){ 
      NSLog(@"Consulta preparada ok"); 
     } else { 
      NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB)); 
     } 


     while (sqlite3_step(sentenciaId) == SQLITE_ROW) { 
      NSString *id = [[NSString alloc] initWithUTF8String: 
         (const char *) sqlite3_column_text(sentenciaId, 0)]; 

      rutaId = [id intValue]; 
      NSLog(@"%d",rutaId); 


     } 
     sqlite3_finalize(sentenciaId); 

     sqlite3_stmt *sentenciaPolyLine; 
     NSString *querySQLpoliline =[NSString stringWithFormat: @"SELECT coordx,coordy FROM config_ruta WHERE id_ruta = %d", rutaId]; 
     if(sqlite3_prepare_v2(turutaDB, [querySQLpoliline UTF8String], -1, &sentenciaPolyLine, NULL)==SQLITE_OK){ 
      NSLog(@"Consulta preparada ok"); 
     } else { 
      NSLog(@"Consulta ha fallado al preparar: %s", sqlite3_errmsg(turutaDB)); 
     } 


     while (sqlite3_step(sentenciaPolyLine) == SQLITE_ROW) { 
      NSString *x = [[NSString alloc] initWithUTF8String: 
          (const char *) sqlite3_column_text(sentenciaPolyLine, 1)]; 

      NSString *y = [[NSString alloc] initWithUTF8String: 
          (const char *) sqlite3_column_text(sentenciaPolyLine, 0)]; 

      double coordx = [x doubleValue]; 
      double coordy = [y doubleValue]; 
      NSLog(@"%f | %f",coordy,coordx); 


      [ruta addCoordinate:CLLocationCoordinate2DMake(coordy, coordx)]; 

     } 



    sqlite3_finalize(sentenciaPolyLine); 


     botonRuta.titleLabel.text = [arrayRutas objectAtIndex:row]; 

     [self.pickerView removeFromSuperview]; 
     [pickerView selectRow:0 inComponent:0 animated:YES]; 
    } 

} 




- (IBAction)rutaboton:(id)sender { 

} 

一些帮助,请...

您需要设置一个标记,指示哪个按钮被按下。然后在每个选择器视图数据源和委托方法中检查该标志。

int属性添加到您的班级。在selectRuta:方法中将属性设置为1。在horasBoton:方法中,将属性设置为2

然后你选择器视图的方法看起来像这样(假定你的财产被评为buttonType):

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    if(self.buttonType == 1) { 
     return [arrayRutas count]; 
    } else { 
     return [arrayHoras count]; 
    } 
} 
+0

谢谢你,这样的作品,但现在我还有一个问题,如果我把第一个按钮,当我选择一行,然后按另一个按钮,pickerView显示第一个按钮的数据。 如果我先按第二个按钮,然后显示数据表单第二个按钮,选择一行,然后按另一个按钮,pickerView会显示我第一次按下的按钮中的数据。 当我选择一行并隐藏pickerView时,我该如何删除所有数据? – leirbaggmj 2013-05-02 18:07:47

+0

您应该在两个按钮方法中重新加载选择器视图(设置'buttonType'属性后)。 – rmaddy 2013-05-02 18:09:36

+0

非常感谢!有用! – leirbaggmj 2013-05-02 18:16:03