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

标签 iphone ios objective-c delegates protocols

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

相关文章:

ios - layoutAttributesForElementsInRect 的无限循环

iphone - 应用程序在某些国家/地区崩溃

iphone - 如何在 iPhone 中列出 .EPUB 文件?

iphone - iOS 上的用户体验流程

沙盒环境中的 iOS ApplePay

iphone - iOS 7 上的 UILabel 自动调整大小错误

iphone - 将 UILabel 添加到 UITableView - iphone

ios - 如何在 Xcode 中为 ios 部署设置 Irrlicht 游戏引擎

ios - Xcode iOS : UITabBar update objects while switching to another tab

iphone - 如何在没有导航 Controller 的情况下更改 iPhone 中按钮单击的 View ?