- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
今天早上,我正在查看我在某个地方找到的一些旧代码(并在应用程序中使用),这些代码破解了 UIAlertView
以便从用户那里获取一串输入。出于这个目的,它有点矫枉过正,看起来很傻,但我从未见过更简单的方法。然后我想出了以下方法,它似乎有效(iPad 5.1.1 和模拟器)。
我的问题有点开放,但从本质上讲,是否存在这种策略会中断的条件:创建一个带有附件 View 的屏幕外文本字段,将代理文本字段放在附件 View 中,然后转发代理的各种属性设置?
PMKeyboardTextField.h:
#import <UIKit/UIKit.h>
@interface PMKeyboardTextField : UITextField
- (id)initWithPrompt:(NSString *)prompt;
@end
PMKeyboardTextField.m:
#import "PMKeyboardTextField.h"
@interface PMKeyboardTextField ()
@property (nonatomic, strong) UITextField *inputField;
@end
@implementation PMKeyboardTextField
@synthesize inputField = _inputField;
- (id)initWithPrompt:(NSString *)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
selector:@selector(keyboardDidShow)
name:UIKeyboardDidShowNotification
object:nil];
}
return self;
}
- (void)keyboardDidShow {
[self.inputField becomeFirstResponder];
}
- (id<UITextFieldDelegate>)delegate {
return self.inputField.delegate;
}
- (void)setDelegate:(id<UITextFieldDelegate>)delegate {
self.inputField.delegate = delegate;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
最佳答案
如果您的目标是 iOS 5.0 或更高版本,则无需破解警报 View 即可添加文本字段。您只需将警报 View 的 alertViewStyle
设置为 UIAlertViewStylePlainTextInput
即可为警报 View 提供一个纯文本输入字段,然后您可以通过发送 textFieldAtIndex:
来访问它到警报 View 。
除此之外,您的解决方法似乎还不错。我想您必须将屏幕外文本字段添加到警报 View 的窗口,而不是您的普通应用程序窗口。
关于ios - inputAccessoryView 可以用于实际输入吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11973634/
我正在尝试在 SwiftUI 中的 TextField 上实现一个 inputAccessoryView。目标是在键盘上方出现一个“完成”按钮,按下该按钮时会摆脱键盘(即 resignFirstRes
我已添加 InputAccessoryView到我的键盘消失。它炒得很好。但在某些 ViewController它没有出现。即使那个方法也不会调用。 我有一个按钮。当我点击我的文本字段时,它会自动成为
自从我升级到 Xcode 5.1 后,我的 inputAccessoryView 出现了问题。 如您在附图中所见,按键在 inputAccessoryView 内的工具栏后面弹出。我认为它与新版本的
我正在为 UITextField 和 UIToolbar 设置一个 datepicker 来代替键盘。我希望能够关闭 accessoryView,但它没有正常运行。虽然我按下了 Done 按钮,但没有
我有一个 Objective-C 项目,我想在其中添加一个 inputAccessoryView 到 UITextView 的子类。老办法是 self.myTextView.inputAccessor
我已经看过了this solution .我已经创建了一个显示错误的示例项目。 该 View 只有一个 UITextField。 这是我的代码: class ViewController: UIVie
Problem relates to this.我正在尝试使用我制作的自定义 View ,它有两个 TextField 输入和一个按钮,我使用 IB 它的 xib 制作它,我将它放在我的 Storyb
今天早上,我正在查看我在某个地方找到的一些旧代码(并在应用程序中使用),这些代码破解了 UIAlertView 以便从用户那里获取一串输入。出于这个目的,它有点矫枉过正,看起来很傻,但我从未见过更简单
我正在尝试复制 Facebook Messenger 应用程序,其中有一个 UITextView 附加到键盘顶部。 由于此应用的性质,我需要附加我的 View ,而不是在出现键盘时手动上下滚动Scro
是否可以在键盘上方创建 inputAccessoryView,用于拆分模式下的屏幕全宽?系统会自动调整其大小以适合应用程序框架。这是 iOS 中的错误吗? 最佳答案 您可以使用此方法检测您的键盘是否处
知道如何让 inputAccessoryView 锚定到标签栏而不是屏幕底部吗? 我创建了一个 UIViewController 并覆盖了以下方法: -(BOOL)canBecomeFirstResp
我正在尝试创建一个类似于 iOS 消息传递应用程序的消息传递界面。我正在努力解决的部分是覆盖 inputAccessoryView(在 View Controller 上)对我没有影响。 如果您查看下
我正在尝试添加带有“完成”按钮的 UIView 作为文本字段的输入附件 View 。 let view = UIView() let doneButton = UIBu
UISearchBar 似乎将 inputAccessoryView 作为 readOnly 属性。如何使用我自己的 customToolbar 进行设置? 最佳答案 编辑:如以下评论中所述,这不再是
我在 textView 的 inputAccessoryView 属性中使用工具栏。当键盘显示时,它会按预期显示工具栏。旋转设备时,我想删除工具栏。我试过: myTextView.inputAcce
我正在尝试实现与 Apple 消息应用中底部文本输入栏类似的定位行为。 我尝试了很多方法,到处搜索,有很多类似的问题,但没有一个是令人满意的。 要指定: 在 View 底部有一个UIToolbar 工
我已经为我的应用程序中的某些 UITextFields 添加了一个 inputAccessoryView 到 iOS 键盘。这个附件 View 为用户提供了基本功能(一些按钮在我的应用程序的其他地方没
我正在以模态方式呈现一个 UIViewController,该 View 包含各种 UITextFields。我已经重写了模态视图 InputAccessoryView,以便它显示一个简单的 View
全部, 我的 UITextView 上附加了一个可滚动的 InputAccessoryView。创建如下: 创建一个 UIScrollView 添加一个水平 UIStackView。 将堆栈 View
我有一个简单的 UIViewController,其中包含一些对象:一个 UITextView 和一个作为 InputAccessoryView 添加到 UTextView 的 UIToolBar。当
我是一名优秀的程序员,十分优秀!