顯示具有 textfield 標籤的文章。 顯示所有文章
顯示具有 textfield 標籤的文章。 顯示所有文章

2015年5月11日 星期一

[iOS] 可輸入文字(textfield)之警告視窗(alertview)

又遇到問題,於是找了方法來解決


android上面的alertview十分簡單(custom的部份)

iOS呢?是否一樣簡單?

[ iOS 7 + Xcode 6.3.1 ]


第一次看別人的code:


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"輸入IP" message:@" " delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確認",nil];
    UITextField * txt = [[UITextField alloc] init];
    txt.backgroundColor = [UIColor whiteColor];
    txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
    [alert addSubview:txt];

    [alert show];

結果有跳出警告視窗,但是沒有可以輸入的阿!

於是就繼續查,才發現要加上

alert.alertViewStyle = UIAlertViewStylePlainTextInput;


正確版本:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"輸入IP" message:@" " delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確認",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * txt = [[UITextField alloc] init];
    txt.backgroundColor = [UIColor whiteColor];
    txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
    [alert addSubview:txt];
    [alert show];

當然style也有password/default(無法顯示)/login..之類的,大家可以自行嘗試喔!

end

2015年1月27日 星期二

[iOS] 警告訊息框 + 帳號密碼輸入

練習到這部分又覺得這個功能很實用,於是又放上來了 。

其實跟昨天的那篇沒有差太多,但就是進階版本。(詳情可以看 警號訊息框 )

先放上完成圖 & code




一樣是在viewcontroller.m裡面做修改喔!

詳細的備註程式裡面有寫完整了,如果還不會也是可以發問

//
//  ViewController.m
//  AlertDialog_with_account_n_passwd
//
//  Created by daniel on 2015/1/27.
//  Copyright (c) 2015 daniel. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) viewDidAppear:(BOOL)animated
{
    //create a UIAlertController
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
    //declare a "cancel" button
    UIAlertAction *cacelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler: ^(UIAlertAction *action){
        [self dismissViewControllerAnimated:YES completion:nil];}];
    //declare a "login" button
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"login" style:UIAlertActionStyleDefault
        handler:^(UIAlertAction *action){
            NSString *uid = ((UITextField *) [alertController.textFields objectAtIndex:0]).text;
            NSString *pwd = ((UITextField *) [alertController.textFields objectAtIndex:1]).text;
            NSLog(@"account = %@",uid);
            NSLog(@"password = %@",pwd);}];
    //create first textfield
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
    {textField.placeholder = @"Login";}];
    //create second textfield
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
    {textField.placeholder = @"password";
        textField.secureTextEntry = YES;}];
    
    [alertController addAction:cacelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
                                  
}

@end


就是這樣囉!

END