0

我正在尝试将 NSString 传递给另一个视图。

在我的第一个视图(是地图视图)中,我的代码是:

FirstView.m 文件:

导入“FlipsideViewController.h”

-(IBAction) displayDetails:(id) 发件人{

MyLocation *ann = [_mapView.selectedAnnotations objectAtIndex:([_mapView.selectedAnnotations count]-1)];

FlipsideViewController *flipsideViewController;
flipsideViewController=  [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(@"ann.name:%@", ann.name);
NSLog(@"details:%@", flipsideViewController.details);

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"details" bundle:nil];
    controller.delegate = self;     
  controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release]; 

}

并且 NSlog 打印正确的字符串。但是,当我尝试将该字符串传递给名为 details 的 SecondViewController 字符串时:

在我的 FlipsideViewController.h 中定义:

NSString *详细信息;

@property (nonatomic, strong) NSString *details;

在 FlipsideViewController.m 中

@synthesize 详细信息;//每当我输入日志时,它都会打印空值

值:NSLog(@" * ** * ** * ** * *** *detalli %@",detalli);

我的错在哪里?

4

3 回答 3

1

在 secondViewController 添加一个属性,一个 NSString 类型的保留属性添加属性请阅读以下内容

http://cocoacast.com/?q=node/103

请编辑此部分

FlipsideViewController *flipsideViewController;
flipsideViewController=  [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(@"ann.name:%@", ann.name);
NSLog(@"details:%@", flipsideViewController.details);

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"details" bundle:nil];
    controller.delegate = self;     
  controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release]; 

FlipsideViewController *flipsideViewController;
flipsideViewController=  [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(@"ann.name:%@", ann.name);
NSLog(@"details:%@", flipsideViewController.details);

flipsideViewController.delegate = self;     
flipsideViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:flipsideViewController animated:YES];
[flipsideViewController release];
于 2012-06-02T11:07:13.887 回答
1

试试这个步骤:- 1)首先你在 anotherViewController.h 中创建属性

@property (nonatomic, strong ) NSString *fetchStr;

2) 在 anotherViewController.m 中合成 NSString

@synthesize fetchStr;

3)在firstViewController中使用

anotherViewController *avc =  [[anotherViewController alloc]init];
avc.fetchStr = ann.name;

并在 anotherViewController 中打印(NSLog) fetchStr

于 2012-06-02T11:19:13.153 回答
0

做了注释

@property(nonatomic,retain) MyLocation *annotation;

然后将其传递给另一个视图控制器。

它会起作用的。

于 2012-06-02T12:37:42.740 回答