我正在尝试在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.h中我有
@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行,但它不会进入City.m控制器中的EditCityController方法。任何关于我做错的想法?
答案 0 :(得分:0)
我假设您在City.m中调用AddCity.m并使用presentViewController
显示它AddCity *addcityVC = [AddCity new];
addcityVC.delegate = self; // initialize the delegate here
[self presentViewController:addCityVC animated:YES completion:nil];
您需要将委托初始化为self,以便它可以调用您需要的方法
希望这有帮助