我已將自定義視圖添加到警報視圖。驗證自定義警報視圖
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier: @"MyView"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Submit", nil];
[alertView setValue: myViewController.view forKey:@"accessoryView"];
[alertView show];
myViewController.view
中有幾個文本框。警報視圖有兩個按鈕「提交」和「取消」。
輕敲「提交」按鈕,我需要在文本框來驗證輸入。如果輸入無效,我不想解除警報。例如。如果電子郵件無效,我會顯示另一個提示「電子郵件無效」。我該如何實現這個目標?在下面的方法
1)編寫驗證邏輯:我嘗試下面的事情到目前爲止
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
但警報視圖下執行此方法後解散。
2)繼承UIAlertView。我試圖重寫以下方法在子類UIAlertView中:雖然我點擊任何關於警報視圖按鈕
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
這種方法不會被調用。蘋果說,
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
我已經花了很多時間今天解決這個問題,但沒有運氣..!請幫幫我。
剛剛創建自己的AlertView,因爲alertview僅被用作是。您正在使用私人方法來設置您的自定義視圖,這可能會讓您的應用遭到拒絕。 – rckoenes
感謝您的建議@ rckoenes ..! –
我想你可以使用委託方法的東西。 (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; – BangOperator