0

在导航控制器中有两个视图控制器:

PhoneNumbersTVC> 保存由NewPhoneNumberTVC NewPhoneNumberTVC> 添加电话号码的控制器添加的电话号码列表

当我点击取消时,NewPhoneNumberTVC我想回到PhoneNumbersTVC一个代表团,如下所示。

电话号码TVC.h

#import <UIKit/UIKit.h>
#import "NewPhoneNumberTVC.h"

@interface PhoneNumbersTVC : UITableViewController <NewPhoneNumberTVCDelegate>

@end

电话号码TVC.m

- (void)saveBtnWasTappedOnNewPhoneNumberTVC:(NewPhoneNumberTVC *)newPhoneNumberTVC
{
    NSLog(@"saveBtnWasTappedOnNewPhoneNumberTVC");
    [newPhoneNumberTVC.navigationController popViewControllerAnimated:YES];
}

- (void)cancelBtnWasTappedOnNewPhoneNumberTVC:(NewPhoneNumberTVC *)newPhoneNumberTVC
{
    NSLog(@"cancelBtnWasTappedOnNewPhoneNumberTVC");
    [newPhoneNumberTVC.navigationController popViewControllerAnimated:YES];
}

新电话号码TVC.h

#import <UIKit/UIKit.h>

@class NewPhoneNumberTVC;

@protocol NewPhoneNumberTVCDelegate <NSObject>

- (void)saveBtnWasTappedOnNewPhoneNumberTVC:(NewPhoneNumberTVC *)newPhoneNumberTVC;
- (void)cancelBtnWasTappedOnNewPhoneNumberTVC:(NewPhoneNumberTVC *)newPhoneNumberTVC;

@end


@interface NewPhoneNumberTVC : UITableViewController

@property (weak, nonatomic) id <NewPhoneNumberTVCDelegate> delegate;
@property (strong, nonatomic) NSManagedObjectContext *managedOC;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

- (IBAction)saveBtnTapped:(UIBarButtonItem *)sender;
- (IBAction)cancelBtnTapped:(UIBarButtonItem *)sender;

@end

新电话号码TVC.m

- (IBAction)cancelBtnTapped:(UIBarButtonItem *)sender
{
    NSLog(@"cancelBtnTapped");
    self.phoneNumberTextField.text = @"";
    self.phoneKindTextField.text = @"";
    [self.delegate cancelBtnWasTappedOnNewPhoneNumberTVC:self];

}

当我点击取消时,NewPhoneNumberTVC我看到上面的方法cancelBtnTapped触发但delegation不起作用,没有method执行PhoneNumbersTVC。视图没有消失,我没有看到:cancelBtnWasTappedOnNewPhoneNumberTVCon console

4

1 回答 1

0

如果你想cancelBtnWasTappedOnNewPhoneNumberTVC:调用这个方法,你肯定需要将你的对象的委托设置NewPhoneNumberTVC为一个PhoneNumbersTVC对象。例如,PhoneNumbersTVC.m中应该存在一些代码:

NewPhoneNumberTVC *myNewPhoneNumberTVC = [[NewPhoneNumberTVC alloc] init];
myNewPhoneNumberTVC.delegate = self;

您可以按以下方式登录以验证委托是否设置成功“

新电话号码TVC.m

- (IBAction)cancelBtnTapped:(UIBarButtonItem *)sender
{
NSLog(@"cancelBtnTapped");
self.phoneNumberTextField.text = @"";
self.phoneKindTextField.text = @"";
NSLog(@"self.delegate :%@",self.delegate);
[self.delegate cancelBtnWasTappedOnNewPhoneNumberTVC:self];
}
于 2013-03-09T16:05:25.550 回答