我使用委托方法在视图控制器之间传递数据。这不起作用。
@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];
}
答案 0 :(得分: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
}
答案 1 :(得分: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
{
}
希望这会起作用
感谢。
答案 2 :(得分:1)
首先,不能保留委托实例。 其次,在调用方法[self delegate]之前,应使用“@synthesize delegate”合成委托。