简体   繁体   English

如何通过3个ViewController传递NSString?

[英]How do I pass an NSString through 3 ViewControllers?

hey, I'm currently using the iPhone SDK and I'm having trouble passing an NSString through 3 views 嘿,我目前正在使用iPhone SDK,无法在3个视图中传递NSString

I am able to pass an NSString between 2 view controllers but I am unable to pass it through another one. 我可以在2个视图控制器之间传递NSString,但无法通过另一个NSString传递。 My code is as follows... 我的代码如下...

    `- (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;
}

in the "ViewController 2" implementations I am able use "string1" in the title bar by doing the following.... 在“ 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;

    }

but I also have a NavBar Button on the right side that I would like to push a new view 但我在右侧也有一个NavBar按钮,我想按一下新视图

- (void)goToThirdView 
    { 
     ViewController3 *vc3 = [[ViewController3 alloc] initWithNibName:@"ViewController3" bundle:[NSBundle mainBundle]];

     [self.navigationController pushViewController:NESW animated:YES];
     vc3.string1 = string1 ;
     [vc3 release];
     vc3 = nil;
}

How do I pass on that same string to the third view? 如何将相同的字符串传递给第三个视图? (or fourth) (或第四)

What you have should work exceptthat you want to set the string in vc3 before you push it onto the stack to ensure it is present when the view and the nav bar draws. 除了在vc3中设置字符串,然后将其推入堆栈以确保在视图和导航栏绘制时出现该字符串之前,您应该执行的工作。 That is the way you have it in the vc2 that works. 这就是您在vc2中有效的方式。

However, as a matter of application design, it is poor practice to pass values directly between view controllers. 但是,作为应用程序设计的问题,在视图控制器之间直接传递值是一种不好的做法。 Ideally, you want your view controllers to be standalone and able to function regardless of which other controller did or did not precede it. 理想情况下,您希望视图控制器是独立的并且能够运行,而不管哪个控制器在其之前或之前没有。 (This becomes really important when you need to resume an app back to the point where it was interrupted.) If make the view controllers interdependent your app will grow tangled and complex as it grows larger. (当您需要将应用程序恢复到被中断的位置时,这变得非常重要。)如果使视图控制器相互依赖,则应用程序将变得越来越大,变得越来越复杂。

The best way to exchange data between views is to park the data in a universally accessible place. 在视图之间交换数据的最佳方法是将数据存放在可普遍访问的地方。 If it is application state information put it in user defaults or you can put in an attribute of the app delegate. 如果是应用程序状态信息,则将其设置为用户默认值,或者您可以放入应用程序委托的属性。 If it is user data, then it should go in a dedicated data model object (which is either a singleton or accessible through the app delegate.) 如果是用户数据,则应将其放入专用的数据模型对象(可以是单例,也可以通过应用程序委托访问)。

您可能会从我之前提出的问题中找到代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 iOS 上的 ViewController 之间传递 object? - How do you pass an object between ViewControllers on iOS? 无法在ViewControllers之间传递NSString,而其他对象可以 - Unable to pass NSString between ViewControllers, while other objects can 如何将引用从AppDelgate中的对象传递给ViewControllers - How can I pass referece from an object in AppDelgate to ViewControllers 如何在视图控制器之间传递数据 - How to pass data between viewcontrollers Xcode:如何将对象从一个viewControllers视图移动到另一个viewControllers视图? - Xcode: How do I move an object from one viewControllers view to another viewControllers view? 界面生成器。 如何用IB ViewController替换程序化ViewController? - Interface Builder. How do I Replace Programatic ViewControllers with IB ViewControllers? 在viewControllers之间切换时如何在UILabel中保存信息 - How do I save information in a UILabel when switching between viewControllers 如何将编程的 ViewController 与故事板中创建的新视图连接起来? - How do I connect programmed ViewControllers with new views created in storyboard? 如何在第x行修剪NSString? - How do I trim an NSString at line x? 如何从NSString转换为NSDate? - How do I convert from NSString to NSDate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM