gpt4 book ai didi

iphone - 委托(delegate)方法不工作 ios

转载 作者:行者123 更新时间:2023-11-28 19:08:34 25 4
gpt4 key购买 nike

我使用委托(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com