UITextField的InputAccessoryView

时间:2011-11-05 14:33:39

标签: uitextfield

我需要将一个带有UITextField的工具栏放在键盘内。

如果我将整个toolBar(我认为是textField的超级视图)设置为textField的inputAccessoryView怎么办?

我是这样的意思:

textField.inputAccessoryView = toolBar; // textField在toolBar中

我一直在尝试这个,但我还没有让它发挥作用。

任何人都可以提供帮助吗?

还是有另一种方法可以达到我的目的吗?

2 个答案:

答案 0 :(得分:2)

   - (void)viewDidLoad
      {
        [self createAccessoryView];

        [textField setDelegate:self];
        [textField setKeyboardType:UIKeyboardTypeDefault];
        [textField setInputAccessoryView:fieldAccessoryView];


      }


- (void)createAccessoryView
        {

            CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
            fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
            fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
            fieldAccessoryView.tag = 200;

            [fieldAccessoryView setBarStyle:UIBarStyleBlack];

            UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  target:self action:@selector(done:)];

            UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
            [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
            segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
            [segmentedControl setMomentary:YES];
            UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

            [fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
            [segmentButton release];
            [spaceButton release];
            [doneButton release];
            [segmentedControl release];

        }

答案 1 :(得分:0)

您何时设置酒店?您可能在视图加载后进行设置。

尝试在

中进行设置
- (void) viewDidLoad