我使用委托和协议创建了一个简单的TableView应用程序,它在ios 5中工作但是当我更新我的xcode时,我在ios 6中不起作用。 代码是这样的:
在儿童观点中:
@class AddItemViewController;
@class CheckListItem;
@protocol AddItemViewControllerDelegate <NSObject>
- (void)addItemViewControllerDidCancel: (AddItemViewController *)controller;
- (void)addItemViewController:(AddItemViewController *)controller didFnishAddingItem:(CheckListItem *)item;
@end
@interface AddItemViewController : UITableViewController <UITextFieldDelegate>
//declare an property
@property (nonatomic, assign) id <AddItemViewControllerDelegate> delegate;
in childview.m:
- (void)addItemViewControllerDidCancel: (AddItemViewController *)controller{
//i do something here
}
in parentview.h:
#import "AddItemViewController.h"
@interface CheckListViewController : UITableViewController <AddItemViewControllerDelegate>
-(IBAction)addItem;
@end
in parentview.m:
- (void)addItemViewControllerDidCancel:(AddItemViewController *)controller
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"AddItem"]){
UINavigationController *navigationController = segue.destinationViewController;
AddItemViewController *controller = (AddItemViewController *)navigationController;
controller.delegate = self;
NSLog(@"perform prepare for segue");
}
}
这段代码有什么问题吗? 感谢
答案 0 :(得分:0)
在子视图中,您必须调用[delegate addItemViewControllerDidCancel:(AddItemViewController *)controller],而不是仅调用该方法。
如果你改变它,它应该工作:)