为什么我的班级没有被叫?

时间:2012-03-06 14:24:01

标签: objective-c ios class uitextfield uiactionsheet

每次退出文本字段时,都应该出现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];
    } 
}

2 个答案:

答案 0 :(得分:0)

行动看起来很好。确保已在Interface Builder中链接editDidEnd操作。

答案 1 :(得分:0)

我建议你先从H文件中实现UITextFieldDelegate协议

我还会使用以下代码 - 确保textField已连接到您希望在完成时运行的操作(使用“界面”构建器)

#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{

}