0

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

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

3 回答 3

2
  #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

   }
于 2013-07-29T07:32:10.597 回答
1

尝试这个::

.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
{
}

希望这会奏效

谢谢。

于 2013-07-29T07:38:00.020 回答
1

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

于 2013-07-29T07:42:21.657 回答