2013-01-18 53 views
-1

內容得到了下面的代碼:充分利用自定義UITextfields在UIAlertView中

-(void)addGrade { 

    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Text" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 

    // Textfield1 

    UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)]; 

    [utextfield becomeFirstResponder]; 

    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; 

    utextfield.leftView = paddingView; 
    utextfield.leftViewMode = UITextFieldViewModeAlways; 

    utextfield.placeholder = @"Subject"; 
    [utextfield setBackgroundColor:[UIColor whiteColor]]; 
    utextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    [alertview addSubview:utextfield]; 


    // Textfield2 

    UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 90.0, 25.0)]; 

    ptextfield.placeholder = @"Another Subject"; 
    ptextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    ptextfield.textAlignment = NSTextAlignmentCenter; 

    [ptextfield setBackgroundColor:[UIColor whiteColor]]; 
    [alertview addSubview:ptextfield]; 

    // Textfield3 

    UITextField *ctextfield = [[UITextField alloc] initWithFrame:CGRectMake(161.0, 85.0, 27.0, 25.0)]; 

    ctextfield.placeholder = @"3"; 
    [ctextfield setBackgroundColor:[UIColor whiteColor]]; 
    ctextfield.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter; 
    ctextfield.textAlignment = NSTextAlignmentCenter; 
    [alertview addSubview:ctextfield]; 


    // Label1 

    UILabel *telt = [[UILabel alloc] initWithFrame:CGRectMake(121.0, 85.0, 40.0, 25.0)]; 

    telt.text = @"Text"; 
    telt.textColor = [UIColor whiteColor]; 
    telt.backgroundColor = [UIColor clearColor]; 
    [alertview addSubview:telt]; 

    // Label2 

    UILabel *keermee = [[UILabel alloc] initWithFrame:CGRectMake(199.0, 85.0, 100.0, 25.0)]; 

    keermee.text = @"more text"; 
    keermee.textColor = [UIColor whiteColor]; 
    keermee.backgroundColor = [UIColor clearColor]; 
    [alertview addSubview:keermee]; 

    [alertview show]; 

} 

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


    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

    if ([title isEqualToString:@"Add"]) 
    { 
     UITextField *field = (UITextField *)[[alertView subviews] lastObject]; 
     NSLog (@"%@", field.text); 

    } 




} 

此代碼3個文本框和2個標籤在UIAlertView中。

With UITextField *field = (UITextField *)[[alertView subviews] lastObject];我收到了「更多文本」,因爲您詢問最後一個對象,所以這是真實的。

現在是我的問題如何獲得所有UITextfield的內容不僅僅是標籤?

回答

1
for (UIView *view in [alertView subviews]){ 
    if ([view isMemberOfClass:[UITextField class]]){ 
     UITextField *textField = (UITextField *)view; 

     NSLog(@"%@", textField.text); 

    } 
} 
+0

感謝您的答覆,但我應該把在'//做你的code'拿到這是擺在文本框內容?當我嘗試使用NSLog時,我得到了3次相同的日誌。 – user1883396

+0

UITextField * textField =(UITextField *)view; –

+0

如果您有3個文本框,此方法將執行3個TIN。 –

1

指定標籤並檢索它們。

myTextField.tag = 100; 

然後找到它使用此功能:

myTextField = [view subviewWithTag: 100];