2011-12-05 36 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

相关问题