将 IndexPath 传递给子视图

发布于 2024-12-07 17:39:22 字数 776 浏览 0 评论 0原文

您好,我正在尝试将索引值从父视图传递到子视图,如下所示 //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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2024-12-14 17:39:22

添加一些 NSLog 来确定调用顺序,@Kevin 指出可以在设置属性之前调用 viewDidLoad 。

此外,如果每当实例化 subViewController 时总是设置 indexPath ,那么最好以 indexPath 作为参数创建您自己的指定初始值设定项。在@Kevin 做出的情况下,这就是答案。

Add some NSLogs to determine the order of calls, @Kevin makes the point that viewDidLoad may be called prior to setting the property.

Also if the indexPath is always set whenever subViewController is instantiated it is good practice to create your own designated initializer with indexPath as a parameter. In the case that @Kevin makes this is the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文