gpt4 book ai didi

ios - 应该 [UIView endEditing :YES] ever return no?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:36 24 4
gpt4 key购买 nike

我正忙于修复一个错误,目前只有当我在模拟器上运行我的应用程序时才会出现这个错误。本质上,我有一个 UITextView,我正在尝试将消息 endEditing 发送到。如果我在用户编辑 TextView (强制或其他方式)时发送消息,我会返回 YES。但是,如果用户还没有开始编辑 textview 并且我发送消息 endEditing:YES,我会返回 NO。这甚至可能吗? endEditing:YES 不应该总是强制 View 结束编辑吗?

其他详细信息:我已尝试将拥有类设置为 uitextviews 委托(delegate),但即便如此,它看起来也不像 shouldEndEditing 方法被调用。


更新:这似乎不是正常行为(如果文本字段当前不是第一响应者,该方法应返回 no)。

我创建了一个简单的测试:

AppDelegate.h

#import <UIKit/UIKit.h>

@interface DRAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (assign) IBOutlet UITextView *textView;
@property (assign) IBOutlet UIButton *endButton;
@property (assign) IBOutlet UILabel *results;

-(IBAction)tapEndEditingButton:(id)sender;

@end

和 AppDelegate.m

-(void)tapEndEditingButton:(id)sender
{
BOOL didEndEditing = [self.window endEditing:YES];
NSString *result = (didEndEditing) ? @"YES" : @"NO";
_results.text = result;
}

无论文本字段是否有焦点,也无论我将 endEditing 的 force 参数设置为 YES 还是 NO,didEndEditing 都会设置为 YES。

最佳答案

检查 Apple Doc on UIView

endEditing:

Causes the view (or one of its embedded text fields) to resign the first responder status.

...

Return Value

YES if the view resigned the first responder status or NO if it did not.

Discussion

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.


所以...当你说:

If however, the user has not yet begun editing the textview and I send the message endEditing:YES, I get back NO

这非常好,因为在您的场景中,由于没有要辞职的 firstResponder,调用 -endEditing: 将返回 NO 并且不会不会影响性能(也不是错误,恕我直言)


回答问题的本质:

[UIView endEditing:YES/NO]; 将返回 NO 如果指定的 UIView 对象不是 firstResponder.

例子:

-(void)testEndEditing
{
UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom];
[btnTest setFrame:CGRectMake(0, 130, 320, 44)];
[btnTest setBackgroundColor:[UIColor blueColor]];
[btnTest addTarget:self action:@selector(myEndEditing:)
forControlEvents:UIControlEventTouchUpInside];
[btnTest setTitle:@"End Editing" forState:UIControlStateNormal];
[self.view addSubview:btnTest];

UITextField *txtFTest = [[UITextField alloc] init];
[txtFTest setFrame:CGRectMake(0, 50, 320, 30)];
[txtFTest setBackgroundColor:[UIColor orangeColor]];
[txtFTest setText:@"textField"];
[self.view addSubview:txtFTest];

/*
globally declare "UITextView *txtVTest;"
*/
txtVTest = [[UITextView alloc] init];
[txtVTest setFrame:CGRectMake(0, 80, 320, 50)];
[txtVTest setBackgroundColor:[UIColor redColor]];
[txtVTest setText:@"textView"];
[self.view addSubview:txtVTest];

//make UITextField object the firstResponder
[txtFTest becomeFirstResponder];
}

-(void)myEndEditing:(UIButton *)sender
{
//test endEditing method on UITextView object
BOOL isWhat = [txtVTest endEditing:YES];
NSLog(@"%i",isWhat);
}

PS:如果 textFieldtextView 都不是 firstResponder 那么它返回 YES (不知道为什么,但我会检查)

关于ios - 应该 [UIView endEditing :YES] ever return no?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22540864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com