使用编辑按钮更改分段控件的标题? [英] Change title of segmented control with edit button?

查看:86
本文介绍了使用编辑按钮更改分段控件的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个Edit barButtonItem,按下后我希望能够选择一个分段控件并编辑我选择并保存的分段标题。

解决方案

花一些时间提出示例,但是这里是!!! >

这是我的UIViewController头文件中的内容:

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

//添加以支持此示例
UISegmentedControl * controlBeingEdited;
unsigned int segmentBeingEdited;
}

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

很明显,我的UISegmented控件及其操作项已在Interface Builder中连接。 / p>

您必须为您的分段控制实施操作项,这里是我的

 -(IBAction)centerAreaSizeSelector:(id)sender {
//这些是从选择器
的零读取。unsigned char charcenterAreaSizeSelection = centerAreaSizeSelector.selectedSegmentIndex;

//在这里我们实例化一个新的不可见UITextField来调出键盘。
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 =自我;
[textField成为FirstResponder];

//以下变量是全局定义的,以允许键盘委托例程
//了解哪个分段控件以及控件中的哪个项目来编辑
//我的设计具有多个UISegmentedControls,因此需要使用此控件来分隔
controlBeingEdited = centerAreaSizeSelector; //类型为UISegmentedControl
segmentBeingEdited = centerAreaSizeSelection; //类型为unsigned int
}

执行以下3个UITextFieldDelegate方法,

  //实现键盘委托例程
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[ textField resignFirstResponder];
[textField版本];
controlBeingEdited = nil;
segmentBeingEdited = 0;
返回是;
}

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

[controlBeingEdited setTitle:theString forSegmentAtIndex:segmentBeingEdited];

返回是;
}

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

这实现了UISegmentedControl元素的逐项可见编辑。 / p>

注释:
如果文本大于控件所提供的空间,则该方法丝毫不会实现自动调整大小的功能。



这也不会实现任何形式的可见光标或可见选择代码。



这将保留文本框插入符号的位置字符串中最后一个字符之后。它将在编辑之前将当前UISegmentedControl的文本复制到不可见的文本字段中,以使您不会丢失副本,尽管可以轻松地对其进行编辑以在编辑之前清除这两个文本。


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?

解决方案

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.

这篇关于使用编辑按钮更改分段控件的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆