0

Here is my code.

@interface MasterViewController : UIViewController{

}
-(void) initCallService;
@end

@implementation MasterViewController
-(void) initCallService{

}
@end

other class which is sub class of above

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


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

I need whenever the title changes in btntitle_changed method of DetailViewController class to send it to MasterViewController's initCallService method. I don't know how can i achieve this with the class hierarchy. Can anyone guide me for this ? Valid answer will be appreciated.

Pete
  • 57,112
  • 28
  • 117
  • 166
Viral Narshana
  • 1,855
  • 2
  • 21
  • 45
  • Please edit your question. What exactly do you want to do ? – Petar Mar 21 '13 at 09:39
  • needed a property `NSString *buttonTitle` in MasterVC, `[super setButtonTitle:btn.title]` – Anoop Vaidya Mar 21 '13 at 09:41
  • I need the btn(which is defined in DetailViewController) title in MasterViewController's "initCallService" method – Viral Narshana Mar 21 '13 at 09:43
  • Thanks annop vaidya for answering. But i wont it without declaring nsstring in MasterVC. is it possible ? – Viral Narshana Mar 21 '13 at 09:48
  • @ViralNarshana: then where are you planning to store the value. As you must have seen my answer. – Anoop Vaidya Mar 21 '13 at 10:15
  • @Anoop Vaidya can i do this with objective c categories ? – Viral Narshana Mar 21 '13 at 10:27
  • @ViralNarshana: category will add method, even a property can be added, but that is against your policy!!! In the method what and where will you store the string, do you need to pass the string to something like textfield, button title, xml, json, core data etc? – Anoop Vaidya Mar 21 '13 at 10:36
  • I hope NSNotificationCenter may work for you. – Exploring Mar 21 '13 at 10:56
  • Your concept/class-design is wrong. If you need something from a subclass in a superclass, you are violating inheritance laws. A correct design would implement the button as a property on the superclass (which needs the title) and also define a method `btnChanged` which could send a notification or use an instance-variable to save the title. Subclasses would then have to call `[super btnChanged]` in their implementation. – patric.schenke Mar 21 '13 at 09:49

3 Answers3

0

You need a property in MasterViewController say NSString *buttonTitle

-(void) btntitle_changed{
    [btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal];
    [super setButtonTitle:btn.title];
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

It is possible by setting a Protocol

in your DetailViewController.h

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

and set a property as

      @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]
       }

In your MasterViewContoller.h add TitleChange delegate as

    @interface MasterViewController : UIViewController<TitleChange>{

    }

In you MasterViewContoller.m while initiating DetailViewController

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

this is how you Protocol and delegates which is widely used for passing data and just go through the basics of it

Lochana Ragupathy
  • 4,320
  • 2
  • 25
  • 36
  • the reason is the same i stated in the other protocol-answer: it's a good idea for general inter-object-communication, but not for class-hierarchies. the subclass would essentially end up being its own delegate. – patric.schenke Mar 21 '13 at 09:59
0

Just change your -(void)initCallService to -(void)initCallServiceWithButtonTitle:(NSSTring*)title. Then change btntitle_changed function as follows:

-(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'
}
Petar
  • 2,241
  • 1
  • 24
  • 38