user1438042
user1438042

Reputation: 109

Save variable when view controller is pushed

I have two view controllers: A table view that is seen when the app loads and A controller that is pushed when a cell is selected. I have a variable in one of the table view declarations that needs to be transferred to the other view controller when it is pushed.The variable gives each table cell a number ([objectAtIndex row] + 1) so that the cell on top is 1. the next is 2. ect. I need that variable so various used in the view controller. How can I do this? Thank you.

Here is the code in the first View controller:

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:@"UITableViewCell"];
}

BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];


//Variable that I need in the other view:
NSString *string = [NSString stringWithFormat:@"%d. %@",[indexPath row] + 1, p];


[[cell textLabel] setText:string];

 return cell;  
}

Upvotes: 0

Views: 526

Answers (2)

junaidsidhu
junaidsidhu

Reputation: 3580

There are many ways but the two simple ways are here...

One simple way in this

save value in user default

[[NSUserDefaults standardUserDefaults] setValue:@"Your Value" forKey:@"Your Key"];

retrieve value in next controller, write in viewWillAppear

[[NSUserDefaults standardUserDefaults] objectForKey:@"Your Key"];

2nd way is

   MessageBoxViewController *msgBoxController = [[MessageBoxViewController alloc]initWithNibName:@"MessageBoxViewController" bundle:nil];

[msgBoxController setReceverArray:urArray]; 

[self.navigationController pushViewController:msgBoxController animated:YES];
[msgBoxController release]; 

Upvotes: 4

David G
David G

Reputation: 5784

Use key-value coding/observing around textLabel from one controller class to the other. If UITableViewCell is KVC-compliant then you can get KVO for free by registering an observer on its text property (I use MacOS, not iOS so I don't know if this is the case). If not, before you set the text call [self willChangeValueForKey:] for some key property shadowing the cell's text and afterwards call [self didChangeValueForKey:]. Assuming your other class has set an observer on the first controller class it will get a observeValueForKeyPath: notification.

UPDATE: in response to the request for clarification.

Say I have two classes Foo and Bar. I want Foo to be notified whenever a property (i.e instance variable) of Bar changes. If Bar has been written in an appropriate fashion, it will be key-value coding compliant. This means that its properties are accessible via a so-called key path, which is an encoded string representation. Let's say that Bar is declared like this:

.h

@interface Bar : public NSObject
@property (assign,readwrite) NSInteger x;
@end

.m

#import "Bar.h"
@implementation Bar
@synthesize x;

...

@end

The combination of the @property and @synthesize declarations will have automatically generated a pair of getter and setter methods, called x and setX respectively. IF you have done this, then x is accessible using KVC. A class can do something like the following:

Bar *bar=[[Bar alloc] init];
NSInteger barX=[bar valueForKey:@"x"];
[bar setValue:2 forKeyPath:@"x"]; // really needs an NSNumber wrapper object   

For your purposes, this becomes useful because classes that are KVC-compliant can be made key-value observing (KVO) compliant. To implement KVO, you call the addObserver:forKeyPath:options:context: method on the object to be observed and implement the observeValueforKeyPath:ofObject:change:context: method in the object you wish to be doing the observing. Thus, for an instance of class Foo to be informed every time the x property of an instance of Bar changes, you could do something like the following:

Foo *foo=[[Foo alloc] init];
Bar *bar=[[Bar alloc] init];

[bar addObserver:foo forKeyPath:@"x" options:NSKeyValueObservingOptionNew
   context:NULL];

and in the implementation of the Foo class, you would override the observer method

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
   change:(NSDictionary *)change context:(void *)context
{
   if[keyPath isEqualToString:@"x"]
      {
      // extract new value from change dictionary
      }
}

Every time bar changed its x property using [self setX:<new value>] a message would be sent to the observeValueForKeyPath: method of foo. Note that in order for KVO to be triggered, the setter method must be used. It is not enough to simply say x=2; inside an instance method.

All of this is given to you free as part of the NSObject (or iOS equivalent) implementation. The docs are available from Apple's developer site. They are Key Value Coding Programming Guide and Key Value Observing Programming Guide.

Upvotes: 1

Related Questions