How return a value from view2 to view1 when using

2019-03-30 12:16发布

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

2条回答
The star\"
2楼-- · 2019-03-30 12:29

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.

查看更多
Root(大扎)
3楼-- · 2019-03-30 12:46

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.

查看更多
登录 后发表回答