键盘上的iOS下一个按钮不起作用

时间:2015-06-30 19:40:56

标签: ios objective-c keyboard uitextfield

我找到了这个解决方案,让ui键盘上的下一个按钮转到下一个文本字段,但它对我来说根本不起作用。在故事板中我还有什么需要做的吗?另外,如何让最终文本字段的下一个按钮调用unwind segue?谢谢

//
//  AddToDoItemViewController.m
//  ToDoList
//
//  Copyright (c) 2015 com.example. All rights reserved.
//

#import "AddToDoItemViewController.h"

@interface AddToDoItemViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UITextField *totalTextField;
@property (weak, nonatomic) IBOutlet UITextField *tipTextField;
@property (weak, nonatomic) IBOutlet UIView *singleTableView;
@property (weak, nonatomic) IBOutlet UISwitch *ccSwitch;

@end

@implementation AddToDoItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.textField) {
        [self.totalTextField becomeFirstResponder];
    }
    else if (textField == self.totalTextField ) {
        [self.tipTextField becomeFirstResponder];
    }

    else{
        [textField resignFirstResponder];
    }
    return YES;
}

/*- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];
    } else if (theTextField == self.totalTextField) {
        [self.tipTextField becomeFirstResponder];
    }
    return YES;
}
 */


- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    NSLog(@"sender = %@",sender);
    if (sender != self.saveButton)  return YES;
    NSNumberFormatter *formatter1 = [[NSNumberFormatter alloc] init];
    NSNumber *totalOrder = [formatter1 numberFromString:self.totalTextField.text];
    NSNumber *tipOrder = [formatter1 numberFromString:self.tipTextField.text];
    double tot = totalOrder.doubleValue;
    double totalOrderWithTip = tipOrder.doubleValue;
    if(self.textField.text.length <= 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Field not entered"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return NO;


    }
    if(self.totalTextField.text.length <= 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Field not entered"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return NO;


    }
    if(self.tipTextField.text.length <= 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Field not entered"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return NO;


    }
    if(totalOrder == nil)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Invalid Order Total"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return NO;


    }
    if(tipOrder == nil)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Invalid Amount Recieved"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return NO;


    }

    if(totalOrderWithTip < tot)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"The amount recieved must be       equal to or greater than the order total."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return NO;

    }
    return YES;



}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if (sender != self.saveButton) return;
    if (self.textField.text.length > 0) {

        NSNumberFormatter *formatter1 = [[NSNumberFormatter alloc] init];
       // NSNumber *tipPercent = [[NSNumber alloc] initWithFloat:0.0];
        NSNumber *totalOrder = [formatter1 numberFromString:self.totalTextField.text];
        NSNumber *tipOrder = [formatter1 numberFromString:self.tipTextField.text];
        NSNumber *actualTip = [[NSNumber alloc] initWithFloat:tipOrder.doubleValue - totalOrder.doubleValue];
        double tot = totalOrder.doubleValue;
        double tip1 = tipOrder.doubleValue - tot;
        self.toDoItem = [[ToDoItem alloc] init];
        self.toDoItem.location = self.textField.text;
        NSNumber *percent1 = [[NSNumber alloc] initWithDouble:(tip1/tot)*100.0];
        if(self.ccSwitch.isOn)
            self.toDoItem.isCreditCard = YES;
        else
            self.toDoItem.isCreditCard = NO;

        self.toDoItem.total = totalOrder;
        self.toDoItem.tip = actualTip;
        self.toDoItem.percentage = percent1;
        self.toDoItem.completed = NO;
    }
}


@end

1 个答案:

答案 0 :(得分:0)

在textField的Connections Inspector中,从“Outlets” - “delegate”部分下的“circle”拖动到View Controller Icon。或者您可以在viewDidLoad方法中进行设置。有些人喜欢:

self.textField.delegate = self;

最后一个textField正在关闭键盘,因为您正在告诉键盘重新调整其第一个响应者条件。只需替换您想要呼叫的那一行:

[self shouldPerformSegueWithIdentifier:@"YOUR_IDENTIFIER" sender:your_sender];

就是这样!