0

我创建了三个视图控制器,FirstViewControllerSecondViewController& ThirdViewControllerFirstViewController推,SecondViewController同样SecondViewControllerThirdViewController

ThirdViewController是一个 UITableViewController。

SecondViewController具有ThirdViewController自定义协议。他们都传递了一些价值来FirstViewController使用他们各自的代表。

问题是SecondViewController成功地将值传递给FirstViewController,但ThirdViewController无法做到这一点。

是不是因为SecondViewController直接从 导航FirstViewController,所以可以通过,ThirdViewController没有连接,FirstViewController所以不能通过数据?

任何帮助,将不胜感激。

编辑:

FirstViewController只需按下SecondViewController按钮动作。

SecondViewController.h

@protocol secondViewDelegate <NSObject>

-(void)didCompleteSending:(NSArray *)array;

@end


@interface secondView : UIViewController
@property (nonatomic,strong) NSArray *footballPlayers;
@property (assign) id<secondViewDelegate>delegate;
- (IBAction)btnAction:(id)sender;
- (IBAction)btnGoToThirdView:(id)sender;

SecondViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

footballPlayers = [[NSArray alloc]initWithObjects:@"Rooney",@"Van Persie",@"Ronaldo",@"Kagawa", nil];
}

- (IBAction)btnAction:(id)sender {
[delegate didCompleteSending:footballPlayers];
}
- (IBAction)btnGoToThirdView:(id)sender {
thirdView *objthirdView = [[thirdView alloc]init];
objthirdView.delegate=self;
[self.navigationController pushViewController:objthirdView animated:YES];
}

同样,我有 for ThirdViewController

FirstViewController.h -(void)didSendDetailsFromThirdView:(NSString *)colors{ NSLog(@"selected string is = %@",colors); }

-(void)didCompleteSending:(NSArray *)array{
  for (NSString *str in array) {
    NSLog(@"%@",str);
  }
 }

-(void)didSendDetailsFromThirdView:(NSString *)colors不会从“ThirdViewController”调用。

第二次编辑

- (IBAction)btnGoToThirdView:(id)sender {
FirstViewController *objFirstViewController = [FirstViewController alloc]init];
thirdView *objthirdView = [[thirdView alloc]init];
objthirdView.delegate=objFirstViewController;
[self.navigationController pushViewController:objthirdView animated:YES];
}
4

1 回答 1

0

ThirdViewController您正在将SecondViewController.m中的委托设置为 self

objthirdView.delegate=self;

这意味着 the 的代表ThirdViewControllerSecondViewController而不是FirstViewController。保留对FirstViewControllerin 的引用SecondViewController并将其用作这样的委托。

objthirdView.delegate=myFirstViewController;

假设不可能从那里弹出,ThirdViewControllerFirstViewController可能会滥用将SecondViewController数据传递回FirstViewController您已经实现的通过委托方法。但我不会推荐它——它不是一个好的设计。

于 2012-10-05T15:08:52.610 回答