我想向 parentviewcontroller 发送数据,但以下代码崩溃。给我解决方案
Post *vc;
vc.abc =@"Comment Conttroller";
[self.parentViewController dismissModalViewControllerAnimated:YES];
在这里, Post 是我调用该presentViewController:animated:completion
方法的控制器名称。
我想向 parentviewcontroller 发送数据,但以下代码崩溃。给我解决方案
Post *vc;
vc.abc =@"Comment Conttroller";
[self.parentViewController dismissModalViewControllerAnimated:YES];
在这里, Post 是我调用该presentViewController:animated:completion
方法的控制器名称。
把它放在你的父控制器中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];
}
将以下内容放在您的孩子班级中
-(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"];
}
一旦模型视图被关闭, yourNotificationHandler就会被执行,并且您作为对象传递的任何内容都会在您的父类中获取。请问是否还需要一些澄清。
把这个放在ParentViewController的 .h 文件中
NSString *strABC;
在ParentViewController中创建以下函数
-(void)setString:(NSString *)strEntered{
strABC=strEntered;
}
现在在 Post 视图控制器中这样做:
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];
由于您想要影响的视图控制器是parentViewController
,因此您应该能够强制转换和设置abc
属性。