我正在尝试将数据从一个Table View Controller传回到之前的那个。这是我为完成它而编写的代码: NPViewController.h(第二个视图传回数据):
@class NPViewController;
@protocol NPViewControllerDelegate <NSObject>
-(void) passItemBack: (NPViewController *) controller didFinishWithItem: (NSString *) string;
@end
@interface NPViewController : UITableViewController <UITextFieldDelegate>
@property (weak, nonatomic) id<NPViewControllerDelegate> delagate;
- (IBAction)createNewProject:(id)sender; //a bar button item that sends data on click
@end
NPViewController.m:
//barButton item IBAction
- (IBAction)createNewProject:(id)sender {
[self.delagate passItemBack:self didFinishWithItem:@"Test"];
[self dismissViewControllerAnimated:YES completion:nil];
}
InternalTabViewController.h //第一个接收数据的视图
#import <UIKit/UIKit.h>
#import "NPViewController.h"
@interface InternalTabViewController : UITableViewController <NPViewControllerDelegate>
@property (weak, nonatomic) NSString * projectName;
@property (weak, nonatomic) NSString * projectWorth;
@end
InternalTabViewController.m
@synthesize projectName, projectWorth;
//in ViewDidLoad
NPViewController *NPVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NPViewController"];
NPVC.delagate = self;
//implementation of protocol function
-(void)passItemBack:(NPViewController *)controller didFinishWithItem:(NSString *)string
{
self.projectName = string;
}
程序无法通过第一个视图控制器(与这两个视图无关)并抛出错误:
由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类与密钥createNewProject不符合密码值编码。'
我只是想把两个字符串从一个屏幕传回另一个屏幕。我该如何解决这个问题?
答案 0 :(得分:0)
我无法理解你为什么要将NPViewController(ViewController)传递给InternalTabViewController(ViewController),稍后你将它解雇(dismissViewControllerAnimated)。
由于您只需要字符串,我建议重新设计协议
-(void) passItemBack: (NPViewController *) controller didFinishWithItem: (NSString *) string;
更简单的事情
-(void) passItemBack:(NSString*) item;
我不知道它是否能解决问题。如果我有完整的代码,我可以提供帮助。