在iPhone上使用Actionsheet Picker获取文本输入弹出对话框的简单方法是什么

时间:2018-06-29 08:41:34

标签: objective-c

enter image description here

想要在警报显示目标C时从textField的pickerView中获取输入

1 个答案:

答案 0 :(得分:1)

注意 :首先从Alertview打开操作表,然后关闭操作表,然后再打开操作表。我的代码可能会帮助您解决从Alertview打开操作表

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate,UIActionSheetDelegate>
{
    NSString *alertSelectValue;
    UITextField * alertTextField;
}
@end

@implementation ViewController

- (void)viewDidLoad {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
     alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * alertTextField = [alert textFieldAtIndex:0];
    alertTextField.tag=101;
    alertTextField.delegate=self;
    alertTextField.keyboardType = UIKeyboardTypeNumberPad; alertTextField.placeholder = @"Enter your name";
    [alert show];



    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag==101)
    {
        [textField setUserInteractionEnabled:YES];
        [textField resignFirstResponder];
        NSString*String = [NSString stringWithFormat:@"%@",textField.text];
        [self openSheet:String];
    }

}

-(void)openSheet:(NSString*)text
{

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select" delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:text,@"Test" ,nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
   UIViewController *topvc=[self topMostController];
    [actionSheet showInView:topvc.view];

}


//-------- Get top Most view to -----
- (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}

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

    NSLog(@"Index = %d - Title = %@", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);

    if(buttonIndex == 0)
    {
        alertTextField.text= [actionSheet buttonTitleAtIndex:buttonIndex];

    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end