开发者

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

开发者 https://www.devze.com 2023-01-01 11:32 出处:网络
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 开发者_StackOverflowstring values from view1 to view2 to

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 开发者_StackOverflowstring 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号