简体   繁体   中英

how to make popup menu in IOS universal App

I need to know how to make the popup menu for a universal app.I had used alert for that purpose,but i was not able to embed new font type and not able to add any images thus the menu look not good.Could any one tell me way around this problem.

这是我创建的菜单的图片

The code for the alert menu

- (IBAction)setting:(UIBarButtonItem *)sender {

UIAlertView *alert = [[UIAlertView alloc]
                     initWithTitle:@"དཀར་ཆག"
                      message:@"\nགང་རུང་ཞིག་འདེམས་རོགས།"
                      delegate:self
                      cancelButtonTitle:@"ཕྱིར་འཐེན།"
                      otherButtonTitles:@"ཉེ་འཆར།",@"དགའ་མོས།",@"ཉེ་འཆར་གཙང་བཟོ།",@"དགའ་མོས་གཙང་བཟོ།",nil];

[alert show];
}

PopoverViewController

open the ViewController.h file, and declare the following IBAction method: 1

- (IBAction)showUserDataEntryForm:(id)sender;

Return to the Interface Builder, and connect that action method to the newly added button.

Now, open once again the ViewController.h file, and firstly import the TestViewController class as shown below: 1

#import "TestViewController.h"

Also, make the ViewController class conform to the TestViewControllerDelegate protocol by adding it to the interface header line like below: 1

@interface ViewController : UIViewController <UIActionSheetDelegate, TestViewControllerDelegate>

By adopting the above protocol, we'll be able to use the delegate method of the TestViewController class later on and to get the entered data.

Now, let's go to the most essential part of this section, to the implementation of the IBAction method we previously declared and to the usage of the popover controller. For starters though, we must declare a private class property for the popover, so open the ViewController.m and go to the private section of the interface. In there, add the following property declaration:

@interface ViewController ()

@property (nonatomic, strong) UIPopoverController *userDataPopover;

@end

Now, move straight ahead to the IBAction method implementation, where we will initialize and use the above object. As I already said in the beginning of this section, the special characteristic of the popover controller is the ability to display the contents of another view controller, therefore the first step we must make is to initialize an object of the TestViewController class.

- (IBAction)showUserDataEntryForm:(id)sender {
    TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
    testViewController.delegate = self;

}

As you see, upon initialization we load the view controller's xib file as well, and beyond that, we make our class the delegate of the testViewController object. Now that we have a view controller on our hands, let's use the popover controller object we privately declared and let it finally appear. We only need to add three commands, which we'll see step by step. Initially, let's perform the initialization:

self.userDataPopover = [[UIPopoverController alloc] initWithContentViewController:testViewController];

It's obvious that the view controller we want to display is directly given as a parameter to the popover controller. That's why we first declared and initialized the TestViewController object. The next step is to define the popover size, which usually matches the contained view's size:

self.userDataPopover.popoverContentSize = CGSizeMake(320.0, 400.0);

Finally, let's display it:

[self.userDataPopover presentPopoverFromRect:[(UIButton *)sender frame]
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny
                                     animated:YES];

This method accepts four parameters as you can notice. The first one is the frame from which the popover originates, and usually is the button's frame that is used to present it. The next one, is the view in which the popover controller will appear to. As you understand, it's not always necessary to display it in the default view, but that's the most common case. The third parameter specifies the direction of the arrow that appears on the popover controller, pointing to the button that originates from. Unless you make sure that the popover controller will always be displayed on the same place, then you'd better use UIPopoverArrowDirectionAny parameter to allow the system decide the position of the arrow. Don't forget that when changing iPad's orientation the popover controller can be re-positioned to a new place, and is quite possible that the arrow should point towards another direction. Finally, the last parameter specifies whether the popover will appear using animation or not, and usually that value is set to YES.

Here's the whole IBAction method:

- (IBAction)showUserDataEntryForm:(id)sender {
    TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
    testViewController.delegate = self;

    self.userDataPopover = [[UIPopoverController alloc] initWithContentViewController:testViewController];
    self.userDataPopover.popoverContentSize = CGSizeMake(320.0, 400.0);
    [self.userDataPopover presentPopoverFromRect:[(UIButton *)sender frame]
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny
                                     animated:YES];


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM