gpt4 book ai didi

ios - 第一个 View 中的委托(delegate)方法不会从第二个 View 中调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:14 25 4
gpt4 key购买 nike

在一个 View 中,我们称它为 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
}

然后在第一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com