2013-11-20 15 views
1

我有兩個類:如何在xcode中使用兩個類的對象?

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

按鈕操作

-(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); 
     [email protected]"h"; 
     svc.txtGratuity.userInteractionEnabled=YES; 
    } 
    else 
    { 
     svc.txtGratuity.userInteractionEnabled=NO; 
    } 
} 
+0

使用協議或不使用協議通知 –

+0

你可以把代碼片段? – Swapnil

+0

@mobiSpace請檢查它。 – vivek

回答

2

正常模式是使用protocoldelegate pattern

在你的單選按鈕頭,加上協議和實施協議的代表:

@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 
} 
+0

@property(nonatomic,assign)id delegate;我必須在@interface中寫入? – vivek

+0

是的,這是正確的。 –

+0

謝謝你詹姆斯...做了我的代碼,它的工作.... – vivek

相關問題