从另一个视图控制器调用方法时,它变为dealloc&#d; d

时间:2014-07-11 02:54:42

标签: ios objective-c

我有两个视图控制器。我们将它们称为 listViewController mapViewController 。 listViewController首先显示,用户点击一个显示mapViewController的按钮。用户点击注释的按钮,这是我的问题开始的地方。

当用户点击mapViewController上的按钮时,我想调用方法listViewController并解除mapViewController。为此,我使用此代码:

mapViewController.m

listViewController* lvc = [[listViewController alloc] initWithNibName:nil bundle:nil];
[lvc getInformation:StringParameter];
[self dismissViewControllerAnimated:YES completion:nil];

然而,当执行getInformation时,似乎已经释放了listViewController类,因为我在listViewController上的viewDidLoad中初始化的所有对象现在都是零,包括self,这打破了一切。

我假设我在mapViewController中错误地创建了listViewController对象。我已经用nib为nibName尝试了相同的结果。

2 个答案:

答案 0 :(得分:1)

好的,让我说清楚一件事。在listViewController上,你showViewController一个mapViewcontroller对吗?并且您希望从mapViewController调用实例的getInformation。在您添加的代码中,您再次实例化listViewController。你有2个不同的listViewController实例。

答案 1 :(得分:1)

一种选择是使用委托模式:

MapViewController.h文件中:

@protocol MapViewControllerDelegate <NSObject>
-(void)dismissMe;
-(void)getInformation:(NSString *)stringParameter;

@end

@interface MapViewController : UIViewController
@property (weak)id <MapViewControllerDelegate> delegate;
@end

MapViewController.m文件中:

-(void)annotationButtonTap:(id)button
{
    [self.delegate getInformation:stringParameter];
    [self.delegate dismissMe];
}

ListViewController.h

#import "MapViewController.h"

@interface ListViewController : UIViewController <MapViewControllerDelegate>

-(void)dismissMe
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)getInformation:(NSString *)stringParameter
{
    //do whatever you plan with stringParameter
}

在您创建ListViewController.m实例的MapViewController文件中的某个位置:

mapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
mapViewController.delegate = self;   //make sure you do this
相关问题