将 IndexPath 传递给子视图
您好,我正在尝试将索引值从父视图传递到子视图,如下所示 //main.m
SubViewController *subViewController = [[SubViewController alloc] init];
subViewController.parentViewSelectedIndexPath = indexPath;
但是,当我尝试检查值是否已传递到子视图时,就像这样
//sub.m
- (void)viewDidLoad
{
//
NSLog(@"%@", parentViewSelectedIndexPath);
//
声明在侧面 子.h
//
@interface VehicleResultViewController : UITableViewController {
//
NSIndexPath *parentViewSelectedIndexPath;
//
@property (nonatomic, retain) NSIndexPath *parentViewSelectedIndexPath;
//
@end
//回答
子视图在获取传递给 is 的索引路径之前加载..所以我 必须使用稍后执行的方法 is(didselectricwatindexpath) 这打印出正确的索引路径 这是从主视图发送给它的。
它打印 (null) 到控制台..我做错了什么吗?
Hi I am trying to pass an indexvalue from my parent view to my subview like this
//main.m
SubViewController *subViewController = [[SubViewController alloc] init];
subViewController.parentViewSelectedIndexPath = indexPath;
However when I try to check to see if the value have been passed over to the subview like so
//sub.m
- (void)viewDidLoad
{
//
NSLog(@"%@", parentViewSelectedIndexPath);
//
decliration in side
sub.h
//
@interface VehicleResultViewController : UITableViewController {
//
NSIndexPath *parentViewSelectedIndexPath;
//
@property (nonatomic, retain) NSIndexPath *parentViewSelectedIndexPath;
//
@end
//Answer
subview loads before it gets the indexpath being passed to is.. so I
had to use a method that is executed later
i.s.(didselectrowatindexpath) this printed out the correct indexpath
that was sent to it from the main view.
it prints (null) to the console.. am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加一些 NSLog 来确定调用顺序,@Kevin 指出可以在设置属性之前调用 viewDidLoad 。
此外,如果每当实例化
subViewController
时总是设置indexPath
,那么最好以indexPath
作为参数创建您自己的指定初始值设定项。在@Kevin 做出的情况下,这就是答案。Add some
NSLog
s to determine the order of calls, @Kevin makes the point thatviewDidLoad
may be called prior to setting the property.Also if the
indexPath
is always set wheneversubViewController
is instantiated it is good practice to create your own designated initializer withindexPath
as a parameter. In the case that @Kevin makes this is the answer.