简体   繁体   English

如何在IOS通用App中制作弹出菜单

[英]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打开 ViewController.h 文件,并声明以下IBAction方法: 1

- (IBAction)showUserDataEntryForm:(id)sender;

Return to the Interface Builder, and connect that action method to the newly added button.返回到 Interface Builder,并将该操作方法连接到新添加的按钮。

Now, open once again the ViewController.h file, and firstly import the TestViewController class as shown below: 1现在,再次打开ViewController.h文件,首先导入TestViewController类,如下所示: 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此外,通过将ViewController类添加到界面标题行,使其符合TestViewControllerDelegate协议,如下所示: 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.通过采用上面的协议,我们以后就可以使用TestViewController类的delegate方法来获取输入的数据了。

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.现在,让我们进入本节最重要的部分,我们之前声明的IBAction方法的实现以及 popover 控制器的使用。 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.不过对于初学者来说,我们必须为弹出框声明一个私有类属性,所以打开ViewController.m并转到界面的私有部分。 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.现在,直接进入 IBAction 方法实现,我们将在其中初始化并使用上述对象。 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.正如我在本节开头所说的,popover 控制器的特殊特性是能够显示另一个视图控制器的内容,因此我们必须做的第一步是初始化TestViewController类的对象。

- (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.如您所见,在初始化时我们也加载了视图控制器的xib文件,除此之外,我们使我们的类成为testViewController对象的委托。 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.很明显,我们要显示的视图控制器是直接作为参数提供给 popover 控制器的。 That's why we first declared and initialized the TestViewController object.这就是我们首先声明并初始化TestViewController对象的原因。 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); 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.第三个参数指定出现在 popover 控制器上的箭头的方向,指向起源于的按钮。 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.除非你确保弹窗控制器总是显示在同一个地方,否则你最好使用 UIPopoverArrowDirectionAny 参数让系统决定箭头的位置。 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.不要忘记,当改变 iPad 的方向时,popover 控制器可以重新定位到一个新的位置,而且很可能箭头应该指向另一个方向。 Finally, the last parameter specifies whether the popover will appear using animation or not, and usually that value is set to YES.最后,最后一个参数指定是否使用动画显示弹出框,通常该值设置为 YES。

Here's the whole IBAction method:这是整个IBAction方法:

- (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];


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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