gpt4 book ai didi

objective-c - 获取UIAlertView以响应键盘上的返回,并具有与“确定”按钮相同的结果

转载 作者:行者123 更新时间:2023-12-01 17:58:46 25 4
gpt4 key购买 nike

如果要按下警报上的按钮,或者要按下键盘上的回车键,我想运行相同的代码。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
适用于警报上的“确定”按钮,但是找不到与返回键等效的按钮。

我一直在尝试- (BOOL)textFieldShouldReturn:(UITextField *)alertTextField,但无济于事。

一年前,在对this site的评论中的三个人有相同的问题,但未得到回答:

您如何获得Alertview来响应键盘上的“Return”,其结果与点击“OK”按钮的结果相同?我似乎在Google上找不到适用于iOS 5 SDK的任何东西。

完成此步骤需要采取什么步骤?

编辑:

进一步来说:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

[[NSUserDefaults standardUserDefaults] setObject:[alertView textFieldAtIndex:0].text forKey:@"url_preference"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

当用户按“确定”时,将运行该代码。键盘上的Return键有什么相似的简单等效项?

目前,我正在尝试:
- (BOOL)textFieldShouldReturn:(UITextField *)alertTextField {
[alertTextField resignFirstResponder];
NSLog(@"test");
return YES;
}

什么都不做。

这是我的警报的定义:
-(void) noUrl {

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Add a URL" message:@"Change it later in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Load",nil]; //If you change button name, change it in the other alertView function
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeURL;
alertTextField.returnKeyType = UIReturnKeyGo;
alertTextField.enablesReturnKeyAutomatically = YES;
alertTextField.placeholder = @"http://www.example.com";
[alert show];
return;

}

如果需要将代码添加到另一个文件或另一个地方,我将不胜感激。

最佳答案


[alert textFieldAtIndex:0].delegate = self; 

然后实施
- (BOOL)textFieldShouldReturn:(UITextField *)alertTextField {//add any method you want to execute to here. This method will be called when user taps on the textfield in alertview.
[alertTextField resignFirstResponder];// to dismiss the keyboard.
[alert dismissWithClickedButtonIndex:0 animated:YES];//this is called on alertview to dismiss it.
}

还要将 UITextFieldDelegate放在您的.h文件中,
@interface CustomViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate> 

根据问题中的当前修改,您不见了,
alertTextField.delegate = self; 

更新:

根据您的评论,将警报设为类级别变量,
@interface CustomViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate> {
UIAlertView *alert;
}

然后将此方法更改为
-(void) noUrl {

alert = [[UIAlertView alloc] initWithTitle:@"Add a URL" message:@"Change it later in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Load",nil]; //If you change button name, change it in the other alertView function
//rest of the code here...

关于objective-c - 获取UIAlertView以响应键盘上的返回,并具有与“确定”按钮相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170480/

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