dismissModalViewController在消息在视图控制器之间传递
我只是想知道这是否是在iphone/ipad中的不同视图之间传递数据或消息的正确方式。dismissModalViewController在消息在视图控制器之间传递
我有两个ViewControllers,FirstViewController和SecondViewController。我有一个NSString *消息作为我的ViewControllers中的一个属性,我通过以下方式进行设置。
在FirstViewController.h中,我导入了SecondViewController.h类。我有这个IBAction为被调用,当用户点击第一视图中的按钮
-(IBAction)ShowSecondView
{
SeondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
secondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
secondView.message = @"Presented from First View";
[self presentModalViewController:secondView animated:YES];
[secondView release];
}
在我SecondViewController.h,我导入类FirstViewController.h 我有这个IBAction为被调用,当用户点击一个按钮第二视图
-(IBAction)GoBack
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:[NSBundle mainBundle]];
firstView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
firstView.message = @"Presented from Second View";
[self presentModalViewController:firstView animated:YES];
[firstView release];
}
的消息被成功地在视图之间通过,但如果使用以关闭当前视图控制器返回到在父视图 [self dismissModalViewController]
,不传递该消息。
在GoBack的你的firstView alloc'd不是提出的第二个视图控制器的第一个视图控制器。这是FirstViewController类的新实例。而不是创建这个firstView实例,你只需要关闭第二个视图控制器。但是,您还需要在第二个视图控制器中创建一个指向第一个视图控制器的指针,以便您可以在其中设置数据。
在第二视图控制器的报头:
@synthesize firstView;
在第一视图控制器
:
-(IBAction)ShowSecondView {
SeondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
secondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
secondView.message = @"Presented from First View";
secondView.firstView = self;
[self presentModalViewController:secondView animated:YES];
[secondView release];
}
在
#import "FirstViewController.h"
FirstViewController *firstView;
@property (retain, nonatomic) firstViewController *firstView;
在第二视图控制器的实施
你的第二个视图控制器:
-(IBAction)GoBack {
firstView.message = @"Presented from Second View";
[self dismissModalViewControllerAnimated:YES];
}
顺便说一句,还有其他的方式viewcontrollers之间的沟通,我经常使用Notifications
顺便说一句,上面的代码是未经测试的直离开我的头。如果有任何问题,我们表示歉意。
这将导致“循环导入”问题。任何方式我会使用通知或代表。谢谢你的时间。 – 2012-02-29 11:17:09
它不会导致“循环导入”问题。在这种方法中,第一个ViewController导入第二个,但第二个不导入第一个(它会自动返回到原来的第一个)。 – ader 2012-02-29 11:26:02
我的建议是使用委托 – Bonny 2012-02-27 09:53:11