[英]How to perform reset of textfield using actionsheet?
I want to reset the textfields of myview to empty when an actionsheet destructive button is pressed. 我想在按下操作表破坏性按钮时将myview的文本字段重置为空。 Done button calls the actionsheet. “完成”按钮将调用操作表。 This is my actionsheet: 这是我的操作表:
- (IBAction)submit:(id)sender {
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"Options" delegate:sender cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Reset" otherButtonTitles:@"Save", nil];
[sheet showInView:self.view];
}
And is used this method to reset: 并使用此方法重置:
- (void)sheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
self.textf1.text = @"";
}
}
But Nothing is happening. 但是什么也没发生。
Replace delegate:sender
to delegate:self
that's why delegates 将delegate:sender
delegate:self
替换为delegate:self
,这就是委托人的原因
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
are not getting call also update the delegate function, once you done just set all textFiled to .text= @""
. 一旦调用完毕,只需将所有textFiled设置为.text= @""
,就不会更新调用。 i hope it will work 我希望它能工作
Also posted as Comment earlier. 也在早期发布为评论。
Lots of issues: 很多问题:
Change this line: 更改此行:
if (buttonIndex == 0)
to: 至:
if (buttonIndex == sheet.destructiveButtonIndex)
You also need to pass self
as the delegate instead of sender
. 您还需要将self
作为委托而不是sender
传递。
UIActionSheet *sheet= [[UIActionSheet alloc] initWithTitle:@"Options" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Reset" otherButtonTitles:@"Save", nil];
And the name of the delegate method matters. 和委托方法的名称很重要。 You need: 你需要:
- (void)actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)buttonIndex
See the docs for UIActionSheet
. 请参阅UIActionSheet
的文档。 There are specific properties to get various button indexes. 有一些特定的属性可以获取各种按钮索引。 Use those over hardcoding index numbers. 使用那些过度编码索引号。
Also note that UIAlertView
is deprecated. 另请注意,不建议使用UIAlertView
。 You should be using UIAlertController
unless you need to support iOS 7. 除非需要支持iOS 7,否则应使用UIAlertController
。
delegate method you are using is worng 您正在使用的委托方法
Please make your delegate to self 请让你的代表自我
delegate:self
and Use this method 并使用此方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
////check index and empty all textfields
}
You should set self as the delegate, and I think the new UIAlertController is more convenience to use, without any delegate: 您应该将self设置为委托,我认为新的UIAlertController在没有任何委托的情况下使用起来更加方便:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.