Iphone 视图控制器之间消息传递中的dismissModalViewController

Iphone 视图控制器之间消息传递中的dismissModalViewController,iphone,Iphone,我只是想知道这是否是iphone/ipad中不同视图之间传递数据或消息的正确方式 我有两个视图控制器,第一个视图控制器和第二个视图控制器。我在我的两个ViewController中都有一个NSString*消息作为属性,我用以下方式设置它 在FirstViewController.h中,我导入了SecondViewController.h类。我有一个iAction,当用户点击第一个视图上的按钮时会调用它 -(IBAction)ShowSecondView { SeondViewCont

我只是想知道这是否是iphone/ipad中不同视图之间传递数据或消息的正确方式

我有两个视图控制器,第一个视图控制器和第二个视图控制器。我在我的两个ViewController中都有一个NSString*消息作为属性,我用以下方式设置它

在FirstViewController.h中,我导入了SecondViewController.h类。我有一个iAction,当用户点击第一个视图上的按钮时会调用它

-(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 我有一个iAction,当用户点击第二个视图上的按钮时会调用它

-(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不是显示第二视图控制器的第一视图控制器。它是FirstViewController类的新实例。您不需要创建这个firstView实例,而只需要关闭第二个view控制器。但您还需要在第二个视图控制器中创建指向第一个视图控制器的指针,以便在其中设置数据

在第二个视图控制器的标题中:

#import "FirstViewController.h"
FirstViewController *firstView;
@property (retain, nonatomic) 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];
}
-(IBAction)GoBack { 
    firstView.message = @"Presented from Second View";
    [self dismissModalViewControllerAnimated:YES];
}
在第二个视图控制器的实现中:

#import "FirstViewController.h"
FirstViewController *firstView;
@property (retain, nonatomic) 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];
}
-(IBAction)GoBack { 
    firstView.message = @"Presented from Second View";
    [self dismissModalViewControllerAnimated:YES];
}
在第一个视图控制器中:

#import "FirstViewController.h"
FirstViewController *firstView;
@property (retain, nonatomic) 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];
}
-(IBAction)GoBack { 
    firstView.message = @"Presented from Second View";
    [self dismissModalViewControllerAnimated:YES];
}
在第二个视图控制器中:

#import "FirstViewController.h"
FirstViewController *firstView;
@property (retain, nonatomic) 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];
}
-(IBAction)GoBack { 
    firstView.message = @"Presented from Second View";
    [self dismissModalViewControllerAnimated:YES];
}
顺便说一句,我经常使用其他方式在ViewController之间进行通信


顺便说一句,上面的代码并没有经过测试。如果有任何问题,我深表歉意。

我的建议是使用delegates,这将导致“循环导入”问题。我将以任何方式使用通知或委托。谢谢您的时间。它不会导致“循环导入”问题。在这种方法中,第一个ViewController导入第二个,但第二个ViewController不导入第一个(它会自行关闭以返回到原始的第一个)。