【发布时间】:2015-10-18 11:41:30
【问题描述】:
基本上,我现在打算使用委托在视图控制器之间传递值。
视图控制器的流程是 A -> B -> C
当用户在“C”视图控制器中执行某些操作时,如何将值传递回第一个视图控制器,即“A”?
在我自己的代码中,委托方法永远不会被触发,并且“self.delegate”始终为“null”。我不知道为什么以及如何解决这个问题。
下面是First VC和Third VC的代码:
#import "ViewController.h"
#import "ThirdViewController.h"
@interface ViewController ()<PassValueProtocal>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
@implementation ViewController
{
ThirdViewController *thirdVC;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) passValueBack:(NSString *)value
{
NSLog(@"HAHAH");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)segueToSecondVC:(UIButton *)sender
{
thirdVC = [ThirdViewController sharedManager];
thirdVC.delegate = self;
}
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@end
@implementation ThirdViewController
+ (id) sharedManager
{
NSLog(@"myDelegate sharedManager");
static ThirdViewController *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ sharedManager = [[self alloc] init]; });
return sharedManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)passValueAction:(UIButton *)sender
{
NSLog(@"%@", self.delegate);
if ([self.delegate respondsToSelector:@selector(passValueBack:)])
{
[self.delegate passValueBack:self.myTextField.text];
}
}
【问题讨论】:
-
你考虑过使用 unwind segues 吗?
标签: ios objective-c delegates protocols