2009-12-26 68 views
0

我有五個字段的數字輸入,每個這些字段必須在一定的數字範圍內。例如,Field One應該高於40且低於100. Field Two應該高於1.25且低於5.5。剩下的就是你的想法。UIAlert文本輸入

我能夠製作UIAlertView,但只能用於按下按鈕來計算每個字段。如果最終用戶輸入了五個字段中的每一個的錯誤數據,他們將看到五個不同的警報,並且不是真正的用戶界面友好。

我想讓用戶輸入數字,當用戶進入下一個領域時,評估他們的輸入,然後發送警報視圖。這樣用戶在輸入另一個字段之前就會自動知道。我認爲這對UI會更容易。但是,我不知道做到這一點的最佳方法,我只是在學習iPhone SDK和ObjC。

這裏是我用於我的buttonPressed按鈕的代碼,正如你所看到的那樣,每個五(本例中爲四個)字段,將它們分配給一個變量,創建兩個測試UIAlerts,然後執行一些數學運算輸出答案。請記住我在這裏一個的n00b .....

//calculate total of variables when user clicks button 
-(IBAction) submitYourName; 
{ 
    /********************************************************* 
    declare our variables to strings 
    ********************************************************/ 
    NSString *userNameOne = txtUserName.text; 
    float numOne = [userNameOne intValue]; 
    NSString *userNameTwo = txtUserName2.text; 
    float numTwo = [userNameTwo intValue]; 
    NSString *userNameThree = txtUserName3.text; 
    float numThree = [userNameThree intValue]; 
    NSString *userNameFour = txtUserName4.text; 
    float numFour = [userNameFour intValue]; 

    /********************************************************* 
    allow error handling for our numbers 
    ********************************************************/ 
    if(numOne < 40 || numOne > 100) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"User Error" 
           message:@"Your age must be at least 40 years old and less than 100 years old" 
           delegate:nil 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 

    if(numTwo < 40 || numTwo > 100) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"User Error" 
           message:@"Your age must be at least 40 years old and less than 100 years old" 
           delegate:nil 
           cancelButtonTitle:@"OK" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 


    /********************************************************* 
    calculate our mathematics 
    ********************************************************/ 
    float one = 3; 
    float main = numOne * one; 


    /********************************************************* 
    produce answer 
    ********************************************************/ 
    lblUserTypedName.text = [[NSString alloc] initWithFormat: @"you input %2.2f %2.2f %2.2f %2.2f", main, numTwo, numThree, numFour]; 
} 

回答

0

我想說調用UITextFieldDelegate的:

- (void)textFieldDidEndEditing:(UITextField *)textField;

方法,並檢查它的輸入驗證。

順便說一句,爲什麼你會顯示很多警報?我會建議創建一個NSString,將通過每個if並添加類似「字段1的輸入無效」。等等,並最終顯示一個UIAlertView的所有細節。 這將是用戶友好和國際海事組織比你目前的解決方案更好。

這只是我的頭腦,但我希望這會對你有所幫助。 〜Natanavra。

+0

我很困惑。我是否使用UITextFieldDelegate創建輸入處理或創建NSString? – HollerTrain 2009-12-29 01:54:17

+0

當用戶從textField移動到另一個時,將調用UITextFieldDelegate方法,允許您在輸入後單獨檢查每個字段。 NSString創建正在堆疊您的消息,然後在一個Alert中顯示所有內容。 – natanavra 2009-12-29 14:28:48

+0

我是否將textFieldDidEndEditing連接到每個字段?那麼它會檢查每個字段? – HollerTrain 2010-01-03 00:00:20