如何在UIViewController中设置parentViewController?

发布于 2024-08-03 21:57:39 字数 318 浏览 8 评论 0原文

UIViewController 的parentViewController 属性是只读的,但我正在嵌套自定义视图控制器并希望使用此属性。

但是,由于它是只读的,并且我没有找到其他方法来设置此属性,所以我的问题是:我如何设置它?

显然,UINavigationController 可以在 -pushViewController 中以某种方式设置属性, -presentModalViewController 也可以,所以它一定是可能的。

我知道我可以添加自己的 UIViewController 属性,但我确信parentViewController 原则上是正确的属性。

The parentViewController property of UIViewController is readonly, but I am nesting custom view controllers and would like to use this property.

However, since it is readonly, and I found no other way to set this property, my quesion is: how do I set it?

Obviously, UINavigationController can set the property somehow in -pushViewController, and so can -presentModalViewController, so it must be possible.

I am aware that I can just add my own UIViewController property, but I'm sure that parentViewController is, in principle, the correct property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

剧终人散尽 2024-08-10 21:57:39

解决方案是:

   - (void)setParentController:(UIViewController*)parent{
 [self setValue:parent forKey:@"_parentViewController"];
    }

它不会导致链接器出现问题!

PS:不要使用“setParentViewController”作为方法名称,因为该方法存在于私有API中并且Apple说:
“您的应用程序中包含的非公共 API 是 setParentViewController:。

如果您在源代码中定义了与上述 API 同名的方法,我们建议更改您的方法名称,以便它不再与 Apple 的方法名称冲突。私有 API,以避免您的应用程序在未来的提交中被标记,

请在下次更新中解决此问题......”

Solution is:

   - (void)setParentController:(UIViewController*)parent{
 [self setValue:parent forKey:@"_parentViewController"];
    }

It does not cause problems with linker!

PS: Don't use "setParentViewController" as method name, because this method exists in private API and Apple say:
"The non-public API that is included in your application is setParentViewController:.

If you have defined a method in your source code with the same name as the above mentioned API, we suggest altering your method name so that it no longer collides with Apple's private API to avoid your application being flagged with future submissions.

Please resolve this issue in your next update..."

↙厌世 2024-08-10 21:57:39

我意识到这个问题是在 iOS 5 之前提出的,但作为参考,当你想要嵌套 UIViewControllers 时,你应该使用 addChildViewController。这也会自动设置parentViewController 属性。

- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);

您可以在 苹果

I realize this question was asked before iOS 5, but for the reference, you should use addChildViewController when you want to nest UIViewControllers. This will also automatically set the parentViewController property.

- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);

You can read more about "Creating Custom Content View Controllers" at Apple.

笑看君怀她人 2024-08-10 21:57:39

我尝试了 Alex 的建议,为 UIViewController 购买一个类别,它在模拟器中有效,但在我的手机上无效。这是类别,

@interface UIViewController (parentSetter) 
-(void)setParentUIViewController:(UIViewController*)parent;
@end

@implementation UIViewController (parentSetter)
-(void)setParentUIViewController:(UIViewController*)parent
{
 _parentViewController = parent;
}
@end

它可以编译并正常工作,但请注意下划线成员,这有点令人反感。
这就是在针对 3.0 SDK 进行编译时导致链接器错误的原因。

我有一个容器视图,它有 2 个子视图,表是其中之一。该表需要一个父表,以便它可以与导航栏等进行交互。

我将采用这个解决方案:

@interface AdoptedTableViewController : UITableViewController {
    UIViewController* surrogateParent;
}

-(UINavigationController*)navigationController;
@property (nonatomic, assign) IBOutlet UIViewController *surrogateParent;
@end

@implementation AdoptedTableViewController
@synthesize surrogateParent;

-(UINavigationController*)navigationController
{
    if( [super navigationController] )//self.navigationController ) 
    {
        return [super navigationController];
    }
    else
    {
        return surrogateParent.navigationController;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

我所有的表视图控制器现在都采用TableViewControllers。他们需要父级的主要原因是他们可以将视图控制器推送到导航堆栈上,以便由导航控制器 getter 透明地处理。

如果parentViewController不是只读的,那就太好了,但在我对 _parentViewController 的涉猎中,我发现 ViewController 层次结构不仅仅是该属性。我认为,这种关系中可能存在很多耦合和责任,苹果还没有为大众清理干净。例如,我注意到在向上移动导航层次结构时出现了奇怪的取消选择行为,但我无法修复。也许 UINavigation 控制器反映了其顶级控制器的类并且行为不同?

简而言之,它确实是只读的,并且没有干净或简单的解决方法。你只需要围绕它进行设计。

I tried Alex's suggestion buy making a category for UIViewController, and it worked in the simulator, but not on my phone. here is the category

@interface UIViewController (parentSetter) 
-(void)setParentUIViewController:(UIViewController*)parent;
@end

@implementation UIViewController (parentSetter)
-(void)setParentUIViewController:(UIViewController*)parent
{
 _parentViewController = parent;
}
@end

It compiles and works fine, but note the underscore member which is a bit off putting.
That's what causes the linker error when compiling against the 3.0 SDK.

I have a container view that has 2 subviews and a table is one of them. The table needs a parent so it can interact with the navigation bar, among other things.

I'm going with this solution instead:

@interface AdoptedTableViewController : UITableViewController {
    UIViewController* surrogateParent;
}

-(UINavigationController*)navigationController;
@property (nonatomic, assign) IBOutlet UIViewController *surrogateParent;
@end

@implementation AdoptedTableViewController
@synthesize surrogateParent;

-(UINavigationController*)navigationController
{
    if( [super navigationController] )//self.navigationController ) 
    {
        return [super navigationController];
    }
    else
    {
        return surrogateParent.navigationController;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

All of my table view controllers are now adoptedTableViewControllers. The main reason they need parents is so they can push view controllers on to the navigation stack, so that is handled transparently by the navigation controller getter.

It would be nice if parentViewController were not read only, but in my dabble with _parentViewController I discovered there is more to the ViewController hierarchy than just that property. I think there might be a lot of coupling and responsibilities in that relationship that Apple hasn't cleaned up enough for the masses. For instance, I noticed an odd deselection behavior when moving up the navigation hierarchy that I couldn't fix. Perhaps UINavigation controllers reflect the class of their top controller and behave differently?

In short, it really is read only and there is no clean or simple workaround. You just got to architect around it.

你是暖光i 2024-08-10 21:57:39

但是,由于它是只读的,并且我没有找到其他方法来设置此属性,所以我的问题是:如何设置它?

如果它是只读,则可以'不要使用点符号而不出现 编译器错误< /a>.

不过,您也许可以使用类别来添加自定义将ParentViewController 方法修改为UIViewController 类。

即使属性是只读的,如果变量不是@protected,它本身也可能是可修改的。如果它是@protected,子类化视图控制器可能允许您选择修改变量。

However, since it is readonly, and I found no other way to set this property, my quesion is: how do I set it?

If it is readonly, you can't use dot notation without getting a compiler error.

However, you might be able to use categories to add a custom modifyParentViewController method to the UIViewController class.

Even if the property is readonly, the variable itself might be modifiable, if it is not @protected. If it is @protected, subclassing the view controller may allow you the option of modifying the variable.

鹤仙姿 2024-08-10 21:57:39

ParentViewController 用于 NavigationViewControllers 和呈现模式视图控制器,如果没有方法 PushViewController 或 PresentModalViewController,则无法设置属性。 ParentViewController 是一个只读属性,只能由 UIViewController 类和 UINavigationController 类读取,这些子类将无权设置该属性。

parentViewController is for the purposes of NavigationViewControllers and presenting modal view controllers , there is no way to set the property without the methods pushViewController or presentModalViewController. The parentViewController is a readonly property can only be read by the UIViewController class and UINavigationController class, subclasses of these will not have access to set the property.

浅语花开 2024-08-10 21:57:39

看起来调用 setParentViewController: 方法是有效的。

[childViewController setParentViewController:self];

然而,这仍然会生成编译器警告。在我看来,这意味着不要这样做(我改为子类化)。

It looks like calling the setParentViewController: method works.

[childViewController setParentViewController:self];

However this still generates a compiler Warning. Which, IMO, means don't do it (I subclassed instead).

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