我正在创建一个应用,我必须将值从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
答案 0 :(得分:4)
id firstClass
变量。id
从(3)设置为自我。firstClass.passedValue = passingValue
例如:
//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);