zoukankan      html  css  js  c++  java
  • iOS开发技巧

    解决方案:

    (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.");
        }
    }
  • 相关阅读:
    m个苹果放入n个篮子
    C++ sort函数用法 C中的qsort
    C常见机试题
    二维数组作为函数参数的几种方法
    远程调用相关技术
    反射 与 序列化
    MFC 三种消息
    【2019.09.13】中秋快乐!来点福大猫咪色图ヽ(=^・ω・^=)丿
    【2019.09.11】用交互式思维导图展示学期规划(软件工程实践2019第二次作业)
    【2019.09.07】审视自我~(软件工程实践2019第一次作业)
  • 原文地址:https://www.cnblogs.com/davidgu/p/5773648.html
Copyright © 2011-2022 走看看