如何从alertview获取textview的价值

时间:2013-03-14 10:30:50

标签: ios textview alertview

我无法从alertView

获取textView的值
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];

    [textView setText:@""];

    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"put text\n\n\n\n\n\n" message:@"" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:@"Envoyer",nil];
    av.alertViewStyle = UIAlertViewStyleDefault;
    [av addSubview:textView];
    [av show]; 

它显示带有textview的alertview视图,并且它很好,我想得到textview的值我的

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {        
}
你可以帮我吗

5 个答案:

答案 0 :(得分:4)

声明textView以便在整个班级中使用

@implementation objectTables
{
    UITextView *textView;
}

然后在你的

textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];

    [textView setText:@""];

    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Push Notification\n\n\n\n\n\n" message:@"" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:@"Envoyer",nil];
    av.alertViewStyle = UIAlertViewStyleDefault;
    [av addSubview:textView];
    [av show]; 

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

   NSString *textViewText = [textView text];

}

答案 1 :(得分:1)

你可以使用 - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex ,它将返回 UITextField 的对象。

并从 UITextField 获取文字。

或者另一种方式是

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


UITextField *TextField = (UITextField *)[alertView viewWithTag:YOURTAG];
}

答案 2 :(得分:1)

你可以试试这个:

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:@""];
textView.tag = 10;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"put text\n\n\n\n\n\n" message:@"" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:@"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show]; 

代表

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
      UITextView *textView = (UITextView *) [alertView viewWithTag:10];
      NSString *yourText = textView.text;       
}

答案 3 :(得分:0)

只需在.h文件中声明UITextView

UITextView *textView;

和.m文件

textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];

    [textView setText:@""];

    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"put text\n\n\n\n\n\n" message:@"" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:@"Envoyer",nil];
    av.alertViewStyle = UIAlertViewStyleDefault;
    [av addSubview:textView];
    [av show];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
//Gives you the Value of UITextView
        NSLog(@"%@",[textView text]); 
}

或者您可以将其定义为Extended class(。m文件)中的属性并访问它。

@Interface yourINterfaceName()
{
@property(nonatomic , retain ) UITextView *textView;
}

答案 4 :(得分:0)

试试这个

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];

[textView setText:@""];
textView.tag =10;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"put text\n\n\n\n\n\n" message:@"" delegate:self cancelButtonTitle:@"Annuler" otherButtonTitles:@"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];



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

 UITextView *textView = (UITextView *)[alertView viewWithTag:10];
  NSLog(@"%@",[textView text]); 

}