为不同的视图控制器委派功能

时间:2015-12-15 10:23:40

标签: ios objective-c delegates

此图片http://postimg.org/image/3qo8x6lyv/

enter image description here

在AddContactViewController.m中(对于蓝色的)

-(void)viewWillDisappear:(BOOL)animated{
    [self passBD];
}
- (void)passBD {
    //some code

    [self.delegate passDataBack:p];
}

passDataBack在AddContactViewController.h中声明,并在MasterViewController.m(对于灰色的)和DetailViewController.m(对于yellow-ish)中定义。我对该计划进行了描述,但我发现:

当我从Master转到AddContact视图控制器时返回viewWillDisappear,执行调用passBD调用MasterViewController中定义的passDataBack

当我从Detail返回AddContact视图控制器时返回viewWillDisappear执行调用passBD但它不调用DetailViewController.m中定义的passDataBack

痕迹刚好过去了。当我命令+单击passDataBack时,它会显示三个选项Master,Detail和Add。为什么从一个VC执行它而不是从另一个VC执行呢?

编辑 DetailViewController.m

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

    if([segue.identifier isEqualToString:@"editContact"]){
        AddConatctViewController *passData = [segue destinationViewController];

    //some code
    }

}

MasterViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"addContact"]){
        self.addContactVC = segue.destinationViewController;
        self.addContactVC.delegate = self;
    }
}

编辑2 AddContactViewController.h

#import <UIKit/UIKit.h>
#import "PersonHolder.h" 
@protocol AddDataDelegate <NSObject>
@required
- (void) passDataBack:(PersonHolder *)data;
@end
@interface AddConatctViewController : UIViewController
@property (nonatomic,weak) id <AddDataDelegate> delegate;
@end

0 个答案:

没有答案