1

Possible Duplicate:
Passing Data between View Controllers

I have 2 modal views. The first modal view is used to edit quantity and price; the second modal view is used when we click the price textfield of first modal view in order to give give reason why we change the price and we can put new price in price textfield of modal view. I want the price in first modal view change when I set the price in second modal view. How to catch the value of second modal view to put in first modal view ?

Community
  • 1
  • 1

4 Answers4

1

Use NSNotification center

You have to addobserver event in First Modalview

    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload:) name:@"refresh" object:nil];
    }
- (void)reload:(NSNotification *)notification {
    textfield.text= [[notification userInfo] valueForKey:@"price"] ;
}

In second modalview you have to post notification after you complete edit

(pass your textfield value)

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"333" forKey:@"price"];
   [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:nil userInfo:userInfo]

;

Finally remove observer

-

(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refresh" object:nil];

}
Rams
  • 1,721
  • 12
  • 22
  • this is the error i got after inserting the code above : due to uncaught exception 'NSInvalidArgumentException', reason: '-[EditBillItemModalViewController reload]: unrecognized selector sent to instance 0x6a6b8f0' –  Aug 16 '12 at 06:52
  • sorry just change @selector(reload) to@selector(reload:) check my edited ans – Rams Aug 16 '12 at 07:30
  • now i use delegate, and it works ! –  Aug 16 '12 at 08:10
0

You can used Singleton just to save some data on that class

DLende
  • 5,162
  • 1
  • 17
  • 25
0

The following simple steps will make you able to do this.

In the first Modal ViewController, you can declare a function like

- (void) setUpdatedValueWithValue: (NSString *) newValue
{
    self.objTextField.text  =  newValue;
}

Declare this function on the header file too, so that we can access it from the other class.

In the second Modal ViewController

SecondViewController.h

@interface SecondViewController : UIViewController
{
     id    objFirstViewController;
}
@property (nonatomic, retain)   id   objFirstViewController;

@end

SecondViewController.m

@implementation SecondViewController

@synthesize objFirstViewController;

@end

Before you present the SecondViewController pass the object of FirstViewController to SecondViewController like,

- (void) presentSecondViewController
{
    SecondViewController    *objSecondViewController  =  [[SecondViewController alloc] init];
    objSecondViewController.objFirstViewController    =  self;
    [self presentModalViewController: objSecondViewController animated: YES];
    [objSecondViewController release];
    objSecondViewController = nil;
}

Then, in the function you are calling to dismiss the SecondViewController after the value edit you can do like,

- (void) finishEdit
{
    if([objFirstViewController respondsToSelector: @selector(setUpdatedValueWithValue:)])
    {
        [objFirstViewController performSelector: @selector(setUpdatedValueWithValue:) withObject: editedTextView.text];
    }
    [self dismissModalViewControllerAnimated: YES];     
}

Hope this helps.

Mathew Varghese
  • 4,527
  • 2
  • 17
  • 26
-1

set you object with your keyword

[[NSUserDefaults standardUserDefaults]setObject:@"value of second modal view" forKey:@"keyName"];

than get that object in first modal view

NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"keyName"];
freelancer
  • 1,658
  • 1
  • 16
  • 37
  • 1
    -1 This is not what NSUserDefaults is for. – Mick MacCallum Aug 16 '12 at 04:43
  • when i click button change, i use this method, but it doesn't work: - (IBAction)changeUnitPriceClicked:(id)sender { [self dismissModalViewControllerAnimated:YES]; EditBillItemModalViewController *ebi = [[EditBillItemModalViewController alloc] init]; ebi.unitPriceTextField.text = [self.changePriceTextfield.text init]; ebi.anItem.priceSale = [ebi.unitPriceTextField.text intValue]; } –  Aug 16 '12 at 04:46
  • unitPriceTextField is in first modalview and changePriceTextfield is in second modal view. I declare these textfields as properties of the class. –  Aug 16 '12 at 04:53
  • u can't change text value like that you have create property for that, first you have to pass value in that properly than you can set value in unitPriceTextField from that properly. – freelancer Aug 16 '12 at 04:53
  • Thank SmartWork that reply my question! I don't know much about it because I just start programming on iphone. –  Aug 16 '12 at 04:56
  • i think you have to different class one is first modal view and other one is second modal view if i am right than check this question http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?lq=1 – freelancer Aug 16 '12 at 05:04
  • i am doing the bill that has the tableview of items and when i click 1 item, i can go the 1 modal view that i can change the quantity of item and price. when i click to change the price, i will go to the second modal view to give reason and set the price! –  Aug 16 '12 at 05:07