2011-12-12 37 views
1

大家好,我有一個UITableViewUIViewController。當表格中的一行被點擊時,我正在收集單元格的文本值並將其放入一個名爲localstringGValue的字符串中。如何將值傳遞給另一個viewcontroller?

我想通過此串,而在另一個顯示它,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; 
} 

在此先感謝。

+0

代表和協議的簡單情況...... http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html –

+0

搜索關於委託變量... – Maulik

回答

2

保存到NSUserDefaults的:

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

從檢索NSUserDefaults的:

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

ohh我的上帝對我的作品,非常感謝你 – ICoder

+0

這是一個很好的方式來使用'NSUserDefaults'來存儲動態值嗎? – Kjuly

+0

在這種情況下你會如何定義「好」?這只是一種使用方式,當你覺得它是明智的。有時我會使用通知和委託組合。但是,大多數情況下,我使用不同的模型類。對於少量罕見的數據和簡單的選項,nsuserdefaults有時可以滿足要求。我們都必須走路才能跑步。 – ader

0

爲什麼不申報@property(nonatomic,retain)@synthesize呢?然後getters/setters將被自動創建。否則,自己定義一些getter/setter。

+0

我alerdy diclared它,但si先前說我沒有螞蟻的價值,以在頁面重定向代碼pas,我只是希望該值顯示在標籤上,當我點擊桌面單元格 – ICoder

1

只需使用delegate。在推送'UploadViewController'實例之前,您需要將其設置爲delegate,並將其設置爲self(位於GoogleDocMainPageController.m中)。

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

主代碼如下所示:每次的TABEL小區被選擇,它會爲self.delegate通過分派self.delegate的方法,該方法是通過GoogleDocMainPageController實現設定值(這裏是GoogleDocMainPageController實例)
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]; 
} 
+0

先生我不明白這個代碼:/ /然後只顯示你的另一個視圖控制器 - (void)theMethodDispatchAnotherViewController {self.navigationController pushViewController:self.yourAnotherViewController animated:YES]; }我把這段代碼放在哪裏? – ICoder

+0

所以在viewwillappear的另一個視圖控制器我想把lbel.text = self.yourAnotherviewcontroller吧? – ICoder

+0

@代碼它只是一個示例方法,你可以把它放在你的'.m'文件中。那麼,忘記它吧,當你需要顯示你的'另一個視圖'時,只需使用'self.yourAnotherViewController'。 – Kjuly

0

通過FOL去降脂步驟 -

在首先的ViewController -

  1. 創建第二視圖控制器 像SecondViewController *秒= [[SecondViewController的alloc] INIT]的對象; 秒。myLable = @「My Lable String」;

    在第二的ViewController -

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

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

    2.In.m文件 @synthesize myLable;

相關問題