[英]Pass value to parent controller when dismiss the controller
我想向 parentviewcontroller 发送数据,但以下代码崩溃。 给我解决方案
Post *vc;
vc.abc =@"Comment Conttroller";
[self.parentViewController dismissModalViewControllerAnimated:YES];
在这里, Post 是 controller 名称,我从中调用presentViewController:animated:completion
方法。
把它放在你的父 controller 中viewDidLoad
// get register to fetch notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourNotificationHandler:)
name:@"MODELVIEW DISMISS" object:nil];
// --> Now create method in parent class as;
// Now create yourNotificationHandler: like this in parent class
-(void)yourNotificationHandler:(NSNotification *)notice{
NSString *str = [notice object];
}
在您的孩子 class 中添加以下内容
-(void)dissmissModelView{
[self dismissModalViewControllerAnimated:YES];
NSLog(@"DismissModalviewController");
//raise notification about dismiss
[[NSNotificationCenter defaultCenter]
postNotificationName:@"MODELVIEW DISMISS"
object:@"Whatever you want to send to parent class"];
}
只要model视图被关闭,你的NotificationHandler 就会被执行,并且你作为对象传递的任何东西都会在你的父 class 中获取。 请问是否还需要一些澄清。
在ParentViewController中获取这个 in.h 文件
NSString *strABC;
在 ParentViewController 中制作以下function
-(void)setString:(NSString *)strEntered{
strABC=strEntered;
}
现在在帖子视图中 controller 这样做:
ParentViewController *objSecond =
[[ParentViewController] initwithNibName:@"parentView.xib" bundle:nil];
[objSecond setString:@"Comment Controller"];
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];
现在,在secondViewController viewWillAppear
方法中写下这个。
-(void)viewWillAppear:(BOOL)animated{
lblUserInput.text = strABC;
}
如果你没有使用UINavigationContoller
那么你可以做这样的事情。
SecondViewControler *objSecond =
[[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
[objSecond setUserInput:txtUserInput.text];
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];
vc
似乎没有被初始化。
你可能可以这样做,
vc = (Post *)self.parentViewController;
vc.abc = @"Comment Conttroller";
[vc dismissModalViewControllerAnimated:YES];
由于您想要影响的视图 controller 是parentViewController
,因此您应该能够转换和设置abc
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.