从静态库访问 self.view

发布于 2024-12-02 19:37:44 字数 450 浏览 1 评论 0原文

所以我正在为 iOS 制作一个静态库。基本上我希望人们能够调用这样的东西:

[MyStaticLibrary showview]

我的静态库将子视图添加到他们当前的视图中。通常我可能会这样做:

CGRect mainframe = [[UIScreen mainScreen] bounds];
UIView *mView = [[UIView alloc] initWithFrame:mainframe];
//Add some stuff to the view
[self.view addSubview:mView];

但是 xcode 正在抱怨,并且理所当然地,我无法从静态库访问 self.view,因为我当前在静态库中使用 NSObject。无论如何,我是否可以从静态库访问“self.view”,以便我可以将视图添加到应用程序屏幕?

So I'm making a static library for iOS. Basically I want people to be able to call something like this:

[MyStaticLibrary showview]

And my static library to add a subview to their current view. Normally I might do something like this:

CGRect mainframe = [[UIScreen mainScreen] bounds];
UIView *mView = [[UIView alloc] initWithFrame:mainframe];
//Add some stuff to the view
[self.view addSubview:mView];

But xcode is complaining, and rightfully so, that I can't access self.view from the static library since I'm currently using an NSObject in my static library. Is there anyway for me to access the "self.view" from my static library so I can add a view to the application screen?

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

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

发布评论

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

评论(1

用心笑 2024-12-09 19:37:44

1) 您可以将 showview 方法调整为 showViewInParentView: 并允许调用者为您提供所需的视图。

2) 您可以使用 [[[UIApplication sharedApplication] keyWindow] addSubview:mView] 将库的视图直接添加到窗口,确保它位于顶部。

我推荐 #1 而不是 #2。

1) You could adapt your showview method to be showViewInParentView: and allow the caller to give you the view you need.

2) You could use [[[UIApplication sharedApplication] keyWindow] addSubview:mView] to add the library's view directly to the window, ensuring that it is on top.

I recommend #1 over #2.

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