开发者

iOS: Can a library in Objective C produce a pop up window and manage an interaction?

I want to 开发者_StackOverflow中文版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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜