Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Username and Password Popup - UIAlertView (Objective-C,iPhone)

iOS/iPhone Username and Password Alert Popup - UIAlertView (Objective-C,iPhone,iPad) บทความนี้จะเป็นตัวอย่างการสร้าง Popup (UIAlertView) บน iOS แบบมีช่องกรอกรับค่า Username และ Password โดยจะมีประโยชน์ในกรณีที่ทำเป็นระบบ Login สมาชิก หรือ ID โดยเราสามารถอ่านค่าของ Username และ Password เพื่อนำไปตรวจสอบกับระบบอื่น ๆ ได้

iOS/iPhone  Username and Password Alert Popup

Username and Password Alert Popup


ใน iOS 5 เป็นต้นไปการสร้าง Text Box (UITextField) บน Alert Popup (UIAlertView) นันสามารถทำได้ง่ายมาก ๆ แค่เพียงการกำหนด Property ในส่วนของ setAlertViewStyle ก็สามารถสร้างรูปแบบได้เพียงง่าย ๆ ตามตัวอย่างนี้

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Login"
                                                      message:@"กรุณากรอก ชื่อผู้ใช้ และ รหัสผ่าน "
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"Login", nil];
    
    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    
    [alert show];
    [alert release];

การรับค่า
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
        UITextField *username = [alertView textFieldAtIndex:0];
        UITextField *password = [alertView textFieldAtIndex:1];
        lblUsername.text = [username text];
        lblPassword.text = [password text];;
}


ทดสอบบน Xcode กับ iPhone

iOS/iPhone  Username and Password Alert Popup

เริ่มต้นการสร้าง Project ง่าย ๆ ด้วย Single View Application

iOS/iPhone  Username and Password Alert Popup

กำหนดชื่อ Project และเลือกดังภาพ

iOS/iPhone  Username and Password Alert Popup

ออกแบบหน้าจอ Interface ดังรูป โดยทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อย








Code สำหรับ .h และ .m

ViewController.h
//
//  ViewController.h
//
//  Created by Weerachai on 10/27/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UILabel *lblPassword;
    IBOutlet UILabel *lblUsername;
}

- (IBAction)btnLogin:(id)sender;

@end

ViewController.m
//
//  ViewController.m
//
//  Created by Weerachai on 10/27/55 BE.
//  Copyright (c) 2555 Weerachai. 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)dealloc {           
    [lblUsername release];
    [lblPassword release];
    [super dealloc];
}

- (IBAction)btnLogin:(id)sender {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Login"
                                                      message:@"กรุณากรอก ชื่อผู้ใช้ และ รหัสผ่าน "
                                                     delegate:self
                                            cancelButtonTitle:@"Cancel"
                                            otherButtonTitles:@"Login", nil];
    
    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    
    [alert show];
    [alert release];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    
    if([title isEqualToString:@"Login"])
    {
        UITextField *username = [alertView textFieldAtIndex:0];
        UITextField *password = [alertView textFieldAtIndex:1];
        lblUsername.text = [username text];
        lblPassword.text = [password text];;
    }
    
    if([title isEqualToString:@"Cancel"])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!"
                                                        message:@"Your Select Cancel"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    
}

@end


Screenshot

iOS/iPhone  Username and Password Alert Popup

คลิกที่ Button เพื่อเปิด Popup

iOS/iPhone  Username and Password Alert Popup

แสดง Popup แบบมีช่อง Username และ Password

iOS/iPhone  Username and Password Alert Popup

การอ่านค่าจาก Popup แสดงบน Label


เพิ่มเติม

iOS/iPhone  Username and Password Alert Popup

จะเห็นว่าการสร้างรูปแบบ Style ของ UIAlertView สามารถกำหนดง่าย ๆ ได้เพียงการปรับแก้ไข Property เท่านั้น

iOS/iPhone  Username and Password Alert Popup

รายการ Property ต่าง ๆ ที่สามารถใช้ได้

iOS/iPhone  Username and Password Alert Popup

ทดสอบกับ Property Style อื่น ๆ

iOS/iPhone  Username and Password Alert Popup

ทดสอบกับ Property Style อื่น ๆ







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-29 15:14:48 / 2017-03-25 22:42:16
  Download : Download  iOS/iPhone  Username and Password Popup - UIAlertView (Objective-C,iPhone)
 Sponsored Links / Related

 
การสร้าง Message Box หรือ Alert ด้วย UIAlertView บน iOS (iPhone,iPad)
Rating :

 
iOS/iPhone Confirm Dialog Popup - UIAlertView (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone Textbox in Popup - UITextField / UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Dialog Popup and Optional Item - UIAlertView (Objective-C,iPhone)
Rating :

 
iOS/iPhone Action Sheet Popup - UIActionSheet (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone Adding Show Image Action Sheet (UIActionSheet) and Alert View (UIAlertView)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่