2013-03-21 75 views
0

这是我的代码。我如何获得超类目标的子类UIButton标题c

@interface MasterViewController : UIViewController{ 

} 
-(void) initCallService; 
@end 

@implementation MasterViewController 
-(void) initCallService{ 

} 
@end 

其它类,这是子类的上述

@interface DetailViewController : MasterViewController{ 
    IBOutlet UIButton *btn; 
} 
@end 


@implementation DetailViewController 
-(void) btntitle_changed{ 
    [btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal]; 
} 
@end 

我需要每当DetailViewController类的btntitle_changed方法的标题改变它发送到MasterViewControllerinitCallService方法。我不知道如何用类层次结构实现这一点。任何人都可以指导我吗?有效的答案将不胜感激。

+0

请编辑您的问题。你到底想做什么? – Petar 2013-03-21 09:39:43

+0

在MasterVC需要一个属性'NSString * buttonTitle','[super setButtonTitle:btn.title]' – 2013-03-21 09:41:14

+0

我需要MasterViewController的“initCallService”方法中的btn(在DetailViewController中定义)标题 – 2013-03-21 09:43:17

回答

0

你需要MasterViewController物业说NSString *buttonTitle

-(void) btntitle_changed{ 
    [btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal]; 
    [super setButtonTitle:btn.title]; 
} 
+0

感谢@Anoop Vaidya提供了这么好的帮助。 – 2013-03-21 12:44:27

0

有可能通过在DetailViewController.h

  @protocol TitleChange <NSObject> 
      -(void)initCallService:(NSString *)title; 
     @end 

设置协议

,并设置属性为

 @property(nonatomic,assign) id <TitleChange> delegate; 

     In DetailViewController.m 

    -(void) btntitle_changed 
     { 
     [btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal]; 
     [self.delegate initCallservice: btn.title] 
     } 

在你MasterViewContoller.h添加TitleChange委托作为

@interface MasterViewController : UIViewController<TitleChange>{ 

    } 

在你MasterViewContoller.m同时启动DetailViewController

DetailViewController *vc =[[DetailViewController alloc] :@"DetailViewController" bundle:nil]; 
    vc.delegate=self 
    [self presentViewController:vc animated:YES completion:nil]; 

这是怎么了协议,并委托其被广泛用于传递数据,只是去通过它的基础知识

+0

为什么被低估了 – 2013-03-21 09:53:41

+0

原因与我在其他协议中所说的一样 - 答案:对于一般的对象间通信而言,这是一个好主意,但不适用于类层次结构。子类本质上最终会成为它自己的代表。 – 2013-03-21 09:59:55

0

只需将您的-(void)initCallService更改为-(void)initCallServiceWithButtonTitle:(NSSTring*)title。 然后更改btntitle_changed功能如下:

-(void) btntitle_changed{ 
    [btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal]; 
    [super initCallServiceWithButtonTitle:btn.title]; 
} 

-(void)initCallServiceWithButtonTitle:(NSString*)title{ 
//do whatever you want with 'title' 
}