1

Hello everybody, I have a UITableView in a UIViewController. When a row in the table is tapped I am collecting the cell's text value and putting it in a string called localstringGValue.

I want to pass this string and display it in another, viewController, but without using -pushViewController: I want this value to be stored somewhere like NSUserDefaults or NSDictonary and then, on viewWillapper of the other view controller I want this stored value to be displayed in a label.

in my .h:

 NSString *localStringGValue;

in my .m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    localStringGValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; 
}

in my other view controller:

-(void)viewWillAppear
{
    label.text=localStringGValue;
}

Thanks in advance.

2

4 Answers 4

2

save to nsuserdefaults:

[[NSUserDefaults standardUserDefaults] setObject:localstringGValue forKey:@"localstringGValue"];
[[NSUserDefaults standardUserDefaults] synchronize];

retrieve from nsuserdefaults:

NString *localstringGValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"localstringGValue"];
Sign up to request clarification or add additional context in comments.

2 Comments

Is that a good way to use NSUserDefaults for storing dynamic value?
How would you define "good" in this context? It's just one way to use as and when you feel it's sensible. Sometimes I use a mix of notifications and delegates instead. However, mostly I use a distinct model class. For a small infrequent amount of data and a simple option nsuserdefaults can sometimes suffice. We all have to walk before we can run.
1

Just use delegate. Before you push the 'UploadViewController' instance, you need set it's delegate as self(in GoogleDocMainPageController.m). Everytime, the tabel cell is selected, it'll set value for self.delegate(Here is GoogleDocMainPageController instance) by dispatching self.delegate's method, which is implemented by GoogleDocMainPageController:

[self.delegate setDataAfterSelectedTabelCell:[NSString stringWithFormat:@"TalbeCell %d selected", [indexPath row]]];

The main code is shown below:
UploadViewController.h:

#import <UIKit/UIKit.h>

@class UploadViewController;

@protocol UploadViewControllerDelegate <NSObject>

- (void)setDataAfterSelectedTabelCell:(NSString *)stringValueInCell;

@end

@interface UploadViewController : UITableViewController

@property (nonatomic, retain) id <UploadViewControllerDelegate> delegate;

@end

UploadViewController.m:

//...
@synthesize delegate = _delegate;

//...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate setDataAfterSelectedTabelCell:[NSString stringWithFormat:@"TalbeCell %d selected", [indexPath row]]];
}

GoogleDocMainPageController.h:

#import <UIKit/UIKit.h>

#import "UploadViewController.h"

@class UploadViewController;

@interface GoogleDocMainPageController : UIViewController <UploadViewControllerDelegate>

- (void)loadUploadViewController;

@property (nonatomic, retain) UILabel * glLabel;
@property (nonatomic, retain) UploadViewController * uploadViewController;

@end

GoogleDocMainPageController.m:

//...
@synthesize glLabel = _glLabel;
@synthesize uploadViewController = _uploadViewController;

//...
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton * uploadButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 160.0f, 300.0f, 35.0f)];
    [uploadButton setBackgroundColor:[UIColor blackColor]];
    [uploadButton setTitle:@"Upload Button" forState:UIControlStateNormal];
    [uploadButton addTarget:self action:@selector(loadUploadViewController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:uploadButton];
    [uploadButton release];

    self.glLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 200.0f, 300.0f, 35.0f)];
    [self.glLabel setBackgroundColor:[UIColor blackColor]];
    [self.glLabel setTextColor:[UIColor whiteColor]];
    [self.glLabel setTextAlignment:UITextAlignmentCenter];
    [self.glLabel setText:@"Default"];
    [self.view addSubview:self.glLabel];

    self.uploadViewController = [[UploadViewController alloc] initWithStyle:UITableViewStylePlain];
}

//...
#pragma mark -

- (void)loadUploadViewController
{
    [self.uploadViewController setDelegate:self];
    [self.navigationController pushViewController:self.uploadViewController animated:YES];
}

#pragma mark - UploadViewControllerDelegate

- (void)setDataAfterSelectedTabelCell:(NSString *)stringValueInCell
{
    [self.glLabel setText:stringValueInCell];
}

9 Comments

sir i didn't understand this code:// Then just show your another view controller - (void)theMethodDispatchAnotherViewController { [self.navigationController pushViewController:self.yourAnotherViewController animated:YES]; } where did i put this code?
so in viewwillappear of another view controller i want to put lbel.text = self.yourAnotherviewcontroller right?
@ICoder it just a sample method, you can put it in your .m file. Well, forget it, just use your self.yourAnotherViewController when you need to display your another view.
so label.text =self.yourAnotherViewController; in the viewwillapper of the anotherviewcontroller
@ICoder, nope, it's label.text = self.localStringGValue;, localStringGValue is declared in YourAnotherViewController.h.
|
0

Why don't you declare some @property(nonatomic,retain) and @synthesize it ? Then the getters/setters will be automatically created. Otherwise, define some getter/setters yourself.

1 Comment

i alerdy diclared it,but s i said earlier i didn't ant that value to pas in page redirection code ,i just want that value to displayed in label when i tap the tableview cell
0

Go through Following Steps-

In First ViewController -

  1. Create Object of second ViewController like SecondViewController *sec= [[SecondViewController alloc]init]; sec.myLable=@"My Lable String";

    In Second ViewController -

  2. In .h file UILable *myLable;

    @property(nonautomic,retain)IBOutlet UILable *myLable;

    2.In .m file @synthesize myLable;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.