Iphone 如何通过3个ViewController传递NSString?
嘿,我目前正在使用iPhone SDK,在通过3个视图传递NSString时遇到问题 我可以在两个视图控制器之间传递NSString,但无法通过另一个。我的代码如下Iphone 如何通过3个ViewController传递NSString?,iphone,objective-c,sdk,nsstring,Iphone,Objective C,Sdk,Nsstring,嘿,我目前正在使用iPhone SDK,在通过3个视图传递NSString时遇到问题 我可以在两个视图控制器之间传递NSString,但无法通过另一个。我的代码如下 `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)index`Path { NSString *string1 = nil; NSDictionary *dictionary = [listOfItem
`- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)index`Path {
NSString *string1 = nil;
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"items"];
string1 = [array objectAtIndex:indexPath.row];
//Initialize the detail view controller and display it.
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:[NSBundle mainBundle]];
vc2.string1 = string1;
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 release];
vc2 = nil;
}
在“ViewController 2”实现中,我可以通过执行以下操作来使用标题栏中的“string1”
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = string1;
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"icon_time.png"]
style:UIBarButtonItemStylePlain
//style:UIBarButtonItemStyleBordered
target:self
action:@selector(goToThirdView)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
}
但我也有一个导航栏按钮在右侧,我想推动一个新的看法
- (void)goToThirdView
{
ViewController3 *vc3 = [[ViewController3 alloc] initWithNibName:@"ViewController3" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:NESW animated:YES];
vc3.string1 = string1 ;
[vc3 release];
vc3 = nil;
}
如何将相同的字符串传递给第三个视图?(或第四个)除了要在vc3中设置字符串,然后再将其推到堆栈上,以确保在视图和导航栏绘制时该字符串存在外,您所拥有的其他功能都应该有效。这就是vc2的工作方式 但是,作为应用程序设计的一个问题,在视图控制器之间直接传递值是一种不好的做法。理想情况下,您希望视图控制器是独立的,并且能够正常工作,而不管前面有或没有其他控制器。(当你需要将一个应用程序恢复到被中断的位置时,这一点就变得非常重要了。)如果让视图变得更大,你的应用程序就会变得混乱和复杂
在视图之间交换数据的最佳方法是将数据停放在一个普遍可访问的位置。如果是应用程序状态信息,请将其放入用户默认值中,或者可以放入应用程序委托的属性中。如果它是用户数据,那么它应该放在一个专用的数据模型对象中(该对象可以是单例对象,也可以通过应用程序委托访问)。您可以从我前面询问的一个示例中找到代码示例