如何将值从一个UIViewController传递给另一个

时间:2011-12-05 07:15:15

标签: ios objective-c

我正在创建一个应用,我必须将值从second class传递给first class。我在second class中为它创建了一个委托方法。

second class我有UITextField,如果在此文本字段中输入任何文字,则应将其传递到cell UITableView中的first view

但是,在我的情况下,该值未正确传递。我做错了什么?

这是我的代码:

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

2 个答案:

答案 0 :(得分:4)

  1. 删除委托方法。
  2. 将您的第二堂课改为第一堂课。
  3. 在第二类导入第一类并在那里实现id firstClass变量。
  4. 当您推动第二课时,将id从(3)设置为自我。
  5. 当你完成并准备传递它时,设置firstClass.passedValue = passingValue
  6. 流行第二课
  7. 例如:

    //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;
    

答案 1 :(得分:3)

请参考以下粗略划痕:

应用程序委托中的

.h

创建变量

NSString *varStr;

分配属性

@propery (nonatomic, retain) NSString *valStr;

在委托.m

@synthesize varStr;

初始化var

varStr = [NSString strinWithFormat:@"Hi"];
第一课

创建委托变量;

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

设定值

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

获取价值

NSLog (@"%@",var.varStr);
第二课

创建委托变量;

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

设定值

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

获取价值

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