在 View 中,我们称其为firstView,我按如下方式创建了secondView,并在firstView中发生某些事情时推送它:
SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
现在,当我在第二个 View 中时,如果按下按钮,我想返回到第一个 View ,并将一个值从第二个 View 传递回第一个 View (假设从第二个 View 到第一个 View 的文本字段的整数值) )。
这是我尝试过的:
@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
}
然后在第一个 View 中我做了:
#import "SecondViewController.h"
@interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {
}
@end
并实现:
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
现在,问题是对于 FirstViewController 中的一个,我收到警告“未找到协议(protocol)“SecondViewControllerDelegate”的定义,对于两个,委托(delegate)方法(上面的最后一段代码)根本没有被调用。有人可以吗?请告诉我出了什么问题?
最佳答案
此行之后
SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
添加
secondVC.delegate = self;
也可以代替
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
你应该使用
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[sender popViewControllerAnimated:YES];
}
关于ios - 第一个 View 中的委托(delegate)方法不会从第二个 View 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11284831/