从静态库访问 self.view
所以我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 您可以将
showview
方法调整为showViewInParentView:
并允许调用者为您提供所需的视图。2) 您可以使用
[[[UIApplication sharedApplication] keyWindow] addSubview:mView]
将库的视图直接添加到窗口,确保它位于顶部。我推荐 #1 而不是 #2。
1) You could adapt your
showview
method to beshowViewInParentView:
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.