ios - 如何在 xcode 中相互使用两个类的对象?

标签 ios objective-c

我有两个类:

  • ServiceProviderCompleteProfile
  • 单选按钮。

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

我已经在它自己的类中声明了所有与单选按钮相关的方法。

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

我已经编写了下面的代码,但是 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;
}

按钮 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;
    }
}

最佳答案

正常模式是使用 protocoldelegate pattern .

在您的 RadioButton header 中,添加一个协议(protocol)和一个实现该协议(protocol)的委托(delegate):

@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 中,在单选按钮 View 上设置委托(delegate)

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

相关文章:

ios - 在iOS应用程序中解析protobuf文件

ios - 从另一个 ViewController 中移除 subview

ios - NSPredicate 带有 !=?

ios - Objective-C:修复方法中的内存管理

objective-c - Retina 显示来自 URL 的图像

iphone - 如何在Xcode中用+替换空格

html - iOS:从 NSString(一个 html 字符串)中剥离 <img...>

objective-c - 打开应用程序窗口

ios - SpriteKit,使用加速度计返回前景时, Sprite 移动得非常快

ios - 通过 layoutSubviews 自定义 UIAlertButton => 找不到 UIAlertButton 类