如何使用操作表执行文本字段的重置?

How to perform reset of textfield using actionsheet?

我想在按下操作表破坏性按钮时将 myview 的文本字段重置为空。完成按钮调用操作表。 这是我的操作表:

- (IBAction)submit:(id)sender {
    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"Options" delegate:sender cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Reset" otherButtonTitles:@"Save", nil];
    [sheet showInView:self.view];
}

并使用此方法重置:

- (void)sheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0)
    {
        self.textf1.text = @"";
    }
}

但是什么也没有发生。

很多问题:

更改此行:

if (buttonIndex == 0)

至:

if (buttonIndex == sheet.destructiveButtonIndex)

您还需要传递 self 作为代表而不是 sender

UIActionSheet *sheet= [[UIActionSheet alloc] initWithTitle:@"Options" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Reset" otherButtonTitles:@"Save", nil];

委托方法的名称很重要。您需要:

- (void)actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)buttonIndex

请参阅 UIActionSheet 的文档。有特定的属性来获取各种按钮索引。使用那些硬编码索引号。

另请注意,UIAlertView 已弃用。你应该使用 UIAlertController 除非你需要支持 iOS 7.

delegate:sender 替换为 delegate:self 这就是代表

的原因
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

未收到调用也更新委托函数,完成后只需将所有 textFiled 设置为 .text= @""。我希望它会起作用

之前也作为评论发布。

您使用的委托方法已损坏

请将您的代理权交给自己

delegate:self

并使用此方法

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
      ////check index and empty all textfields

}

你应该设置self为delegate,我觉得新的UIAlertController使用起来更方便,没有任何delegate:

 UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Action Sheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

 UIAlertAction *reset = [UIAlertAction actionWithTitle:@"Reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
     self.textf1.text = @"";
 }];

actionSheet.actions = @[reset, cancel];

[self presentViewController:actionSheet animated:YES completion:nil]
#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate,UIActionSheetDelegate>
{
    UIActionSheet *sheet;
}
@property (weak, nonatomic) IBOutlet UITextField *txtText;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//Button click 

- (IBAction)OpenActionSheet:(id)sender
{
    sheet=[[UIActionSheet alloc]initWithTitle:@"ActionSheetDemo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Reset" otherButtonTitles:@"Save", nil];
    [sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        _txtText.text=@"";
    }
}
@end