Я делаю приложение, где мне нужно передать значение от second class
до first class
. Я создал метод делегата для этого в second class
.Как передать значение от одного UIViewController к другому
В 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
почему у создали объект для TextviewExampleAppDelegate * проверка; создайте объект для второго класса, в котором вы создаете объект. –
@coolanikothari может помочь мне, что я должен сделать, попробуйте как par ur, но я не какое-либо решение, пожалуйста, помогите мне – Rocky