2011-12-05 196 views
0

我正在創建一個應用程序,我必須將一個值從second class傳遞到first class。我爲second class創建了一個代理方法。如何將值從一個UIViewController傳遞到另一個

second class我有一個UITextField,如果在這個文本框輸入文本時,應該在first view傳遞到cellUITableView

但是,在我的情況下,價值沒有被正確傳遞。我做錯了什麼?

這是我的代碼:

second.h

#import <UIKit/UIKit.h> 
@protocol secondDelegate<NSObject> 
@required 
- (void)setsecond:(NSString *)inputString; 
@end 

@interface second : UIViewController { 
    IBOutlet UITextField *secondtextfield; 
    id<secondDelegate>stringdelegate; 
    NSString *favoriteColorString; 
} 
@property (nonatomic, retain) UITextField *secondtextfield; 
@property (nonatomic, assign) id<secondDelegate>stringdelegate; 
@property (nonatomic, copy) NSString *favoriteColorString; 
@end 

second.m

#import "second.h" 

@implementation second 
@synthesize stringdelegate, secondtextfield, favoriteColorString; 

- (void)viewWillDisappear:(BOOL)animated { 
    [[self stringdelegate] setsecond:secondtextfield.text]; 
    favoriteColorString=secondtextfield.text; 
    NSLog(@"thuis check:%@", favoriteColorString); 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
    [theTextField resignFirstResponder]; 
    return YES; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //[[self stringdelegate] setsecond:secondtextfield.text]; 
    //favoriteColorString = secondtextfield.text; 
    //NSLog(@"thuis check:%@", favoriteColorString); 
} 
@end  

first.h

#import <UIKit/UIKit.h> 
#import "second.h" 
#import "TextviewExampleAppDelegate.h" 

@interface first : UITableViewController<secondDelegate> { 
    //TextviewExampleAppDelegate *app; 
    TextviewExampleAppDelegate *check; 
} 

first.m

@implementation first 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = @"message"; 
    cell.detailTextLabel.text = check.favoriteColorString; 
    NSLog(@"this second check:%@", check.favoriteColorString); 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    second *viewtwo = [[second alloc] initWithNibName:@"second" bundle:nil]; 
    //viewtwo.favoriteColorString = indexPath; 
    viewtwo.stringdelegate = self; 
    [self.navigationController pushViewController:viewtwo animated:YES]; 
    [viewtwo release]; 
} 

- (void)setsecond:(NSString *)inputString { 
    if (nil != self.stringdelegate) { 
     [self.stringdelegate setsecond:inputString]; 
    } 
    [self.tableView reloadData]; 
} 
@end 
+0

爲什麼ü創建對象TextviewExampleAppDelegate *檢查;創建用於創建對象的第二個類的對象。 –

+0

@coolanikothari可以幫助我什麼我必須做嘗試作爲你的答案,但我沒有任何解決方案,請幫助我 – Rocky

回答

4
  1. 移除委託方法。
  2. 將第二堂課導入第一堂課。
  3. 在二級導入一級並實現id firstClass變量那裏。
  4. 當你推動第二課時,從(3)到id
  5. 時you'v完畢,並準備通過它,設置firstClass.passedValue = passingValue
  6. 彈出第二類

例如:

//first.h: 
#import "second.h" 
@class second 

//second.h: 
#import "first.h" 
@class first 
... 
id firstClass; 

//first.m: 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    second *viewtwo =[[second alloc]initWithNibName:@"second" bundle:nil]; 
    [self.navigationController pushViewController:viewtwo animated:YES]; 
    viewtwo.firstClass = self; 
    [viewtwo release]; 
} 

//second.m: 
firstClass.passedValue = self.passingValue; 
+0

你可以舉一些例子,我嘗試但我的問題還沒解決請幫助我吧 – Rocky

+0

看,我編輯答案 – SentineL

+0

哨兵我做了你已經完成的par你已經做了哪些你給,但它不起作用我的數據不是從第二類到第一類 – Rocky

3

請參考以下粗糙從頭開始:

在申請委託書.h

創建變量

NSString *varStr; 

指派屬性

@propery (nonatomic, retain) NSString *valStr; 

在委託。米

@synthesize varStr; 

初始化變種

varStr = [NSString strinWithFormat:@"Hi"]; 

頭等

創建委託變種;

delegate class *var = (delegate class*)[[UIApplication sharedApplication] delegate]; 

設定值

var.varStr = [NSString strinWithFormat:@"First"]; 

獲取價值

NSLog (@"%@",var.varStr); 

在第二類

create delegate var;

delegate class *var = (delegate class*)[[UIApplication sharedApplication] delegate]; 

設定值

var.varStr = [NSString strinWithFormat:@"Second"]; 

獲取價值

NSLog (@"%@",var.varStr); 
+0

'委託類var =(委託類)[[UIApplication sharedApplication]委託];'不工作的人!!!我能做什麼?我想在我的應用程序 – Emon

+1

嘗試你的方法你能告訴我你的代碼? –

+0

其實我什麼也沒做....我只是想知道如何將一個控制器的多個字符串數據傳遞給另一個控制器。我已經使用了你的方法,但是當我使用'委託類var =(委託類)[[UIApplication sharedApplication ] delegate];'然後它顯示委託undefined.May是我問你一個愚蠢的問題,但實際上我從來沒有使用'sharedApplication'.So請幫我 – Emon

相關問題