我创建了三个视图控制器,即FirstViewController
,SecondViewController
& ThirdViewController
。 FirstViewController
推送SecondViewController
,同样SecondViewController
推ThirdViewController
。
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];
}
是:
ThirdViewController
类似地,我有FirstViewController.h
。
-(void)didCompleteSending:(NSArray *)array{
for (NSString *str in array) {
NSLog(@"%@",str);
}
}
- (void)didSendDetailsFromThirdView:(NSString *)colors {
NSLog(@“选择的字符串是=%@”,颜色);
}
-(void)didSendDetailsFromThirdView:(NSString *)colors
{'1}}不会从'ThirdViewController'调用。
第二次编辑
- (IBAction)btnGoToThirdView:(id)sender {
FirstViewController *objFirstViewController = [FirstViewController alloc]init];
thirdView *objthirdView = [[thirdView alloc]init];
objthirdView.delegate=objFirstViewController;
[self.navigationController pushViewController:objthirdView animated:YES];
}
答案 0 :(得分:0)
您正在设置ThirdViewController
。{m}中的SecondViewController
的委托给自己
objthirdView.delegate=self;
这意味着ThirdViewController
的代表是SecondViewController
而不是FirstViewController
。在[{1}}中保留对FirstViewController
的引用,并将其作为代理人使用。
SecondViewController
假设无法从objthirdView.delegate=myFirstViewController;
弹出到ThirdViewController
,您可能会滥用FirstViewController
通过委托方法将数据传回SecondViewController
实现。但我不推荐它 - 它不是一个好的设计。