如何将值传递给另一个viewcontroller?

时间:2011-12-12 13:31:07

标签: iphone objective-c ios uitableview uiviewcontroller

大家好,我在UITableView中有一个UIViewController。当点击表格中的一行时,我正在收集单元格的文本值并将其放在名为localstringGValue的字符串中。

我想传递此字符串并将其显示在另一个字符串viewController中,但不使用-pushViewController:我希望将此值存储在NSUserDefaultsNSDictonary之类的地方然后,在另一个视图控制器的viewWillapper上,我希望这个存储的值显示在标签中。

.h

 NSString *localStringGValue;

.m

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

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

在我的其他视图控制器中:

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

提前致谢。

4 个答案:

答案 0 :(得分:2)

保存到nsuserdefaults:

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

从nsuserdefaults中检索:

NString *localstringGValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"localstringGValue"];

答案 1 :(得分:1)

只需使用delegate即可。在推送“UploadViewController”实例之前,您需要将其设置为delegateself(在GoogleDocMainPageController.m中)。每次选择tabel单元格时,它都会通过调度self.delegate的方法设置self.delegate(这是GoogleDocMainPageController实例)的值,该方法由GoogleDocMainPageController实现:

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

主要代码如下所示:
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];
}

答案 2 :(得分:0)

为什么不申报一些@property(nonatomic,retain)@synthesize呢?然后将自动创建getter / setter。否则,自己定义一些getter / setter。

答案 3 :(得分:0)

完成以下步骤 -

在First ViewController中 -

  1. 创建第二个ViewController的Object 像SecondViewController * sec = [[SecondViewController alloc] init];  sec.myLable = @“My Lable String”;

    在第二个ViewController中 -

  2. 在.h文件中 UILable * myLable;

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

    2.In .m文件 @synthesize myLable;