Frage

I have a table view controller with a 5 static cells and one calls a UIPicker like this:

#pragma mark PickerView DataSource

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.cityNames count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.cityNames objectAtIndex:row];
}

#pragma mark PickerView Delegate
-(void)presentNewPicker{
    UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:@"For which city?"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:nil];


    self.pickrView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    self.pickrView.showsSelectionIndicator = YES;
    self.pickrView.dataSource = self;
    self.pickrView.delegate = self;

    self.cityNames = [[NSArray alloc] initWithObjects: @"Akron", @"Indie", @"Springfield", @"Loria", @"Merida", nil];


    UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerDateToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClick)];
    [barItems addObject:doneBtn];

    [pickerDateToolbar setItems:barItems animated:YES];

    [aac addSubview:pickerDateToolbar];
    [aac addSubview:self.pickrView];
    [aac showInView:self.view];
    [aac setBounds:CGRectMake(0,0,320, 464)];
}

-(void) dismissActionSheet:(id)sender {
NSLog(@"dismissActionSheet");

UIActionSheet *actionSheet =  (UIActionSheet *) ??? how do i get action sheet? :-);
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

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

    self.myCity.text = [self.cityNames objectAtIndex:row];
    NSLog(@"city %@", self.myCity.text);
}

The UIPicker is presented but when I click the Done button, but how do I dismiss it now? Also, the city is logging null and Im not sure why since I init'd the picker with those cities and they DO show up in the picker once its presented.

War es hilfreich?

Lösung

Store a reference to your UIActionSheet in a property or instance variable

@property (nonatomic) UIActionSheet *actionSheet;

and when you create the action sheet use

self.actionSheet

instead of

UIActionSheet *aac

then call

[self.actionSheeet dismissWithClickedButtonIndex:0 animated:YES];

Andere Tipps

UIActionSheet *aac;

make it global

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top