iOS:Objective C 中的库可以生成弹出窗口并管理交互吗?

发布于 2024-12-02 20:17:29 字数 117 浏览 3 评论 0原文

我想在库中创建一个类,该类将根据请求与用户进行复杂的交互。初始化库的应用程序不会提供任何信息,例如 UIWindow 或任何内容。这在技术上可能吗?我知道我可以做一个简单的弹出窗口,但是如果需要动态构建一些东西呢?谢谢。

I want to make a class in a library that will, upon request, do a complicated interaction with the user. The app that initializes the library will not provide any info like its UIWindow or anything. Is this technically possible? I know I can do a simple popup but what about something that needs to be constructed on the fly? Thanks.

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

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

发布评论

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

评论(2

海夕 2024-12-09 20:17:29

您的库将可以访问像 [UIApplication sharedApplication] 这样的单例,因此如果您想向用户展示某些内容,您可以直接将视图添加到窗口:

- (void)addViewToWindow:(UIView *)aView
{
    UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
    [mainWindow addSubview:aView];
}

也许更好的方法是展示不过,它是在视图控制器内模态地进行的。

Your library will have access to singletons like [UIApplication sharedApplication], so if you want to present something to the user, you can just add your view to the window directly:

- (void)addViewToWindow:(UIView *)aView
{
    UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
    [mainWindow addSubview:aView];
}

Perhaps a better way would be to present it modally from within a view controller, though.

何以畏孤独 2024-12-09 20:17:29

使用 UIViewController 进行任何复杂的用户交互。为了以模态方式呈现视图控制器,您必须找到合适的父视图控制器。
您可以使用 UIWindow-rootViewController 方法访问应用程序的根视图控制器,如下所示:

NSArray *windows = [[UIApplication sharedApplication] windows];
UIViewController *rootViewController = (windows.count > 0) ? [[windows objectAtIndex:0] rootViewController] : nil;
if (rootViewController)
    // Present your view controller modally
else
    // Unable to present any view controller

您还可以提供委托方法并使用它来获取视图控制器:

- (UIViewController *) rootViewControllerForComplexClass:(id)complexClass;  

在您的库中,您可以访问委托并访问视图控制器,如下所示:

UIViewController *rootViewController = [self.delegate rootViewControllerForComplexClass:self];
// Present your view controller modally

Use UIViewController for any complex user interaction. In order to present a view controller modally, you'll have to find a suitable parent view controller.
You can get access to the app's root view controller using the -rootViewController method of UIWindow like that:

NSArray *windows = [[UIApplication sharedApplication] windows];
UIViewController *rootViewController = (windows.count > 0) ? [[windows objectAtIndex:0] rootViewController] : nil;
if (rootViewController)
    // Present your view controller modally
else
    // Unable to present any view controller

You can also provide a delegate method and use this to get a view controller:

- (UIViewController *) rootViewControllerForComplexClass:(id)complexClass;  

In your library you have access to the delegate and access the view controller like that:

UIViewController *rootViewController = [self.delegate rootViewControllerForComplexClass:self];
// Present your view controller modally
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文