objective-c 访问父视图控制器(自定义)属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/956990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:40:06  来源:igfitidea点击:

Accessing parent view controller (custom) properties

objective-ciphone

提问by rob5408

I have written a UITabBarControllersubclass (called MainViewController) and added a few instance variables, specifically a venue of type Venue. Each tab of the MainViewControllerneeds to access the venue variable of the MainViewController. In the first tab, a UIViewControllersubclass named HomeViewController, I wrote my viewDidLoadmethod and tried to output to NSLog with both...

我编写了一个UITabBarController子类(称为MainViewController)并添加了一些实例变量,特别是 Venue 类型的场所。每个选项卡都MainViewController需要访问MainViewController. 在第一个选项卡中,一个UIViewController名为的子类HomeViewController,我编写了我的viewDidLoad方法并尝试使用两者输出到 NSLog...

NSLog(@"%@", self.parentViewController.venue.name);

and

NSLog(@"%@", self.tabBarController.venue.name);

But XCode gives the error

但是 XCode 给出了错误

error: request for member 'venue' in something not a structure or union

I can access the venue property from within MainViewControllerjust fine so have ruled out an error there. The iPhone simulator does see both self.parentViewControllerand self.tabBarControlleras an instance of MainViewController. The line...

我可以MainViewController很好地从内部访问场地属性,因此排除了那里的错误。iPhone 模拟器确实将self.parentViewController和 都self.tabBarController视为MainViewController. 线...

NSLog(@"%@", self.tabBarController);

outputs...

输出...

2009-06-05 17:54:46.502 Venue[29729:20b] <MainViewController: 0x536600>

Since it's seen as a MainViewControllerinstance, I doubt using casting would help. Is my only other option to initialize the HomeViewControllerwith a copy of the venue or am I just doing something completely wrong with 'self.parentViewController.venue.name'? Thanks, Rob

由于它被视为一个MainViewController实例,我怀疑使用强制转换会有所帮助。是我唯一的其他选择来初始化HomeViewController场地的副本还是我只是在用“ self.parentViewController.venue.name”做一些完全错误的事情?谢谢,罗布

回答by Ed Marty

You're doing something completely wrong. parentViewControlleris declared as a UIViewController. NSLoging it outputs its real type due to the wonders of polymorphism.

你在做一些完全错误的事情。 parentViewController被声明为UIViewController. NSLog由于多态性的神奇,它输出了它的真实类型。

UIViewControllerdoesn't have a Venuemember. Your MainViewControllerdoes. Casting it is, in fact, the right answer. Make sure to import MainViewController.has well.

UIViewController没有Venue会员。你的MainViewController。事实上,铸造它是正确的答案。确保也导入MainViewController.h

#import "MainViewController.h"
[...]
NSString *name = ((MainViewController *)self.parentViewController)).venue.name;

Also make sure, of course, that venueis declared as a @propertyof MainViewController, and nameis a @propertyof Venue.

另外,还要确保,当然,那的venue声明为@propertyMainViewController的,并且name@propertyVenue

回答by comonitos

int currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];

//previous view controller
AccountViewController *account = (AccountViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1];

account.property = object;
[account doSmthng];

回答by Marc Charbonneau

Your NSLog()statement is "seeing" it as a MainViewControllerbecause that's what the object actually is. The problem you're having though, is that the compiler doesn't actually realize this because the parentViewControllerproperty is declared as a plain old UIViewController. Even though you're actually returning a subclass of it, as far as the compiler knows you're trying to call that method on a UIViewController, and it's going to complain. Casting it to a MainViewControllerwill solve the issue.

您的NSLog()陈述是将其“视为”,MainViewController因为这就是对象的实际情况。但是,您遇到的问题是编译器实际上并没有意识到这一点,因为该parentViewController属性被声明为普通的 old UIViewController。即使您实际上返回了它的一个子类,就编译器而言,您正在尝试在 a 上调​​用该方法UIViewController,并且它会抱怨。将其转换为 aMainViewController将解决问题。