• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - inputAccessoryView 可以用于实际输入吗

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:28:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

今天早上,我正在查看我在某处(并在应用程序中使用)发现的一些旧代码,这些代码会破解 UIAlertView 以便从用户那里获取一串输入。就目的而言,这有点矫枉过正,而且看起来很愚蠢,但我从未见过更简单的方法。然后我想出了以下方法,似乎可以工作(iPad 5.1.1 和模拟器)。

我的问题有点开放,但从本质上讲,是否存在将其作为策略中断的条件:创建带有附件 View 的屏幕外文本字段,在附件 View 中放置代理文本字段,然后转发代理的各种属性设置?

PMKeyboardTextField.h:

#import <UIKit/UIKit.h>

@interface PMKeyboardTextField : UITextField
- (id)initWithPromptNSString *)prompt;
@end

PMKeyboardTextField.m:

#import "MKeyboardTextField.h"

@interface PMKeyboardTextField ()
@property (nonatomic, strong) UITextField *inputField;
@end

@implementation PMKeyboardTextField
@synthesize inputField = _inputField;

- (id)initWithPromptNSString *)prompt {
    self = [super initWithFrame:CGRectMake(-1, -1, 1, 1)];
    if (self) {
        UIView *accessory =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 72)];
        [accessory setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.8]];

        UILabel *getLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 284, 21)];
        [getLabel setBackgroundColor:[UIColor clearColor]];
        getLabel.text = prompt;
        [accessory addSubview:getLabel];

        self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(18, 37, 264, 31)];
        [accessory addSubview:self.inputField];

        self.inputAccessoryView = accessory;
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selectorselector(keyboardDidShow)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
    }
    return self;
}

- (void)keyboardDidShow {
    [self.inputField becomeFirstResponder];
}

- (id<UITextFieldDelegate>)delegate {
    return self.inputField.delegate;
}

- (void)setDelegateid<UITextFieldDelegate>)delegate {
    self.inputField.delegate = delegate;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end



Best Answer-推荐答案


如果您的目标是 iOS 5.0 或更高版本,则无需破解警报 View 即可添加文本字段。您只需将警报 View 的 alertViewStyle 设置为 UIAlertViewStylePlainTextInput 即可为警报 View 提供一个纯文本输入字段,然后您可以通过发送 textFieldAtIndex: 访问该字段到警报 View 。

除此之外,您的解决方法似乎还可以。我猜您必须将屏幕外文本字段添加到警报 View 的窗口,而不是普通的应用程序窗口。

关于ios - inputAccessoryView 可以用于实际输入吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11973634/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap