0

我无法从我的 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 {        
}

你能帮我吗

4

5 回答 5

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];

}
于 2013-03-14T10:34:21.887 回答
1

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

并从UITextField中获取文本。

或者另一种方式是

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


UITextField *TextField = (UITextField *)[alertView viewWithTag:YOURTAG];
}
于 2013-03-14T10:32:12.403 回答
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;       
}
于 2013-03-14T10:49:03.997 回答
0

只需UITextView在 .h 文件中声明您的

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;
}
于 2013-03-14T10:39:56.387 回答
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]); 

}
于 2013-03-14T10:40:50.813 回答