繁体   English   中英

委托方法不起作用的iOS

[英]Delegate method not working ios

我使用委托方法在视图控制器之间传递数据。 这是行不通的。

@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

   }

尝试这个::

.h文件

@protocol delegateTextSize <NSObject>

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

@interface CustomFontSizeCell : UITableViewCell

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

@end

.m文件

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

在哪里使用

.h文件

Controller <delegateTextSize>

.m文件

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

希望这会起作用

谢谢。

首先,委托实例不能保留。 其次,在调用方法[自委托]之前,应使用“ @synthesize委托”来综合委托。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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