在我正在开发的应用程序中,我在掌握将委托与 Storyboard 结合使用时遇到了一些问题。我查看了其他一些搜索,包括Passing data back :Delegate method not called,但无济于事。在这一点上,我已经删除了我的应用程序中的所有内容,除了我的两个视图的基础知识。第一个视图是一个基本视图控制器,带有一个按钮,当按下该按钮时,它会转到模式视图,其中在文本字段中输入一个数据。保存按钮位于该模式视图上,当按下该按钮时,将返回到第一个视图。当然,我也想将输入的数据发送回第一个视图。听起来很简单?当然可以,但在某个地方我无法让它工作。似乎我的代表电话从未打过。这是代码:
MyDayViewController.h - 这是第一个视图控制器
#import <UIKit/UIKit.h>
#import "AddRatingModalViewController.h"
@interface MyDayViewController : UIViewController <AddRatingDelegate>
@property (strong, nonatomic) IBOutlet UIButton *addRatingBtn;
@property (strong, nonatomic) IBOutlet UIButton *addAccompBtn;
@property (strong, nonatomic) IBOutlet UILabel *ratingLabel;
@end
MyDayViewController.m
#import "MyDayViewController.h"
@implementation MyDayViewController
@synthesize addRatingBtn;
@synthesize addAccompBtn;
@synthesize ratingLabel;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ToRatingVC"])
{
NSLog(@"Setting MyDayVC as a delegate of AddRatingMVC");
AddRatingModalViewController *addRatingController = segue.destinationViewController;
addRatingController.delegate = self;
}
}
- (void)theSaveButtonTapped:(AddRatingModalViewController *)controller withRating:(NSNumber*) rating
{
NSLog(@"In theSaveButtonTapped method of MyDayVC");
ratingLabel.text = [[NSString alloc]initWithFormat:@"%@", rating];
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[self setAddRatingBtn:nil];
[self setAddAccompBtn:nil];
[self setRatingLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
AddRatingModalViewController.h - 这是模态视图控制器
#import <UIKit/UIKit.h>
@class AddRatingModalViewController;
@protocol AddRatingDelegate
- (void)theSaveButtonTapped:(AddRatingModalViewController *)controller withRating:(NSNumber*) rating;
@end
@interface AddRatingModalViewController : UIViewController
@property (nonatomic, weak) id <AddRatingDelegate> delegate;
@property (strong, nonatomic) IBOutlet UITextField *addRatingTextField;
- (IBAction)cancelBtnPressed:(id)sender;
- (IBAction)saveBtnPressed:(id)sender;
@end
AddRatingModalViewController.m
#import "AddRatingModalViewController.h"
@implementation AddRatingModalViewController
@synthesize addRatingTextField;
@synthesize delegate;
- (IBAction)cancelBtnPressed:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)saveBtnPressed:(id)sender {
NSNumber *ratingARMVC = [NSNumber numberWithInteger:[addRatingTextField.text integerValue]];
NSLog(@"The entered Rating in ARMVC is %@", ratingARMVC);
NSLog(@"Telling the MyDayVC Delegate that Save was tapped on the AddRatingTVC");
[self.delegate theSaveButtonTapped:self withRating:ratingARMVC];
}
- (void)viewDidUnload
{
[self setAddRatingTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
控制台的输出如下:
2012-07-16 16:49:16.384 MyDayDelTest[4695:f803] Setting MyDayVC as a delegate of AddRatingMVC
2012-07-16 16:49:39.274 MyDayDelTest[4695:f803] The entered Rating in ARMVC is 7
2012-07-16 16:49:39.275 MyDayDelTest[4695:f803] Telling the MyDayVC Delegate that Save was tapped on the AddRatingTVC
这正好在委托调用应该在 Save 方法中进行之前。任何帮助将不胜感激!我应该说,当我不使用情节提要时,测试可以工作,但我确实需要它来处理情节提要,因为我的应用程序的其余部分都在使用它们。提前致谢。