在一个视图中,我们称它为 firstView 我创建了一个 secondView,如下所示,如果在 firstView 中发生某些事情,则将其推送:
SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
现在,当我在 secondView 中时,如果假设按下按钮,我想返回到 firstView 并将一个值从 secondView 传递回 firstView(假设从 secondView 到 firstView 的文本字段的整数值)。
这是我尝试过的:
@protocol SecondViewControllerDelegate;
#import <UIKit/UIKit.h>
#import "firstViewController.h"
@interface SecondViewController : UIViewController <UITextFieldDelegate>
{
UITextField *xInput;
id <SecondViewControllerDelegate> delegate;
}
- (IBAction)useXPressed:(UIButton *)sender;
@property (assign) id <SecondViewControllerDelegate> delegate;
@property (retain) IBOutlet UITextField *xInput;
@end
@protocol SecondViewControllerDelegate
- (void)secondViewController:(SecondViewController *)sender xValue:(int)value;
@end
并且在 m 文件中
- (IBAction)useXPressed:(UIButton *)sender
{
[self.delegate secondViewController:self xValue:1234]; // 1234 is just for test
}
然后在 firstView 我做了:
#import "SecondViewController.h"
@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {
}
@end
并实施:
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
现在,问题在于 FirstViewController 中的一个我收到警告“没有找到协议“SecondViewControllerDelegate”的定义,并且对于两个委托方法(上面的最后一段代码)根本没有被调用。有人可以告诉我怎么了?