将inputAccessoryView与多个UITextFields和ViewControllers一起使用

时间:2014-02-09 20:20:20

标签: ios objective-c uitextfield

我正在尝试利用inputAccessoryView并显示帮助文本以及上一个和下一个按钮。但是我有很多UITextField跨越许多ViewControllers。所以我想为inputAccessoryView提供一个可重用的UIView。

为此,我创建了一个xib文件('SetupKeyboardAccessory')和一个附加到该xib的ViewController('SetupKeyboardAccessoryViewController')。

SetupKeyboardAccessory

enter image description here

SetupKeyboardAccessoryViewController

@interface SetupKeyboardAccessoryViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *helpTextLabel;
@property (weak, nonatomic) IBOutlet UIButton *previousButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@end

在我的MainViewController中我有我的UITextFields。这个MainViewController正在实现UITextFieldDelegate:

MainViewController

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    // you can create the accessory view programmatically (in code), or from the storyboard
    if (textField.inputAccessoryView == nil) {
        SetupKeyboardAccessoryViewController *accessoryView =
        [[SetupKeyboardAccessoryViewController alloc] initWithNibName:@"SetupKeyboardAccessory"
                                                bundle:nil];
        accessoryView.helpTextLabel.text = @"setting help text here";
        textField.inputAccessoryView = accessoryView.view;
    }

    //self.navigationItem.rightBarButtonItem = self.doneButton;

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;
}

当文本字段开始编辑时,我初始化我的SetupKeyboardAccessoryViewController,设置帮助文本,然后将其设置为inputAccessoryView。由于某种原因,帮助文本没有设置。

我知道我可能在将SetupKeyboardAccessoryViewController的视图设置为inputAccessoryView时做错了。我还是iOS开发的新手。所以任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

在一点点挖掘之后弄清楚问题是什么。当我试图设置helpTextLabel的文本时,原来是helpTextLabel本身是零。

这是由于在视图控制器视图已完全实例化之前未设置插座。 (来源:https://stackoverflow.com/a/4005968/986105

所以在我的SetupKeyboardAccessoryViewController.m文件中,我添加了自己的init函数:(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withHelpText:(NSString*)helpText

然后在viewDidLoad函数中,我将设置UILabel的文本。

<强> SetupKeyboardAccessoryViewController.m:

@interface SetupKeyboardAccessoryViewController ()
{
    NSString* helperText;
}
@end

@implementation SetupKeyboardAccessoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withHelpText:(NSString*)helpText
{
    self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }

    helperText = helpText;
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

    self.helpTextLabel.text = helperText;

}
- (IBAction)previousButton:(id)sender {
    NSLog(@"prev button pressed");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

所以在我的MainViewController中,我只需调用我的init函数并删除该行来设置UILabel的文本。

<强> MainViewController:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    // you can create the accessory view programmatically (in code), or from the storyboard
    if (textField.inputAccessoryView == nil) {
        SetupKeyboardAccessoryViewController *accessoryView =
        [[SetupKeyboardAccessoryViewController alloc] initWithNibName:@"SetupKeyboardAccessory"
                                                bundle:nil withHelpText:@"setting help text here"];
        textField.inputAccessoryView = accessoryView.view;
    }

    //self.navigationItem.rightBarButtonItem = self.doneButton;

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;
}

答案 1 :(得分:2)

仅为了实例化视图而创建视图控制器是不合适的,然后才丢弃视图控制器。

如果您只有一个SetupKeyboardAccessoryView类是UIView的子类,那么您的代码会更简单。然后在这个类的nib文件中创建视图,并使用:

实例化它
SetupKeyboardAccessoryView *accessoryView = [[[NSBundle mainBundle] loadNibNamed:@"SetupKeyboardAccessoryView" owner:nil options:nil] firstObject]