简体   繁体   中英

Delegate method not working ios

I used delegate mehod for pass data between view controllers. This is not working.

@protocol PassCountry <NSObject>

@required
- (void) setPickedCountry:(NSString *)pickedCountry;
@end

@interface SelectCountryViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> {
    id <PassCountry> delegate;
}

@property (copy) NSString *pickedCountry;
@property (retain) id delegate;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent (NSInteger)component {
    pickedCountry = [self.countries objectAtIndex:row];
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.countries.count;
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self delegate] setPickedCountry:pickedCountry];
}
  #import <UIKit/UIKit.h>
  @protocol PassCountry <NSObject>

  @required
   - (void) setPickedCountry:(NSString *)pickedCountry;
   @end

  @interface secondViewViewController : UIViewController<UIPickerViewDelegate,  UIPickerViewDataSource>
  {
      id <PassCountry> delegate;

     IBOutlet UIButton *aButton;
  }

 @property (copy) NSString *pickedCountry;
 @property (assign) id<PassCountry> delegate; // for delegate use assign don't retain

 // in another class you are creating instance of this class

  secondViewViewController *secController = [[secondViewViewController alloc]init];
  secController.delegate = self;//check this added or not
  [self presentViewController:secController animated:YES completion:nil];

  //and implementation of deleagte method  
  - (void) setPickedCountry:(NSString *)pickedCountry
   {
       // do some stuff

   }

Try this::

.h File

@protocol delegateTextSize <NSObject>

@optional
 -(void)selectedTextSize:(double)textSize;
@end

@interface CustomFontSizeCell : UITableViewCell

@property (nonatomic,retain) id delegateTextSize;
-(IBAction)changeSize:(id)sender;

@end

.m File

-(IBAction)changeSize:(id)sender
{
    [delegateTextSize selectedTextSize:app.selectedFontSize];
}

Where to use,

.h File

Controller <delegateTextSize>

.m File

-(void)selectedTextSize:(double)textSize
{
}

Hopefully, this will work

Thanks.

Firstly, delegate instance can not be retained. Secondly, delegate should be synthesized using "@synthesize delegate" before invoke the method [self delegate].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM