![](/img/trans.png)
[英]How to show pickerView in ipad actionSheet using tabBarItems
[英]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 = @"";
}
}
但是什么也没发生。
将delegate:sender
delegate:self
替换为delegate:self
,这就是委托人的原因
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
一旦调用完毕,只需将所有textFiled设置为.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
。 除非需要支持iOS 7,否则应使用UIAlertController
。
您正在使用的委托方法
请让你的代表自我
delegate:self
并使用此方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
////check index and empty all textfields
}
您应该将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.