Change title of segmented control with edit button?

∥☆過路亽.° 提交于 2019-12-10 23:56:21

问题


I want to have an Edit barButtonItem, when pressed I want to be able to select a segmented control and edit the segment title I select and save it. Is this possible?


回答1:


Took me some time to come up with the example, but here it is!!!

Here is what is in my UIViewController header file:

@interface optionsViewController : UIViewController <UIPopoverControllerDelegate, UITextFieldDelegate> {
    IBOutlet UISegmentedControl *       centerAreaSizeSelector;

    // Added to support this example
    UISegmentedControl * controlBeingEdited;
    unsigned int segmentBeingEdited;
}

-(IBAction)centerAreaSizeSelector:(id)sender;
@end

Obviously my UISegmented control and its action item were connect in Interface Builder.

You must implement the action item for your segmented control here is mine

-(IBAction)centerAreaSizeSelector:(id)sender{
    // These are zero Based Reads from the selectors
    unsigned char centerAreaSizeSelection = centerAreaSizeSelector.selectedSegmentIndex;

    // Here we instantiate a NEW invisible UITextField to bring up the keyboard.
    UITextField * textField = [[UITextField alloc] init];
    [self.view addSubview:textField];
    textField.hidden = YES;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.text = [centerAreaSizeSelector titleForSegmentAtIndex:centerAreaSizeSelection];
    textField.delegate = self;
    [textField becomeFirstResponder];

    // The below variable are defined globally to allow the keyboard delegate routines
    // to know which segmented control and which item within the control to edit
    // My design has multiple UISegmentedControls so this is needed for separation
    controlBeingEdited = centerAreaSizeSelector;  // of type UISegmentedControl
    segmentBeingEdited = centerAreaSizeSelection; // of type unsigned int
}

Implement the following 3 UITextFieldDelegate Methods as follows

// Implement the keyboard delegate routines
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [textField release];
    controlBeingEdited = nil;
    segmentBeingEdited = 0;
    return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * theString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    [controlBeingEdited setTitle:theString forSegmentAtIndex:segmentBeingEdited];

    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [controlBeingEdited setTitle:textField.text forSegmentAtIndex:segmentBeingEdited];
}

This implements a key-by-key visible editing of a UISegmentedControl element.

NOTES: This does not in any way implement the auto-resizing that may be necessary if the text is larger than the space provided by the control.

This also does not implement any form of visible cursor or visible selection code.

This will leave the textfield caret position after the last character in the string. It will copy the current UISegmentedControl' text into the invisible textfield prior to editing so that you don't lose your copy, although it could easily be edited to clear both prior to editing.



来源:https://stackoverflow.com/questions/10925376/change-title-of-segmented-control-with-edit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!