检查managedObjectContext中的单个属性是否为* not * nil

时间:2014-09-24 10:22:49

标签: objective-c core-data uibutton nsmanagedobject nsmanagedobjectcontext

我有ServicesViewControllerUIButtons。用户可以点击按钮,并可以选择通过UIAlertView UIAlertViewStylePlainTextInput输入“更多详细信息”。如果用户选择不添加“更多详细信息”,则UIButton状态保持selectedkeyYes中保存为managedObjectContext。如果他们选择添加“更多详细信息”,则key仍会保存为Yes,而“更多详细信息”key会保存为他们输入的string

然后用户可以导航到另一个ViewController并在他们喜欢时返回ServicesViewController。我需要在viewDidLoadServicesViewController keys上的managedObjectContext方法中执行检查。如果UIButton key保存为“是”,则必须突出显示UIButton状态。如果“更多详细信息”key不是nil,则按钮必须将状态更改为disabled,并且还可以更改用户反馈的颜色。

选择按钮的代码:

- (IBAction)asbBtnPressed:(UIButton *)sender {
    if ([sender isSelected]) {
        [sender setSelected:NO];
        [_managedObjectNGLS setValue:@"" forKey:@"asb"];
    } else {
        [sender setSelected:YES];
        [_managedObjectNGLS setValue:@"Yes" forKey:@"asb"];
    }

    UIAlertView *qConfirm = [[UIAlertView alloc]initWithTitle:@"Additional Information"
                                                       message:@"Would you like to give additional information?"
                                                      delegate:self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"Proceed", nil];
    qConfirm.alertViewStyle = UIAlertViewStylePlainTextInput;
    qConfirm.tag = 2;
    if (sender.isSelected == YES) {
        [qConfirm show];
    }
}

如果用户输入“更多详细信息”,请禁用按钮并更改颜色:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // ASB
    if (alertView.tag == 2) {
        if (buttonIndex == 1) {
            NSString *asbDetails = [alertView textFieldAtIndex:0].text;
            [_managedObjectNGLS setValue:asbDetails forKeyPath:@"asbDetails"];
            if (asbDetails.length > 1) {
                [_asbBtn setSelected:NO];
                _asbBtn.backgroundColor = [UIColor colorWithRed:(55/255.0) green:(200/255.0) blue:(0/255.0) alpha:1];
                _asbBtn.enabled = FALSE;
            }
        }
    }

代码尝试检查nil值:

// If UIButton has been selected
if ([[_managedObjectNGLS valueForKey:@"asb"] isEqual: @"Yes"]) {
    _asbBtn.selected = YES;

    // If "Further Details" is empty
} else if ([_managedObjectNGLS valueForKey:@"asbDetails"] == nil) {
    // Do nothing
} else {
    // Else "Further Details" must be populated, so change colour & disable button
    _asbBtn.backgroundColor = [UIColor colorWithRed:(55/255.0) green:(200/255.0) blue:(0/255.0) alpha:1];
    _asbBtn.enabled = FALSE;
    NSLog(@"asbDetails not empty");
}

我意识到这不是最好的方法(主要是因为它不起作用!),理想我想通过执行{{1}来检查key是否不是nil测试,但这也不起作用。我不想走!=的道路,因为我有很多按钮和许多按键要测试,每个按钮必须保持独立,所以我认为我不能使用fetchRequest要么会改变所有按钮?如我错了请纠正我。提前谢谢!

1 个答案:

答案 0 :(得分:0)

经过一番研究,我不得不想出一些解决方法。可能有更好的解决方案,但现在可以使用:

- (void)isInterested {
    // If valueForKey is "yes", keep button in selected state
    // If ...Details key isn't empty, disable button & change colour

    // ASB
    if ([[_managedObjectNGLS valueForKey:@"asb"] isEqual: @"Yes"]) {
        _asbBtn.selected = YES;
    }
    NSString *asbDetails = [_managedObjectNGLS valueForKey:@"asbDetails"];
    if (asbDetails.length > 0) {
        _asbBtn.selected = NO;
        _asbBtn.backgroundColor = [UIColor colorWithRed:(55/255.0) green:(200/255.0) blue:(0/255.0) alpha:1];
        _asbBtn.enabled = FALSE;
        // NSLog(@"asbDetails is not empty");
    }

    // More buttons
}

我在isInterested ServicesViewController中调用viewDidLoad方法 - 这意味着每次加载此view时,isInterested方法都会执行必要的检查并将UIButton设置为正确的状态。