Search code examples
iphonenavigationtableviewviewcontrollerpushviewcontroller

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


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


Solution

  • 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.