How return a value from view2 to view1 when using navigation bar

99封情书 提交于 2019-12-03 20:20:19

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!