这点容易出处,我的出错点在于下面代码红色部分
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"segue200"]) {
YcwFoodTypeViewController *foodView = segue.destinationViewController;
foodView.delegate = self;
}
}
总结:页面传值适用于,在第一个页面跳向第二个页面之后,第二个页面想要向第一个页面传值
例如:YcwAddFoodViewController 跳向YcwFoodTypeViewController页面
YcwAddFoodViewController.h
#import <UIKit/UIKit.h>
#import "YcwFoodTypeViewController.h"
@interface YcwAddFoodViewController : UITableViewController<YcwFoodTypeViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *shezhiLabel;
@property (strong, nonatomic) IBOutlet UILabel *xuanzeLabel;
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"segue200"]) {
YcwFoodTypeViewController *foodView = segue.destinationViewController; //这句代码让我刻骨铭心,一定记住
foodView.delegate = self;
}
}
- (void)foodTypeViewControllerDidSelect:(YcwFoodTypeViewController *)controller andType:(NSString *)type
{
self.shezhiLabel.text = type;
self.xuanzeLabel.text = @"已选择";
[controller.navigationController popViewControllerAnimated:YES];
}
YcwFoodTypeViewController.h文件
#import <UIKit/UIKit.h>
@class YcwFoodTypeViewController;
@protocol YcwFoodTypeViewControllerDelegate<NSObject> //代理协议
-(void)foodTypeViewControllerDidSelect:(YcwFoodTypeViewController *)controller andType:(NSString *)type;
@end
@interface YcwFoodTypeViewController : UITableViewController
@property (weak,nonatomic)id<YcwFoodTypeViewControllerDelegate> delegate;
@end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.delegate foodTypeViewControllerDidSelect:self andType:[typeArray objectAtIndex:indexPath.row]];
}