【问题标题】:I could not able to call one IBaction on click of alertView button单击 alertView 按钮时我无法调用一个 IBaction
【发布时间】:2015-08-20 10:34:24
【问题描述】:

我在这样的方法中声明了一个 alertView。

UIAlertView* alertView =[[UIAlertView alloc]initWithTitle:@"Check In Alert" message:@"Are you sure he/she is there?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
                        alertView.tag = sender;
                        [alertView show];

所以在 clickedButtonIndex 的 alertView 方法中我有这段代码。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if([alertView.title isEqualToString:@"Check In Alert"])
    {
        ClassRoomParticipant *clsrp;
        NSMutableArray *data = nil;
        data=[self.presentDataArray objectAtIndex:0];
        clsrp = (ClassRoomParticipant *)[data objectAtIndex:alertView.tag];
        NSLog(@"Do Nothing");
        if (buttonIndex == 0)
        {

            [clsrp setGetParcpntCheckInStatus : 2];// Checked in by Teacher

        }
        else if (buttonIndex == 1)
        {

            [clsrp setGetParcpntCheckInStatus : 3];// Not checked in by Teacher, still Absent

        }
        return;
    }
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"OK"])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if([title isEqualToString:@"Button 2"])
    {
        //NSLog(@"Button 2 was selected.");
    }
    else if([title isEqualToString:@"Button 3"])
    {
        //NSLog(@"Button 3 was selected.");
    }
}

我有这样的方法。

-(void)changeStudentAction:(id)sender
{
    CustomUIButton *button=sender;
    NSInteger flag = 0;
    if (button.selected)
    {
        [button setSelected:NO];
        [button setBackgroundImage:[UIImage imageNamed:@"Absent_on_coloured.png"] forState:UIControlStateNormal];
        flag=0;
    }
    else
    {
        [button setSelected:YES];
        [button setBackgroundImage:[UIImage imageNamed:@"Present_on_coloured.png"] forState:UIControlStateNormal];
        flag=1;
    }

    if (button.collectionIdentifier == ClsRmPrctPresntCollectionView)
    {
        [self ModifyDataArray:button.taggy flag:flag identifier:ClsRmPrctPresntCollectionView change:@"Button" toDate:nil section:button.section];
        [self modifyCollectionArray:PRESENT processingData:self.presentDataArray identifier:ClsRmPrctPresntCollectionView index:button.taggy section:button.section];
    }

    self.isAnyChangesHappened=YES;
}

我想在 alertView 按钮中调用这个 changeStudentAction 方法,单击“是”。当我在 buttonIndex==0 中调用这个方法时,它给了我错误。能否请您告诉我如何在警报视图中调用此方法。

【问题讨论】:

    标签: ios ios7


    【解决方案1】:

    1) 创建一个“CustomUIButton”的出口,其中“changeStudentAction”是行动

    2) 在警报视图中委托调用[self changeStudentAction:self.customUIbuttonOutlet]

    【讨论】:

      【解决方案2】:

      我猜它因为alertView.tag = sender; 声明而向您显示错误。确保sender 是整数。

      你也没有设置alertView的委托。试试这个:alertView.delegate = self;

      休息一切似乎都很完美。只需在- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 方法的正确块中调用changeStudentAction 方法即可。

      【讨论】:

      • 我已经为 alertView 设置了委托。现在我想在 alertView 中调用 changeStudentAction 方法。我可以这样调用 if (buttonIndex == 0) { [self changeStudentAction:(id)clsrp];但它正在崩溃。显示“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[ClassRoomParticipant isSelected]:无法识别的选择器发送到实例 0x7a7b7ac0”
      • 这是因为您的变量“clsrp”属于“ClassRoomParticipant”类。当您在“changeStudentAction”方法中将此变量映射到“CustomUIButton”时,这是无效的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      相关资源
      最近更新 更多