如何在IOS通用应用程序中制作弹出菜单 [英] how to make popup menu in IOS universal App

查看:67
本文介绍了如何在IOS通用应用程序中制作弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何为通用应用程序制作弹出菜单.我曾经为此目的使用过警报,但是我无法嵌入新的字体类型,也无法添加任何图像,因此菜单看起来不太好.谁能告诉我解决这个问题的方法.

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.

警报菜单的代码

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

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

[alert show];
}

推荐答案

PopoverViewController

打开ViewController.h文件,并声明以下IBAction方法: 1

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.

现在,再次打开ViewController.h文件,然后首先导入TestViewController类,如下所示: 1

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

#import "TestViewController.h"

此外,通过将ViewController类添加到接口标题行中,使ViewController类符合TestViewControllerDelegate协议,如下所示: 1

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>

通过采用上述协议,我们稍后将可以使用TestViewController类的委托方法,并获取输入的数据.

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.

现在,让我们进入本节最重要的部分,介绍先前声明的IBAction方法的实现以及弹出控件的用法.对于初学者来说,我们必须为弹出窗口声明一个私有类属性,因此打开ViewController.m并转到接口的私有部分.在其中添加以下属性声明:

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

现在,直接前进到IBAction方法实现,在此我们将初始化并使用上述对象.正如我在本节开头已经说过的那样,popover控制器的特殊特性是能够显示另一个视图控制器的内容,因此我们必须要做的第一步是初始化TestViewController类的对象. /p>

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;

}

如您所见,在初始化时,我们也会加载视图控制器的xib文件,除此之外,我们还使我们的类成为testViewController对象的委托.现在我们有了一个视图控制器,让我们使用我们私下声明的popover控制器对象,并使其最终出现.我们只需要添加三个命令,就可以逐步看到它们.首先,让我们执行初始化:

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

很明显,我们要显示的视图控制器直接作为参数提供给弹出框控制器.这就是为什么我们首先声明并初始化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);

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

最后,让我们显示它:

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

您会注意到,此方法接受四个参数.第一个是弹出框的来源框架,通常是用来显示它的按钮的框架.下一个是弹出窗口控制器将在其中显示的视图.如您所知,并非总是需要在默认视图中显示它,但这是最常见的情况.第三个参数指定出现在弹出式控制器上的箭头的方向,该箭头指向起源于该按钮的方向.除非您确定弹出窗口控制器始终显示在同一位置,否则最好使用UIPopoverArrowDirectionAny参数来让系统确定箭头的位置.别忘了,当改变iPad的方向时,弹出框控制器可以重新放置到新的位置,而且箭头很可能指向另一个方向.最后,最后一个参数指定是否使用动画显示弹出窗口,通常该值设置为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.

这是整个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];


}

这篇关于如何在IOS通用应用程序中制作弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆