Edit barButtonItemが必要です。押されたときに、セグメント化されたコントロールを選択し、選択したセグメントタイトルを編集して保存できるようにします。これは可能ですか?
1639 次
1 に答える
1
例を思いつくのに少し時間がかかりましたが、ここにあります!!!
これが私の UIViewController ヘッダー ファイルの内容です。
@interface optionsViewController : UIViewController <UIPopoverControllerDelegate, UITextFieldDelegate> {
IBOutlet UISegmentedControl * centerAreaSizeSelector;
// Added to support this example
UISegmentedControl * controlBeingEdited;
unsigned int segmentBeingEdited;
}
-(IBAction)centerAreaSizeSelector:(id)sender;
@end
明らかに、UISegmented コントロールとそのアクション アイテムは、Interface Builder で接続されていました。
セグメント化されたコントロールのアクション アイテムを実装する必要があります。
-(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
}
次の 3 つの UITextFieldDelegate メソッドを次のように実装します。
// 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];
}
これは、UISegmentedControl 要素のキーごとの可視編集を実装します。
注: これは、テキストがコントロールによって提供されるスペースよりも大きい場合に必要になる可能性のある自動サイズ変更を実装しません。
これは、可視カーソルまたは可視選択コードの形式も実装しません。
これにより、文字列の最後の文字の後にテキストフィールドのキャレット位置が残ります。編集前に現在の UISegmentedControl のテキストを非表示のテキスト フィールドにコピーするので、コピーが失われることはありませんが、編集前に両方をクリアするように簡単に編集できます。
于 2012-06-12T03:08:57.473 に答える