如何检查两个对象(UIButtons)是否相同

时间:2012-06-20 23:44:06

标签: ios uitableview tags uibutton

首先关闭标签不会工作。我这样说是因为我创建了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最后我用它来捕获抛出的异常。

        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; frame =(86 110; 32 30); opaque = NO; layer = CALayer:0x6c914f0

2012-06-20 20:24:01.568 TableView [12557:f803] UIButton:0x6ea8ad0; frame =(86 110; 32 30); opaque = NO; tag = 6; layer = CALayer:0x6e746e0

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

3 个答案:

答案 0 :(得分:5)

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

要了解详情,请转至:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html

并检查为isEqual写的内容:

答案 1 :(得分:0)

我不确定,在这个方法中, - (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;
}

答案 2 :(得分:0)

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

相关问题