2013-11-29 38 views
-1

我想在City.m控制器中創建的AddCity.m控制器中使用委託方法。該方法是我的程序不在委託方法內

City.m

- (void)EditCityController:(id)controller didEditItem:(id)item 
{ 
    if(item) 
    { 
     NSDictionary *d = (NSDictionary *)item; 
     [self.model addNewCity:[d valueForKey:@"cityName"] forProvince:self.ro inMayor:[d valueForKey:@"mayorName"] inPopulation:[[d valueForKey:@"population"]intValue] inYearEstablished:[[d valueForKey:@"yearEstablished"]intValue]]; 
     [self.model saveChanges]; 

    } 

    // Dismiss the modal view controller 
    [controller dismissViewControllerAnimated:YES completion:nil]; 
} 

City.h

@interface Cities : 
UITableViewController<NSFetchedResultsControllerDelegate, EditCityDelegate> 

然後在AddCity.hi有

@protocol EditCityDelegate; 

@interface AddCity : UIViewController<UITextFieldDelegate> 

@property (nonatomic, assign) id <EditCityDelegate> delegate; 

@protocol EditCityDelegate <NSObject> 

- (void) EditCityController:(id)controller didEditItem:(id)item; 

@end 

我從AddCity.m調用它(目前正在嘗試取消按鈕)。

- (IBAction)cancel:(id)sender { 
    [self.delegate EditCityController:self didEditItem:nil]; 
} 

當我把一個斷點發生的事情是它擊中self.delegate線,但它並沒有進入EditCityController方法是什麼我做錯了City.m controller.Any想法?

+1

你可以檢查'self.delegate'的值嗎?如果它爲零,Objective-C將默默跳過方法調用。 – dpassage

+0

不,那似乎不是問題 – user2433194

+0

你是如何在AddCity.m中初始化self.delegate的? – Joshua

回答

0

我假設你叫AddCity.m在City.m和使用presentViewController

AddCity *addcityVC = [AddCity new]; 
addcityVC.delegate = self; // initialize the delegate here 
[self presentViewController:addCityVC animated:YES completion:nil]; 

目前它需要初始化委託回自我,因此它可以叫你需要

希望這方法有幫助