vivek bhoraniya
vivek bhoraniya

Reputation: 1535

How to use object of two class in each other in xcode?

I have two classes:

By importing the RadioButton class in ServiceProviderCompleteProfile I can create radio button.

I have declared all method related to radio button in it's own class.

Now I want to enable a textbox in my ServiceProviderCompleteProfile instance when a RadioButton is selected. To achieve this, I am importing ServiceProviderCompleteProfile in RadioButton too.

I have written the code below but ServiceProviderCompleteProfiles object is not responding.

Button Initialisation

- (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;
}

Button Action

-(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;
    }
}

Upvotes: 1

Views: 138

Answers (1)

James Webster
James Webster

Reputation: 32066

The normal pattern is to use a protocol and the delegate pattern.

In your RadioButton header, add a protocol and a delegate implementing that protocol:

@protocol RadioButtonDelegate <NSObject>

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

@end

...

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

In the implementation, you should add a callback to the IBAction

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

And in your ServiceProviderProfileViewController, set the delegate on the radio button view

//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
}

Upvotes: 2

Related Questions