我正在創建一個應用程序,我必須將一個值從second class
傳遞到first class
。我爲second class
創建了一個代理方法。如何將值從一個UIViewController傳遞到另一個
在second class
我有一個UITextField
,如果在這個文本框輸入文本時,應該在first view
傳遞到cell
在UITableView
。
但是,在我的情況下,價值沒有被正確傳遞。我做錯了什麼?
這是我的代碼:
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
爲什麼ü創建對象TextviewExampleAppDelegate *檢查;創建用於創建對象的第二個類的對象。 –
@coolanikothari可以幫助我什麼我必須做嘗試作爲你的答案,但我沒有任何解決方案,請幫助我 – Rocky