【问题标题】:How to check if two objects (UIButtons ) are the same如何检查两个对象(UIButtons)是否相同
【发布时间】:2012-06-23 04:33:46
【问题描述】:

第一关标签不起作用。我这样说是因为我为特定单元格创建了 4 个按钮,它们都具有相同的标签,即 indexPath.row = tag。

在我的 TableViewCellForRowAtIndexpath 中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"courseCell";

//Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//step 2: If there are no cell to reuse, create a new one
if (cell == nil){
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
...


    //-------------Creation of Custom Buttons-------------// 

    //-----img = "radioOn.png"-----//
    //----img2 = "radioOff.png"----//

    //----RadioButtonA----//
    ...

    radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];

    [radioButtonA addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    radioButtonA.tag=indexPath.row;

    //----End RadioButtonA----//

    //----RadioButtonB----//

    radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
    [radioButtonB addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
   radioButtonB.tag =indexPath.row;
...

    //----End RadioButtonB----//

    //----RadioButtonC----//

    radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
    [radioButtonC addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
    radioButtonC.tag = indexPath.row;
...

    //----End RadioButtonC----//

    //----RadioButtonNA----//

    radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
    radioButtonNA.tag = indexPath.row;
    [radioButtonNA addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];

    ...

    //----End RadioButtonC----//

    //---------End of Radio Button Creations---------//

    //---------UIStepper & StepperLabel Creation-----//




    [cell.contentView addSubview:radioButtonA];
    [cell.contentView addSubview:radioButtonB];
    [cell.contentView addSubview:radioButtonC];
    [cell.contentView addSubview:radioButtonNA];

//Step4: Return the cell
return cell;
}

#pragma mark - Buttons

- (void)radioButtonClicked:(UIButton *)sender
 {

UIButton *myButton = sender;


// This Method and all the ones similar to this method is created to handle the UITouchUpInsideEvent that the user sends when pressing the radioButtons A-NA.

[radioButtons addObject:sender];

// Create an instance(object) of class NSIndexPath called indexPath and set its value the indexPath of the cell the user is currently in.
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [myTableView indexPathForCell:cell];


// Initialize two unique variables in order to check if the buttons being clicked are being referenced in the correct index. 
int row = indexPath.row;
NSLog(@"Button is in row %d", row);

...

}

-(IBAction)button:(UIButton*)sender{
  ...
@try {
    for (i=0; i<8; i++) {

        if ([credits count ] ==0) {
            break; 
        }

这里是我试图访问我在单元格中创建的单选按钮的地方。我想做的是这个

if([credits objectAtIndex:i]) == radioButtonA{

做事。我不说 == [radioButtonA 标签] 的原因是因为我还有其他三个按钮都具有相同的标签。如果您阅读了代码,您就会明白为什么以这种方式设置标签。 }

我要的是 1 帮助,2 是否有另一种方法来检查两个按钮,即对象是否相等,而不必依赖它们的标签。

不用担心 Try catch finally 我是用它来捕捉抛出的异常。

        if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:@"A"])  {
            NSLog(@"radioA is in array");
            creditHours+=[[valueArray objectAtIndex:i]doubleValue];
            gradeEarned+=(GradeA.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
            NSLog(@"%f",gradeEarned);
            continue;
        }

        if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:@"B"]) {
            NSLog(@"radioB is in array");
            creditHours+=[[valueArray objectAtIndex:i]doubleValue];
            gradeEarned+=(GradeB.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
            continue;
        }

        if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:@"C"]){
            NSLog(@"radioC is in array");
            creditHours+=[[valueArray objectAtIndex:i]doubleValue];
            gradeEarned+=(GradeC.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
            continue;
        }

        if([credits objectAtIndex:i]== radioButtonNA){
            NSLog(@"boboboobbobob");
            continue;
        }
    }
}
@catch (NSException *exception) {
    NSLog(@"NSException Caught");
    NSLog(@"Name: %@",exception.name);
    NSLog(@"Reason: %@", exception.reason);
}
@finally {
    NSLog(@"in finally block");
}

//        if ([credits objectAtIndex: i] == defaulter) {
//                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Failed to select all grades" message:[NSString stringWithFormat:@"Your grade selections have been reset"] delegate:self cancelButtonTitle:@"great"otherButtonTitles:nil];
//                        [alert show];
//                        [alert release];
//            [self refreshArray];
//        }


NSLog(@"%f",gradeEarned);

if (gradeEarned == 0) {
    textLabel.text= [NSString stringWithFormat:@"%f",gradeEarned];
}else {
    NSLog( @"boob");
    sum= (gradeEarned)/(creditHours);
    NSLog(@"%f",sum);
    textLabel.text= [NSString stringWithFormat:@"%f",sum];
    //[self refreshArray];
}

}

更多信息这里是日志...
NSLog(@"%@",[credits objectAtIndex:i]); NSLog(@"%@",radioButtonA);

      THE First output is the log of the [credits object atIndex:i]  

UIButton: 0x6c91430;帧 = (86 110; 32 30);不透明=否;层 = CALayer: 0x6c914f0

2012-06-20 20:24:01.568 TableView[12557:f803] UIButton: 0x6ea8ad0;帧 = (86 110; 32 30);不透明=否;标签 = 6;层=CALayer:0x6e746e0

如您所见,UIBUtton 不同,因此 == 运算符不起作用

【问题讨论】:

  • == 应该可以正常工作。你有错误吗?如果是这样,错误是什么?堆栈跟踪?
  • 同意上面的评论,具体来说,==应用于对象会测试地址是否相等,所以如果它是被不同变量引用的同一个对象,它将在那个地址。跨度>
  • 这里是日志... NSLog(@"%@",[credits objectAtIndex:i]); NSLog(@"%@",radioButtonA);第一个输出是 [credits object atIndex:i] 2012-06-20 20:22:14.219 TableView[12536:f803] > 第二条日志是radioButtonA 2012-06-20 20:22:14.220 TableView[12536:f803] >
  • 所以在每一行你有 4 个正确的按钮......例如,你有多少行(有这 4 个 btns)?
  • 我有 8 行,每行有 4 个按钮以及标签等

标签: ios uitableview tags uibutton


【解决方案1】:

当检查两个对象时,你不能使用 ==,你必须使用 [objectA isEqual:objectB],如果这两个对象相同,则答案将为 YES,否则为 NO。

要了解更多信息,请访问:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html

并检查为 isEqual 写的内容:

【讨论】:

  • 对象 [objectA isEqual:objectB] 在我的程序中永远不会相等...单击按钮时,发送者被放入数组中,并且发送者(UIButton)每次都被更改程序运行。如果这是我尝试了所有我能想到的解决方案的解决方案,我就不会发布这个问题,所以我在这里发布以获得帮助。谢谢你,即使你的方法行不通。
  • 正确!此外,如果您正在比较相同类型的对象,您可以使用这些更专业的函数:'isEqualToString:, isEqualToArray` 等。
【解决方案2】:

我不确定,在方法上, - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 只有当单元格为 nil 时才应添加按钮。就像这样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"courseCell";

    //Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //step 2: If there are no cell to reuse, create a new one
    if (cell == nil)
    {
         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         cell.selectionStyle = UITableViewCellSelectionStyleGray;
         //-------------Creation of Custom Buttons-------------// 

         //-----img = "radioOn.png"-----//
         //----img2 = "radioOff.png"----//

         //----RadioButtonA----//
          ...

         radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];

         [radioButtonA addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
         radioButtonA.tag=indexPath.row;

         //----End RadioButtonA----//

         //----RadioButtonB----//

         radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
         [radioButtonB addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
         radioButtonB.tag =indexPath.row;
         ...

//----End RadioButtonB----//

//----RadioButtonC----//

         radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
         [radioButtonC addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
         radioButtonC.tag = indexPath.row;
         ...

//----End RadioButtonC----//

//----RadioButtonNA----//

           radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
           radioButtonNA.tag = indexPath.row;
         [radioButtonNA addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];

...

//----End RadioButtonC----//

//---------End of Radio Button Creations---------//

//---------UIStepper & StepperLabel Creation-----//




       [cell.contentView addSubview:radioButtonA];
       [cell.contentView addSubview:radioButtonB];
       [cell.contentView addSubview:radioButtonC];
       [cell.contentView addSubview:radioButtonNA];
 }
     ...

    //Step4: Return the cell
    return cell;
}

【讨论】:

  • 使用自定义单元格怎么样?
【解决方案3】:

我已经知道这一点,但是,这篇文章的目的是看看他们是否还有其他方式。我还使用了 isMemberOfClass:[UIButton class] 来进一步缩小范围。最好和最有效的方法是使用对象的标签进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多