2

当想要在通用应用程序中支持 iOS 4 及更高版本(适用于 iPhone 3G)时,是否有更短的方法来呈现视图控制器?

目前我在下面有这个,但我不喜欢如何需要一个UIPopoverController属性,其中我需要特别是因为如果观看控制器被挑选图像,则需要解雇POLPOVER时。

@interface SASuccessViewController ()
@property (nonatomic, retain) UIPopoverController *myPopoverController;
@end

@implementation SASuccessViewController
-(void)showViewController:(UIBarButtonItem *)sender {
    UIViewController *viewController = [[UIViewController alloc] init];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
            [self presentViewController:viewController animated:NO completion:nil];//iOS 5 and above
        } else {
            [self presentModalViewController:viewController animated:NO]; //iOS 4, deprecated in iOS 6
        }
    } else {
        if (!self.myPopoverController) {
            self.myPopoverController = [[[UIPopoverController alloc] initWithContentViewController:viewController] autorelease];
        } else {
            [self.myPopoverController setContentViewController:viewController];
        }
        [self.myPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
    [viewController release];
}
-(void)dealloc {
    [_popoverController release];
    [super dealloc];
}
@end

考虑到当 iOS 4 是最新的(而不是通用应用程序)时,该方法将是:

-(void)showViewController:(UIBarButtonItem *)sender {
    UIViewController *viewController = [[UIViewController alloc] init];
    [self presentModalViewController:viewController animated:NO];
    [viewController release];
}

我现在看起来很臃肿。有没有更好的方法在通用应用程序上显示视图控制器(更不用说支持 iOS 4)?

4

3 回答 3

1

您可以在 UIViewController 上编写一个类别来执行此操作:

@implementation UIViewController (PresentViewController)

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;

@end

@interface UIViewController (PresentViewController)

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if([super respondsToSelector:@selector(presentViewController:animated:completion:)]){
        [super presentViewController:viewControllerToPresent animated:flag completion:completion];
    } else {
        [super presentModalViewController:viewControllerToPresent animated:flag];
    }
}
于 2012-10-17T00:47:32.587 回答
1

不幸的是,没有更好的方法在通用应用程序中呈现模态视图。你所拥有的逻辑都是非常必要的。不过,这里有一些提示:

  1. presentViewController:animated:考虑到您没有指定完成块,在您删除对 iOS 4.0 的支持之前,坚持使用已弃用的方法可能是可以的。换句话说,你可以这样做:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self presentModalViewController:viewController animated:NO];        
    }
    
  2. 或者,正如其他人所说,您可以为 presentModalViewController 方法创建一个 UIViewController 类别。至少这会清理你的 UIViewController 一点。

  3. 如果您的 SASuccessViewController 直接从 UIViewController 继承,您可以考虑为 SASuccessViewController 创建另一个 UIViewController 来继承。例如,MasterViewController 可以是您的任何视图控制器都可以继承的视图控制器。这里的想法是通过将代码移动到超类来清理视图控制器,同时消除在不同视图控制器中呈现模态控制器时的代码重复。

    @interface MasterViewController ()
    @property (nonatomic, retain) UIPopoverController *popoverController;
    - (void)presentViewController:(UIViewController *)viewController origin:(id)origin;
    @end
    
    @implementation MasterViewController
    @synthesize popoverController;
    
    - (void)presentViewController:(UIViewController *)viewController origin:(id)origin {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
                [self presentViewController:viewController animated:NO completion:nil];//iOS 5 and above
            } else {
                [self presentModalViewController:viewController animated:NO]; //iOS 4, deprecated in iOS 6
            }
        } else {
            if (!self.popoverController) {
                self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:viewController] autorelease];
            } else {
                [self.popoverController setContentViewController:viewController];
            }
    
            if(![self.popoverController isPopoverVisible]) {
                if([origin isKindOfClass:[UIBarButtonItem class]]) {
                    [self.popoverController presentPopoverFromBarButtonItem:origin
                                                   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
                } else {
                    [self.popoverController presentPopoverFromRect:CGRectZero inView:origin permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
                }
            }
        }
    }
    
    @end
    

然后你可以从你的SASuccessViewController

-(void)showViewController:(UIBarButtonItem *)sender {
    [self presentViewController:x origin:sender];
}
于 2012-10-17T15:09:16.180 回答
0

您可以创建您的实例UIViewController并将模态演示样式设置UIModalPresentationStyle为 formsheet 或 currentContext 如果您希望它在弹出窗口中并使用动画transitionStyle然后最终呈现它。我希望这能解决你的问题。

        UIViewController *viewController = [[UIViewController alloc] init];
        [viewController setModalPresentationStyle:UIModalPresentationFormSheet];
        [viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];            
        [self presentViewController:viewController animated:YES completion:nil];

所以如果它不适用于 ios4 我们可以直接使用 presentasmodalview 控制器

于 2012-10-11T04:41:12.623 回答