我有两节课:
通过在
RadioButton
中导入ServiceProviderCompleteProfile
类,我可以创建单选按钮。我已经在其自己的类中声明了与单选按钮相关的所有方法。
现在,当我选择了
ServiceProviderCompleteProfile
时,我想在RadioButton
实例中启用一个文本框。为此,我也将ServiceProviderCompleteProfile
导入RadioButton
中。我已经在下面编写了代码,但是
ServiceProviderCompleteProfile
的对象没有响应。按钮初始化
- (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;
}
}
最佳答案
正常模式是使用protocol和delegate 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
}
关于ios - 如何在Xcode中互相使用两个类的对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20093162/