我正在尝试将NSString传递给另一个视图。
在我的第一个视图(是地图视图)中,我的代码是:
FirstView.m文件:
import“FlipsideViewController.h”
- (IBAction)displayDetails:(id)sender {
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的 在我的FlipsideViewController.h中定义了: NSString *详细信息; @property(非原子,强)NSString *详细信息; 在FlipsideViewController.m @synthesize详细信息; //每当我放入日志时,它都会打印空值 值:NSLog(@“ * ** * ** * ** * *** * detalli%@“,detalli); 我的错在哪里?
答案 0 :(得分: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];
答案 1 :(得分: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;
并在另一个ViewController中打印(NSLog)fetchStr
答案 2 :(得分:0)
制作注释
@property(nonatomic,retain) MyLocation *annotation;
然后将其传递给另一个视图控制器。
它会起作用。