为什么我的自定义协议没有被调用? Objective-C

问题描述:

我有2个VC,其中一个用户在其中写入数据,使用textfield或pickerview,以及我在tableview中显示数据的主屏幕。问题是,当我点击完成按钮,它可能会调用委托,但在第一个VC,实施,它不会收到任何电话。我该怎么办?我已经2天试图找到这个bug,并且没有发生。谢谢你的帮助。为什么我的自定义协议没有被调用? Objective-C

2VC.h

@protocol AddCountryDelegate <NSObject> 

@required 

-(void)didCreateCountry:(CountryClass*)country; 

@end 

@interface AddCountryViewController : UIViewController 

@property (assign, nonatomic) id<AddCountryDelegate> customDelegate; 

2VC.m

- (IBAction)actionDone:(id)sender { 

    NSString* itemName = self.itemNameTextField.text; 
    NSString* countryName = self.countryNameTextField.text; 
    NSDate *countryDate = [datePickerView date]; 
    NSString *daysLeftString = [NSString stringWithFormat:@"%@ days", self.daysLeftTextField.text]; 

    NSInteger daysLeftInt = [daysLeftString integerValue]; 

    NSNumber *daysLeft = [NSNumber numberWithInteger:daysLeftInt]; 

    NSString *paymentMethod = self.paymentMethodTextField.text; 

    CountryClass* newCountryItem = 
    [[CountryClass alloc] initWithCountryName:countryName countryLogo:@"" itemName:itemName countryDate:countryDate daysLeft:daysLeft andPaymentMethod:paymentMethod]; 

    [self.customDelegate didCreateCountry:newCountryItem]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 


} 

最后一日VC实现:

-(void)didCreateCountry:(CountryClass *)country{ 

    //Add new country-itemCountry - UPDATING OUR MODEL DATA 

    NSMutableArray *mutableCountries = [self.countries mutableCopy]; 

    [mutableCountries addObject:country]; 

    self.countries = [mutableCountries copy]; 

    //UPDATING THE VIEW 

    [self.tableView reloadData]; 

    NSLog(@"Created"); 

    [self saveCountriesToDisc]; 

} 

-(void)saveCountriesToDisc{ 

    NSMutableArray* convertedCountries = [NSMutableArray array]; 

    //Converting the model to dictionary 

    for(CountryClass *country in self.countries) 
    { 
     NSDictionary *convertedCountry = [shop countriesToDictionary]; 
     [convertedCountries addObject:convertedCountry]; 
    } 

    //NOW convertedShows CONTAINS ONLY STRINGS, NUMBERS, 
    //ARRAYS AND DICTIONARY 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = paths.firstObject; 

    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"countriesListData.plist"]; 

    //NOW TELL THE ARRAY TO SAVE 
    [convertedCountries writeToFile:destinationPath atomically:YES]; 

    NSLog(@"Saved"); 

    [self.tableView reloadData]; 

} 

我初始化我的委托对prepareforsegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue 
       sender:(id)sender 
{ 


     if ([segue.identifier isEqualToString:@"AddSegue"]) { 

      AddCountryViewController* addCountryVC = 
      segue.destinationViewController; 
      addCountryVC.customDelegate = self; 

    } 
+0

你在哪里初始化委托?我想你的代表是无 –

+0

我想我在Segue上初始化:编辑主要问题。不是这样吗? – Sergio

+0

这部分被称为? –

在VC1中,您是否将customDelegate设置为self

也应您的代理设置为weakassign

+0

我想我在Segue上初始化:编辑主要问题。不是这样吗?关于我在互联网上阅读的弱点 - 几乎相同,我已经尝试过,但没有任何反应 – Sergio

+0

我改变为弱尝试,但仍然无法正常工作。我是否需要初始化第一个viewdidload或anithing类似的委托? – Sergio

+0

不,你做了什么应该工作,第一个VC.h你有? – ozd