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

I am new to iPhone development. How can I transfer a string value from view2 to view1 when using the navigation bar. I have no problem moving string values ​​from view1 to view2 to view3 in ... using pushviewcontroller. But when I go back to previous views using the back button on the navigation bar, I can’t hold on to the string values. I need your help in solving this problem.

Thanks in advance

+4
source share
2 answers

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

How: The class to click.

@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 press 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.

+3
source

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

  • You can access all view controllers in the navigation stack through the property of the navigation manager ControlControllers: self.navigationController.viewControllers

  • You can contact the previous view controller (i.e. the one that pushed the current controller on the navigation stack) through the parentViewController property: self.parentViewController

  • You can use the delegate template where the previous (parent) view controller will be the current (child) controller.)

  • You can save the link (save) on the child controller in the parent controller.

In the first 3, the child controller will be responsible for transmitting data to the parent controller. In the 4th case, the parent will receive data from the child before releasing him.

+2
source

Source: https://habr.com/ru/post/1310841/


All Articles