iOS开发技巧 - 使用Alerts和Action Sheets显示弹出框

解决方案:

(Swift)

使用UIAlertController类

 

(Objective-C)

使用UIAlertView类

 

代码:

(Swift)

import UIKit

class ViewController: UIViewController {
    // 1. define the variable that will hold our alert controller
    var controller:UIAlertController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 2. start constructing a simple alert view controller using the alert view style
        controller = UIAlertController(title: "Title",
            message: "Message",
            preferredStyle: .Alert)
        
        // 3. simply print out a text to the console when pressed
        let action = UIAlertAction(title: "Done",
            style: UIAlertActionStyle.Default,
            handler: {
                (paramAction:UIAlertAction!) in
                println("The Done button was tapped")
            })
    
        // 4. add the action that we created to the alert controller
        controller!.addAction(action)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 5. present the alert controller
        self.presentViewController(controller!, animated: true, completion: nil)
    }
}

 

(Objective-C)

#import "ViewController.h"

@interface ViewController () <UIAlertViewDelegate>
@end

@implementation ViewController
...

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *message = @"Are you sure you want to open this link in Safari?";
    
    UIAlertView *alertView = [[UIAlertView alloc]
        initWithTitle:@"Open Link"
        message:message
        delegate:self
        cancelButtonTitle:@"No"
        otherButtonTitles:@"Yes", nil];
    
    [alertView show];
}

- (void) alertView:(UIAlertView *)alertView
    clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([buttonTitle isEqualToString:@"Yes"]){
        NSLog(@"User pressed the Yes button.");
    }
    else if ([buttonTitle isEqualToString:@"No"]){
        NSLog(@"User pressed the No button.");
    }
}

 

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/303420

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。