【问题标题】:make "Next" Button disabled if no cell is checked如果未选中任何单元格,则禁用“下一步”按钮
【发布时间】:2012-04-23 09:09:53
【问题描述】:

感谢您的帮助!

我像这样更改了 Viewdidload,现在它对我来说工作正常!我正在获取位置并使用谓词来同步检查和按钮的状态

  - (void)viewDidLoad
  {
    [super viewDidLoad];
    app = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [app managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Standort" inManagedObjectContext:context];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ortcheck CONTAINS YES"];
    [request setPredicate:predicate];
    [request setEntity:entity];

    NSError *error = nil;
    NSArray *events = [context executeFetchRequest:request error:&error];    
    int i = events.count;

    if (i == 0) {

        myWeiter = [[UIBarButtonItem alloc] initWithTitle:@"Weiter" style:UIBarButtonItemStyleBordered target:self action:@selector(nextPressed:)];

        self.navigationItem.rightBarButtonItem = myWeiter;


         self.navigationItem.rightBarButtonItem.enabled = NO;
    }
    else {
        myWeiter = [[UIBarButtonItem alloc] initWithTitle:@"Weiter" style:UIBarButtonItemStyleBordered target:self action:@selector(nextPressed:)];

        self.navigationItem.rightBarButtonItem = myWeiter;


        self.navigationItem.rightBarButtonItem.enabled = YES;
    }

【问题讨论】:

  • 你可以创建一个直接映射到你的tableview单元格的布尔属性,当你选择单元格时,它的值将变为yes,反之亦然。现在基于这个 bool 变量,您还可以启用/禁用下一个按钮。
  • @RIP - 只有下一个按钮状态取决于所有单元格的值。 (例如,即使您取消选中一个单元格,但仍选中另一个单元格,它仍应启用)
  • @BinyaminSharet - 我的意思是跟踪所有单元格。就像你做的那样。 :)

标签: ios xcode core-data didselectrowatindexpath


【解决方案1】:

添加一个检查计数器作为实例成员,并将其初始化为0,每次检查增加,每次取消检查减少,每次检查后设置按钮:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // snip

    //if (cell.checkButton.hidden==YES){
    //    cell.checkButton.hidden=NO;
    //}else {
    //    cell.checkButton.hidden=YES;
    //}
    BOOL state = !cell.checkButton.hidden;
    cell.checkButton.hidden=state; // simpler
    self.counter += (state) ? 1 : -1;
    [nextButton setEnabled: counter > 0];

    // snap
}

【讨论】:

  • @Bashud - 遍历 viewDidLoad 中所有单元格的状态并更新计数器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 2014-11-02
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
相关资源
最近更新 更多