我使用委托(delegate)方法在 View Controller 之间传递数据。这是行不通的。
@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
}
关于iphone - 委托(delegate)方法不工作 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917540/