【发布时间】:2012-07-01 19:00:06
【问题描述】:
在一个视图中,我们称它为 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
}
然后在第一个视图中我做了:
#import "SecondViewController.h"
@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {
}
@end
并实施:
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
现在,问题在于 FirstViewController 中的一个我收到警告“没有找到协议“SecondViewControllerDelegate”的定义,并且对于两个委托方法(上面的最后一段代码)根本没有被调用。有人可以请告诉我怎么了?
【问题讨论】:
-
您确定委托!= nil?
-
我应该做什么以及在哪里确保代表不为零?
-
你设置了你的委托吗?像 secondVC.delegate = self;
标签: ios delegates uinavigationcontroller