如何在xcode中使用两个类的对象?

时间:2013-11-20 10:20:15

标签: ios objective-c

我有两个班级:

  • ServiceProviderCompleteProfile
  • 单选按钮。

通过导入RadioButton中的ServiceProviderCompleteProfile类,我可以创建单选按钮。

我已声明所有与单选按钮相关的方法。

现在,我想在选择ServiceProviderCompleteProfile时在RadioButton实例中启用文本框。为此,我也在ServiceProviderCompleteProfile导入RadioButton

我已经编写了下面的代码,但ServiceProviderCompleteProfile s对象没有响应。

按钮初始化

- (id)initWithFrame:(CGRect)frame andOptions:(NSArray *)options andColumns:(int)columns
{
    self.radioButtons = [[NSMutableArray alloc] init];
    if (self = [super initWithFrame:frame])
    {
        // Initialization code
        int framex = 0;
        framex = frame.size.width / columns;
        int framey = 0;
        framey = frame.size.height / ([options count] / (columns));
        int k = 0;

        for(int i = 0; i < ([options count] / columns); i++)
        {
            for(int j = 0;j < columns; j++)
            {
                int x = framex*0.20;
                int y = framey*0.20;
                UIButton *btTemp = [[UIButton alloc]initWithFrame:CGRectMake(framex*j+x, framey*i+y, framex/2+x, framey/2+y)];
                [btTemp addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                btTemp.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                [btTemp setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
                [btTemp setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                btTemp.titleLabel.font =[UIFont systemFontOfSize:11.f];
                btTemp.titleLabel.numberOfLines=1;
                btTemp.titleLabel.lineBreakMode=NSLineBreakByWordWrapping;
                btTemp.titleLabel.textAlignment=NSTextAlignmentLeft;
                [btTemp setTitle:[options objectAtIndex:k] forState:UIControlStateNormal];
                btTemp.tag=j+1;
                [self.radioButtons addObject:btTemp];
                [self addSubview:btTemp];
                k++;
            }
        }
    }
    return self;
}

按钮操作

-(IBAction) radioButtonClicked:(UIButton *) sender
{
    for(int i = 0; i < [self.radioButtons count]; i++)
    {
        [[self.radioButtons objectAtIndex:i] setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
    }

    genderButtonIndex=[sender tag];

    [sender setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateNormal];
    ServiceProviderProfileViewController *svc=[[ServiceProviderProfileViewController alloc]init];

    if([sender tag] == 3)
    {
        NSLog(@"%d",genderButtonIndex);
        svc.txtGratuity.text=@"h";
        svc.txtGratuity.userInteractionEnabled=YES;
    }
    else
    {
        svc.txtGratuity.userInteractionEnabled=NO;
    }
}

1 个答案:

答案 0 :(得分:2)

正常模式是使用protocoldelegate pattern

在RadioButton标题中,添加协议和实现该协议的委托:

@protocol RadioButtonDelegate <NSObject>

@required
-(void) buttonWasActivated:(int) buttonTag;

@end

...

@property (nonatomic, assign) id<RadioButtonDelegate> delegate; //Delegates should always be assign

在实现中,您应该向IBAction添加回调

-(IBAction) radioButtonClicked:(UIButton *) sender
{
    ...
    [_delegate buttonWasActivated:sender.tag];
    ...
}

在ServiceProviderProfileViewController中,在单选按钮视图上设置委托

//Header
@interface ServiceProviderProfileViewController : UIViewController <RadioButtonDelegate>

//Wherever you init radioButtonView
radioButtonView.delegate = self;

//Add a method to implement the protocol
-(void) buttonWasActivated:(int) buttonTag
{
    if (buttonTag == 3) //enable text box
}