iphone - 如何在代码而不是 Interface Builder 中设置 didEndOnExit?
<p><div>
<aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
<div class="d-flex fd-column fw-nowrap">
<div class="d-flex fw-nowrap">
<div class="flex--item wmn0 fl1 lh-lg">
<div class="flex--item fl1 lh-lg">
<b>这个问题在这里已经有了答案</b>:
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>假设您谈论的是 <code>textFieldDidEndEditing:</code> 或 <code>textFieldShouldReturn:</code>。您将在 ViewController 中实现该方法,并将文本字段 <code>delegate</code> 设置为 <code>self</code>,如下所示:</p>
<pre><code>- (void)viewDidLoad {
;
// ....
myTextField = [ initWithFrame:CGRectMake(0, 0, 50, 25)];
myTextField.delegate = self;
;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == myTextField) {
// Do stuff...
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == myTextField) {
return YES;
}
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ((myTextField.text.length + string.length) < 5) {
return YES;
}
return NO;
}
</code></pre>
<p>然后,在 <code>@interface</code> 的头文件中,您可以像这样指定您是 <code>UITextFieldDelegate</code>:</p>
<pre><code>@interface MyViewController : UIViewController <UITextFieldDelegate> {
// ....
UITextField *myTextField;
// ....
}
// Optionally, to make myTextField a property:
@property (nonatomic, retain) UITextField *myTextField;
@end
</code></pre>
<p> <a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextField_Class/Reference/UITextField.html" rel="noreferrer noopener nofollow"><strong>UITextField Reference</strong></a> <br/>
<a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html" rel="noreferrer noopener nofollow"><strong>UITextFieldDelegate Reference</strong></a> </p>
<hr/>
<p>编辑(根据您的评论,这应该可以):</p>
<pre><code>- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ((myTextField.text.length + string.length) < 5) {
return YES;
}
return NO;
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 如何在代码而不是 Interface Builder 中设置 didEndOnExit?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/7771985/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/7771985/
</a>
</p>
页:
[1]