Search code examples
iosuipickerviewuibarbuttonitemuiactionsheet

How to modify a Done button programmatically from UIActionSheet?


I have this method to create an action sheet and uipicker:

-(void)presentNewPicker{
    self.aac = [[UIActionSheet alloc] initWithTitle:@"What City?"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:@"OK", 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: @"Phoenix", @"Tahoe", @"Nevada", @"Lime", @"Florida", 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(dismissActionSheet:)];

    [barItems addObject:doneBtn];

    [pickerDateToolbar setItems:barItems animated:YES];

    [self.aac addSubview:pickerDateToolbar];


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

The button that appears reads DONE but I want to modify it to read, Ready. How do I accomplish this?


Solution

  • Replace:

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

    with:

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Ready" style: UIBarButtonItemStyleBordered target:self action:@selector(dismissActionSheet:)];
    

    You may want to use UIBarButtonItemStyleDone for the style.