Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:47:28+00:00 2026-06-12T21:47:28+00:00

Is there a shorter way to present a view controller, when wanting to support

  • 0

Is there a shorter way to present a view controller, when wanting to support iOS 4 and above (for the iPhone 3G) in a universal app?

Currently I have this below, but I dislike how I need a UIPopoverController property, which I need especially because if the view controller is say for picking an image, when an image is picked the popover needs to be dismissed.

@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

Considering that when iOS 4 was the latest (and not a Universal app), the method would be just:

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

What I have now seem quite bloated. Is there a better way of showing a view controller on Universal apps (let alone supporting iOS 4)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:47:31+00:00Added an answer on June 12, 2026 at 9:47 pm

    Unfortunately there is not a much better way to present modal view in a universal app. The logic you have is all pretty much necessary. Here are some tips, though:

    1. Considering that you are not specifying a completion block, it is probably okay to stick with the deprecated presentViewController:animated: method until you remove support for iOS 4.0. In other words, you could just do this:

      if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
          [self presentModalViewController:viewController animated:NO];        
      }
      
    2. Or, as someone else stated, you could create a UIViewController category for the presentModalViewController methods. At least this will clean up your UIViewController a bit.

    3. If your SASuccessViewController inherits directly from UIViewController, you might consider creating another UIViewController for SASuccessViewController to inherit from. For example, MasterViewController could be a view controller that any of your view controller’s could inherit from. The idea here is to clean up your view controllers by moving the code to your super class, and at the same time eliminate repetition of code when presenting modal controllers in different view controllers.

      @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
      

    And then you could just do this from your SASuccessViewController:

    -(void)showViewController:(UIBarButtonItem *)sender {
        [self presentViewController:x origin:sender];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

string = Yellow puts string[1..string.length] -> outputs ellow Is there a shorter way to
All of my string delete's with regex use gsub, is there a shorter way?
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better shorter way than iterating over the array? int[] arr =
I'm wondering if there's a shorter way to write this: var x = 1;
Is there a better (shorter?) way than the following? let cpucount = System.UInt16.Parse( reader.GetInt32(3).ToString()
Is there a way to make this line shorter? bool pass = d !=
Is there a way to create a shorter alias for an EditorAttribute? Instead of:
is there a shorter way to do this: List<int> mins = new List<int>(); for(int
results_histogram_total=list(numpy.histogram(freq,bins=numpy.arange(0,6.1,.1))[0]) sum_total=sum(results_histogram_total) big_set=[] for i in results_histogram_total: big_set.append(100*(i/sum_total) is there a shorter way i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.