2009-01-03 38 views
2

我已經用於cocos2d遊戲引擎。這是我的代碼:如何在UIAlertView中從UITextField獲取值?

-(void)timed1: (id)sender { 
    UIAlertView *dialog = [[[UIAlertView alloc] init] retain]; 
    [dialog setDelegate:self]; 
    [dialog setTitle:@"Enter Time:"]; 
    [dialog setMessage:@" "]; 
    UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
    [dialog addSubview:nameField]; 
    [nameField setBackgroundColor:[UIColor whiteColor]]; 
    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 70.0); 
    [dialog setTransform: moveUp]; 
    [dialog addButtonWithTitle:@"Done"]; 
    [dialog addButtonWithTitle:@"Cancel"]; 

    nameField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    nameField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 

     //timeStatus is a int type global variable // 
    timeStatus =[nameField.text intValue]; //** this line not working**// 

    [nameField release]; 
    [dialog show]; 

} 

回答

0

發佈nameField後面,並從那裏獲取文本屬性。

2

你不能得到的值的原因是,該功能將退出之前,用戶類型的東西!當您調用[對話框顯示]時,對話框會顯示給用戶,然後他們可以輸入文本。

你應該等到用戶選擇一個按鈕,這將觸發您的委託功能,然後訪問文本字段屬性。

注意做到這一點,你要麼需要保留的指針UITextField對象您添加(簡單),或再次找到它的子視圖之間並投它。

相關問題