作爲驗證的一種形式,是否有任何方法可以防止按下「確定」按鈕時警報視圖被解除?防止UIAlertView退出
場景:我在alertview中有2個用戶名/密碼的文本字段。如果兩者都是空的並且用戶按下「確定」,我不希望警報被解除。
作爲驗證的一種形式,是否有任何方法可以防止按下「確定」按鈕時警報視圖被解除?防止UIAlertView退出
場景:我在alertview中有2個用戶名/密碼的文本字段。如果兩者都是空的並且用戶按下「確定」,我不希望警報被解除。
我不相信你實際上需要傳遞任何按鈕名稱。只要拿出你的確定按鈕字符串,並將其保留爲「無」。
你這樣做是錯誤的,你應該根據輸入啓用和禁用提交按鈕。首先你必須進入按鈕。這很容易,只需創建無按鍵的警報,創建一個獨立的按鈕,並把它添加到對話框:
[alert addButtonWithTitle:@"OK"];
UIButton *submitButton = [[alert subviews] lastObject];
[submitButton setEnabled:…];
然後你必須設置一個委託的文本框,並啓用或禁用時域更改按鈕:
- (BOOL) textField: (UITextField*) textField
shouldChangeCharactersInRange: (NSRange) range
replacementString: (NSString*) string
{
int textLength = [textField.text length];
int replacementLength = [string length];
BOOL hasCharacters = (replacementLength > 0) || (textLength > 1);
[self setButtonsAreEnabled:hasCharacters];
}
// Disable the ‘Return’ key on keyboard.
- (BOOL) textFieldShouldReturn: (UITextField*) textField
{
return NO;
}
當然,你應該將所有這些包裝到一個單獨的類中,這樣你就不會搞砸你的調用代碼。
iOS 5引入了一個新屬性UIAlertView
來處理這個問題。
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
Apple documentation on UIAlertView.
添加新UIAlertViewDelegate
方法來處理該按鈕的啓用/禁用。
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
像iTunes的密碼控件? Apple會使用UIAlertView嗎? – 2009-12-22 17:29:09