所以我正在寻找一种在iPhone中创建某种表单的方法。例如,我添加了3个文本字段,当我点击第一个并在其中写入内容时,我希望能够按下next,这将带我进入下一个文本字段。
链接:http://shrani.si/f/3U/lB/3Nl9qXha/primer.png
我想做一些类似于你填写网络表格的事情。
当用户单击Next时,您可以使用,
[nextTextFieldAlong becomeFirstResponder];
将光标移动到下一个字段。
首先,您必须为字段(1-3)设置标签,然后在文本字段中设置类似的accsesory视图
[messageView setInputAccessoryView:[self createAccesoryView]];
你可以使用这样的方法来生成附件视图(这是从苹果文档中获取的):
-(UIView*)createAccesoryView{
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setBarStyle:UIBarStyleDefault];
[toolbar sizeToFit];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]];
[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentControl addTarget:self action:@selector(selectNextPrevious:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *select = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTyping)];
[toolbar setItems:[NSArray arrayWithObjects:select, flex, done, nil]];
return toolbar;
}
然后在selectNextPrevious
中
-(void)selectNextPrevious:(id)sender{
if ([(UISegmentedControl*)sender selectedSegmentIndex]==0) {
if (activeField.tag>1) {
[[self.view viewWithTag:activeField.tag-1] becomeFirstResponder];
}
else {
if (activeField.tag<numberOfFields) {
[[self.view viewWithTag:activeField.tag+1] becomeFirstResponder];
}
}
}