使用导航栏时如何将值从 view2 返回到 view1

发布于 2024-09-03 12:35:07 字数 182 浏览 3 评论 0原文

我是 iPhone 开发新手。使用导航栏时,如何将字符串值从 view2 传递到 view1。通过使用pushviewcontroller,我可以将字符串值从view1到view2到view3到...传输没有问题,但是当我使用导航栏的“后退”按钮返回到以前的视图时,我无法保存字符串值。 我需要你的帮助来解决这个问题。

提前致谢

I am New to the iPhone Development. How can i carry a string value from view2 to view1 when using navigation bar. I have no problem in carrying string values from view1 to view2 to view3 to....by using pushviewcontroller But when i come back to previous views using Back button of navigation bar, I cannot able to hold string values.
I need your help in solving this issue.

Thanks In Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2024-09-10 12:35:07

如果将当前类的引用传递给下一个类并使用此引用更改值,则可以轻松完成此操作。

喜欢:
要推送的类。

@interface B:UIViewController{
   id delegate;
}
@property (nonatomic,retain) id delegate;
@end

@implementation B
@synthesize delegate;

-(void)methodA{
  [delegate setNewString2:@"Madhup"];
}

@end

您所在的班级 B:

@interface A:UIViewController{
   NSString *newString;
}
@property (nonatomic,retain)  NSString *newString;
@end


 @implementation A
 @synthesize newString
- (void)method2{
     B *newBObject = .......;
     [newBObject setDelegate:self];
     [self.navigationController pushViewCo.......];
  }
 @end

希望这会有所帮助。

This thing can be done easily if the pass the reference of the current class to the next class and change the values by using this reference.

Like:
The class that is to be pushed.

@interface B:UIViewController{
   id delegate;
}
@property (nonatomic,retain) id delegate;
@end

@implementation B
@synthesize delegate;

-(void)methodA{
  [delegate setNewString2:@"Madhup"];
}

@end

The class from which you are pushing B:

@interface A:UIViewController{
   NSString *newString;
}
@property (nonatomic,retain)  NSString *newString;
@end


 @implementation A
 @synthesize newString
- (void)method2{
     B *newBObject = .......;
     [newBObject setDelegate:self];
     [self.navigationController pushViewCo.......];
  }
 @end

Hope this helps.

時窥 2024-09-10 12:35:07

有不止一种方法可以做到这一点。以下是一些:

  1. 您可以通过导航控制器的 viewControllers 属性访问导航堆栈中的所有视图控制器: self.navigationController.viewControllers

  2. 您可以通过 ParentViewController 属性到达前一个视图控制器(即将当前控制器推送到导航堆栈的那个) : self.parentViewController

  3. 您可以使用委托模式,其中前一个(父)视图控制器将是当前(子)控制器的委托。

  4. 您可以在父控制器中保留对子控制器的引用(保留)。

在前 3 个中,子控制器将负责将数据传递给父控制器。在第四种情况下,父级会在释放子级之前从子级获取数据。

There's more than one way to do this. Here are a few:

  1. You can access all the view controllers in the navigation stack via the navigation controller's viewControllers property: self.navigationController.viewControllers

  2. You can reach the previous view controller (i.e. the one that pushed the current controller onto the navigation stack) via the parentViewController property: self.parentViewController

  3. You can use the delegate pattern, where the previous (parent) view controller would be the current (child) controller's delegate.

  4. You can keep a reference (retain) to the child controller in the parent controller.

In the first 3, the child controller would be responsible for handing the data to the parent controller. In the 4th, the parent would get the data from the child before releasing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文