2012-03-06 101 views
0

每當我退出一個文本框,UIActionsheet應該出現以及保存一些信息。當我退出文本字段時,程序退出,控制檯說它是一個無效的類。爲什麼這個代碼不工作?爲什麼我的班級沒有被打電話?

- (IBAction)classFieldDoneEditing:(id)sender 

- (IBAction)classFieldDoneEditing:(id)sender 
        withActionSheet:(UIActionSheet *)typeSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    self.indicatedClass = classField.text; 
    NSString *greeting = [[NSString alloc] 
          initWithFormat:@"%@", indicatedClass]; 
    labelClassTitle.text = greeting; 
    labelClassTitle.hidden = NO; 
    [greeting release]; 
    [sender resignFirstResponder]; 

    typeSheet = [[UIActionSheet alloc] 
           initWithTitle:@"Class types"delegate:self 
           cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
           otherButtonTitles:@"Core Class", @"Elective", nil]; 
    [typeSheet showInView:self.view]; 
    [typeSheet release]; 

    if (buttonIndex == 0) { 
     self.indicatedClass = classField.text; 
     NSString *indicatedString = indicatedClass; 
     NSString *greeting = [[NSString alloc]  
           initWithFormat:@"%@ meets 6 times per rotation",  indicatedString]; 
     labelClassTitle.text = greeting; 
     labelClassTitle.hidden = NO; 
     [greeting release]; 
     [indicatedClass release]; 
    } 

    else if (buttonIndex == 1) { 
     self.indicatedClass = classField.text; 
     NSString *indicatedString = indicatedClass; 
     NSString *greeting = [[NSString alloc] 
           initWithFormat:@"%@ meets 3 times per rotation", indicatedString]; 
     labelClassTitle.text = greeting; 
     labelClassTitle.hidden = NO; 
     [greeting release]; 
     [indicatedClass release]; 
    } 
} 

回答

0

動作看起來不錯。確保您已經鏈接了Interface Builder中的editingDidEnd操作。

+0

我已經鏈接了它 – mentorship 2012-03-06 14:51:51

0

我建議你開始在你的.h文件執行UITextFieldDelegate協議

還我會用下面的代碼 - 確保你的文本字段連接到你想上進行(使用界面生成器)運行的操作

#pragma mark - 
#pragma mark Text Field Delegate 
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    if ([textField canResignFirstResponder]) { 
     [textField resignFirstResponder]; 
    } 

    return YES; 
} 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ 
    // add your method here 

    return YES; 
} 
- (void)textFieldDidEndEditing:(UITextField *)textField{ 

} 
+0

這是我第一次編程,所以這段代碼對我來說沒什麼意義。當我實施它時,出現了很多錯誤。 – mentorship 2012-03-06 15:05:34

相關問題