2014-02-24 14 views
1

當單擊操作表上的cancelButtonTitle時,我想清除所有文本框;單擊操作表(iOS)的取消按鈕時,將所有文本框設置爲空白

這裏是我的動作片代碼:

- (IBAction)cancelButtonPressed:(id)sender { 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
            initWithTitle:@"Are you sure?" 
            delegate:self 
            cancelButtonTitle:@"No Way!" 
            destructiveButtonTitle:@"Yes, I’m Sure!" 
            otherButtonTitles:nil]; 
    [actionSheet showInView:self.view]; 
} 

回答

0

也許你還沒有實現UIActionSheet委託:

#pragma mark - UIActionSheetDelegate 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == actionSheet.cancelButtonIndex) { 
     //Clear textfiled at here 
     self.firstNameField.text = @""; 
    } 
    else{ 
     //Do somethings for @"Yes, I’m Sure!" 
    } 

} 
+0

如何omatic,你可以創建UIView的一個類別,並實現以下遞歸方法我清除文本字段,然後如果語句,和其他設置什麼都不做............我有@property(弱,非原子)IBOutlet UITextField * firstNameField; – Artan

+0

@ user3344907我編輯了代碼。試試。 – simalone

+0

非常感謝你,如何在戒備...我似乎無法找到相同的方法...想清除按鈕「確定」的文本字段...這裏是警報方法...-(IBAction)saveButtonPressed: (ID)發送方{ UIAlertView中*警報= [[UIAlertView中的alloc] initWithTitle:@ 「新聯繫人已保存」 消息:消息 委託:自 cancelButtonTitle:@ 「確定」 otherButtonTitles:無]; [alert show]; } – Artan

0

除了simalone的回答,其中規定您將需要執行以下操作工作表的代表方法:

#pragma mark - UIActionSheetDelegate 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == actionSheet.cancelButtonIndex) { 
     //Clear textfileds at here 
    } 
    else{ 
     //Do somethings for @"Yes, I’m Sure!" 
    } 
} 

如果您想要某些東西如果你需要同時清除所有TextViews,你可以添加以下上述方法

UIView+ClearTextFields.h 

-(void)clearAllTextFields; 




UIView+ClearTextFields.m 

-(void)clearAllTextFields 
{ 
    if ([self isKindOfClass:[UITextField class]]) 
    { 
     [(UITextField*)self setText:nil]; 
    } 

    for (UIView* subview in self.subviews) 
    { 
     [subview clearAllTextFields]; 
    } 
} 

if ([self isKindOfClass:[UITextView class]]) 
    { 
     [(UITextView*)self setText:nil]; 
    } 
相關問題