【问题标题】:How to use object of two class in each other in xcode?如何在xcode中相互使用两个类的对象?
【发布时间】:2013-12-04 06:45:34
【问题描述】:

我有两个班级:

  • ServiceProviderCompleteProfile
  • 单选按钮。

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

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

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

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

按钮初始化

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

【问题讨论】:

  • 使用协议或通知
  • 你能输入代码sn-p吗?
  • @mobiSpace 请检查一下。
  • @wuiii 如何将协议用于 IBAction 方法...
  • 我意识到您是 SO 新手,这就是我帮助格式化的原因。我会正确阅读您的问题并尝试回答。

标签: ios objective-c


【解决方案1】:

正常模式是使用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
}

【讨论】:

  • @property (nonatomic, assign) id 委托;我必须在@interface 中写一行???
  • 詹姆斯我的应用程序。在某一时刻也崩溃了……我认为你可以修复它……你能帮我吗?
  • 你应该发布另一个问题。你应该有崩溃日志,你认为它在哪里破坏以及你已经尝试修复它。
  • 好的,当我发布该问题时,我会在这里通知您。我有那个日志的截图......
猜你喜欢
  • 1970-01-01
  • 2010-11-13
  • 2021-09-19
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 2015-04-06
相关资源
最近更新 更多