iOS新特性

来源:互联网 发布:类似omnifocus的软件 编辑:程序博客网 时间:2024/05/17 09:15
@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你有严重的精神病,赶紧去治疗" preferredStyle:UIAlertControllerStyleActionSheet];
    // 设置popover指向的item
    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    
    // 添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定按钮");
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}
// UIAlertControllerStyleActionSheet的使用注意
// 1.不能有文本框
// 2.在iPad中,必须使用popover的形式展示


// Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
// 只能在UIAlertControllerStyleAlert样式的view上添加文本框


- (void)alertController
{
    // 危险操作:弹框提醒
    // 1.UIAlertView
    // 2.UIActionSheet
    // iOS8开始:UIAlertController == UIAlertView + UIActionSheet
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你有严重的精神病,赶紧去治疗" preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    //    [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    //        NSLog(@"点击了其它按钮");
    //    }]];
    
    // 添加文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.textColor = [UIColor redColor];
        textField.text = @"123";
        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
        //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.secureTextEntry = YES;
        textField.text = @"123";
    }];
    
    [self presentViewController:alert animated:YES completion:nil];
}


- (void)usernameDidChange:(UITextField *)username
{
    NSLog(@"%@", username.text);
}


- (void)actionSheet
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:你有严重的精神病,赶紧去治疗" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定"
                                              otherButtonTitles:@"关闭", nil];
    [sheet showInView:self.view];
}


- (void)alertView
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你有严重的精神病,赶紧去治疗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [alert show];
}


@end








//POP出一个新的界面
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISlider *slider;


@end


@implementation ViewController
// UIPopoverController只能用在iPad


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    HMSecondViewController *vc = [[HMSecondViewController alloc] init];
    // modal出来是个popover
    vc.modalPresentationStyle = UIModalPresentationPopover;
    // 取出vc所在的UIPopoverPresentationController
    vc.popoverPresentationController.sourceView = self.slider;
    vc.popoverPresentationController.sourceRect = self.slider.bounds;
    [self presentViewController:vc animated:YES completion:nil];
}


- (void)popover
{
    HMSecondViewController *vc = [[HMSecondViewController alloc] init];
    
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:vc];
    [popover presentPopoverFromRect:self.slider.bounds inView:self.slider permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}


@end




//实例化另一个界面
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    HMSecondViewController *second = [[HMSecondViewController alloc] init];
//    second.modalPresentationStyle = UIModalPresentationPopover;
//    second.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    [self presentViewController:second animated:YES completion:nil];
    NSLog(@"%@ %@", second.presentationController, second.popoverPresentationController);
}


// 1.只要调用了[self presentViewController:second animated:YES completion:nil];方法
// 2.首先会创建一个UIPresentationController
// 3.然后由UIPresentationController管理控制器的切换




//销毁一个界面
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
    // Dispose of any resources that can be recreated.
}




//界面设置按钮的使用
    // Do any additional setup after loading the view, typically from a nib.
    
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];



























0 0
原创粉丝点击