情况:
3个视图控制器,白色为主,红色和蓝色嵌入(容器视图控制器)
我选择红色的东西,它切换到蓝色,我刷蓝色 - 它返回,我想将标签中的文字更改为一些自定义文本
序列:
所以我使用委托,它也有效。但是当我尝试通过self.thisLabel.text
设置标签的文本属性(在确认我的协议的方法中)时,NSLog说这个标签是零,虽然我有一个出口。如果我使用类似[self.view viewWithTag:tag]
的内容,NSLog会显示标签已存在,但我无法设置文本,但它保持不变。
头
@interface ContactsViewController : UIViewController <BlueViewControllerDelegate>
实施
- (void)adjustLabel:(NSString *)string{
NSLog(@"i am here baby %@",self.thisLabel);
[[self.view viewWithTag:57] setValue:string forKey:@"text"];
}
标题:
@protocol BlueViewControllerDelegate <NSObject>
-(void)adjustLabel:(NSString*)string;
@end
和
@property id<ViewControllerDelegate> delegate;
实施
- (void)viewDidLoad{
[super viewDidLoad];
UIStoryboard *sB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
WhiteViewController *WhiteVc = [sB instantiateViewControllerWithIdentifier:@"whiteID"];
self.delegate = WhiteVc;
}
-(void)willMoveToParentViewController:(UIViewController *)parent{
[self.delegate adjustLabel:@"some custom text"];
}
-(IBAction)swipePerformed:(UISwipeGestureRecognizer*)sender{
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
有什么想法吗?
答案 0 :(得分:0)
如果我正确理解您的VC层次结构,WhiteViewController
是BlueViewController
的父级,那么您不应该在viewDidLoad
中的故事板中实例化新的一个,而是:
WhiteViewController *whiteVc = (WhiteViewController*)self.parentViewController;
self.delegate = whiteVC;
另外,我发现压倒willMoveToParentViewController
特别有用。您可以将代理电话转到您的滑动操作。