2015-06-14 40 views
1

我有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

這部分被稱爲? –

回答

0

在VC1中,您是否將customDelegate設置爲self

也應您的代理設置爲weakassign

+0

我想我在Segue上初始化:編輯主要問題。不是這樣嗎?關於我在互聯網上閱讀的弱點 - 幾乎相同,我已經嘗試過,但沒有任何反應 – Sergio

+0

我改變爲弱嘗試,但仍然無法正常工作。我是否需要初始化第一個viewdidload或anithing類似的委託? – Sergio

+0

不,你做了什麼應該工作,第一個VC.h你有? – ozd

相關問題