自定义弹出模态视图

(1)设置根控制器

(2)设置主视图控制器

RootViewController.m

#import "ModelViewController.h"
#import "UIViewController+MypresentModal.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(90, 90, 90, 50);
    button.backgroundColor = [UIColor redColor];

    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)buttonAction {

    ModelViewController *modelCtrl = [[ModelViewController alloc] init];
    
    [self myPresentViewController:modelCtrl];
    
}

(3)设置模态视图控制器

ModelViewController.m

#import "UIViewController+MypresentModal.h"

@interface ModelViewController ()

@end

@implementation ModelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(90, 90, 90, 50);
    button.backgroundColor = [UIColor redColor];
    
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)buttonAction {
    
    [self myDismissViewController];
    
}

(4)添加类目

UIViewController+MypresentModal.h

//定义弹出的动画效果
- (void)myPresentViewController:(UIViewController *)viewController;

//自定义关闭模态视图的动画效果
- (void)myDismissViewController;
UIViewController+MypresentModal.m

//定义弹出的动画效果
- (void)myPresentViewController:(UIViewController *)viewController {

    //viewController是模态视图控制器
    
    //设置模态视图的frame
    viewController.view.frame = CGRectMake(320, 0, 320, 480);
    //取得当前的主window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //将视图控制器的视图添加到window上显示
    [window addSubview:viewController.view];
    //开始动画
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    
    self.view.frame = CGRectMake(-320, 0, 320, 480);
    viewController.view.frame = CGRectMake(0, 0, 320, 480);
    
    //关闭动画
    [UIView commitAnimations];
    
    [self addChildViewController:viewController];
    
    
}

//自定义关闭模态视图的动画效果
- (void)myDismissViewController {

    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //开始动画
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    self.view.frame = CGRectMake(320, 0, 320, 480);
    //得到模态视图控制器
    window.rootViewController.view.frame = CGRectMake(0, 0, 320, 480);
    
    [UIView commitAnimations];
    
    [self removeFromParentViewController];
    
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: QML是一种用户界面语言,可以用于开发桌面和移动应用程序。在QML中,可以通过弹出自定义模态对话框来实现与用户的交互。下面是一些实现此功能的步骤: 首先,需要创建一个新的QML文件来定义自定义模态对话框的布局和样式。在该文件中,可以使用标准QML组件来创建一个对话框,如Rectangle,Text和Button等。 其次,需要定义弹出自定义模态对话框的触发器,通常是一个按钮或菜单项。触发器应该有一个onClick事件处理程序,用来显示或隐藏自定义模态对话框。 然后,在onClick事件处理程序中,需要使用Qt Quick Dialogs组件中的Dialog组件来创建自定义模态对话框的实例。该组件提供了许多属性和方法,可以用于设置对话框的标题,内容和按钮等。 最后,在自定义模态对话框的QML文件中,可以定义与用户交互的信号和槽,例如用户单击按钮时触发的事件。通过使用这些信号和槽,可以实现与用户的高级交互。 通过以上步骤,可以在QML中实现弹出自定义模态对话框的功能。在开发过程中,还需要注意遵循QML编程最佳实践,例如尽可能使用绑定和模型-视图分离等技术,以提高代码质量和可维护性。 ### 回答2: QML可以通过弹出自定义模态对话框来交互式地与用户进行交互,这主要是通过QML中提供的Dialog组件完成的。要弹出自定义模态对话框,需要创建一个新的自定义组件,该组件将作为Dialog的contentItem。 首先,创建一个QML文件作为自定义组件的模板。该模板可以包含所需的所有控件和布局,以及与这些控件相关的可视化效果。例如,一个包含一个文本框和两个按钮的自定义组件可能如下所示: ``` import QtQuick 2.0 Item { id: customDialog width: 200 height: 100 Rectangle { width: parent.width height: parent.height color: "lightgray" border.color: "black" border.width: 1 radius: 6 Text { text: "Enter your name:" anchors.centerIn: parent } TextInput { id: nameInput width: parent.width - 20 height: 20 anchors.top: parent.top anchors.topMargin: 20 anchors.horizontalCenter: parent.horizontalCenter } Row { width: parent.width spacing: 10 anchors.bottom: parent.bottom anchors.bottomMargin: 10 anchors.horizontalCenter: parent.horizontalCenter Button { text: "OK" onClicked: parent.accepted() } Button { text: "Cancel" onClicked: parent.rejected() } } } } ``` 该自定义组件包括一个文本输入框,一个“OK”按钮和一个“Cancel”按钮。在应用程序逻辑中,将创建一个与Dialog组件关联的弹出窗口,并将自定义组件添加为该窗口的contentItem。下面是一种可能的方法: ``` import QtQuick 2.0 import QtQuick.Controls 2.3 ApplicationWindow { id: mainWindow width: 300 height: 200 visible: true Button { text: "Open dialog" onClicked: openCustomDialog() } Dialog { id: customDialog title: "Custom dialog" x: (mainWindow.width - width) / 2 y: (mainWindow.height - height) / 2 modal: true contentItem: Item { CustomDialog { id: content anchors.fill: parent } } accepted: { console.log("OK clicked") console.log("Name entered: " + content.nameInput.text) } rejected: { console.log("Cancel clicked") } } function openCustomDialog() { customDialog.open() } } ``` 在这个例子中,当用户单击“Open dialog”按钮时,将调用openCustomDialog()函数,该函数将弹出一个自定义模态对话框。该函数将调用Dialog的open()函数,它将显示模态对话框,直到用户单击“OK”或“Cancel”按钮为止。一旦用户单击了其中一个按钮,Dialog的accepted或rejected信号将触发。在这些信号的处理程序中,可以执行自定义操作,如读取文本输入框中输入的数据。 ### 回答3: QML弹出自定义模态对话框通常需要以下步骤: 1. 创建一个自定义的QML组件,例如MyDialog.qml,并将其放置在对话框中。 2. 在MyDialog.qml中定义一个模态属性,以确保对话框在打开时是模态的。例如: Rectangle { // ... 其他属性 property bool modal: true // ... 其他组件 } 3. 在MyDialog.qml中添加关闭对话框的行为。例如: Button { text: "Close" onClicked: myDialog.close(); } 4. 在主QML文件中,初始化自定义对话框的一个实例,例如: import QtQuick.Controls 2.0 ApplicationWindow { // ... 其他属性 MyDialog { id: myDialog // ... 其他属性 } // ... 其他组件 } 5. 要使对话框出现,你可以向触发它的组件添加一个onClickd行为,例如一个按钮: Button { text: "Open Dialog" onClicked: myDialog.open(); } 上述操作将创建一个自定义的QML组件,MyDialog.qml,它是模态的,并允许通过对话框中添加关闭按钮来关闭。还要在主QML文件中,初始化这个自定义对话框的一个实例,并通过触发组件的onClickd行为,使其出现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值