没有直接导航时,代理没有被调用

时间:2012-10-05 14:46:55

标签: iphone ios ios5 delegates protocols

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

ThirdViewController是一个UITableViewController。

SecondViewControllerThirdViewController具有自定义协议。它们都使用各自的代表将一些值传递给FirstViewController

问题是SecondViewController成功将值传递给FirstViewController,但ThirdViewController无法执行此操作。

是不是因为SecondViewController直接从FirstViewController导航,所以它可以传递它,而ThirdViewControllerFirstViewController没有关联,所以它是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];
}

1 个答案:

答案 0 :(得分:0)

您正在设置ThirdViewController。{m}中的SecondViewController的委托给自己

objthirdView.delegate=self;

这意味着ThirdViewController的代表是SecondViewController而不是FirstViewController。在[{1}}中保留对FirstViewController的引用,并将其作为代理人使用。

SecondViewController

假设无法从objthirdView.delegate=myFirstViewController; 弹出到ThirdViewController,您可能会滥用FirstViewController通过委托方法将数据传回SecondViewController实现。但我不推荐它 - 它不是一个好的设计。