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 Passing Data Between View (Objective-C,iPhone,iPad)

iOS/iPhone Passing Data Between View (Objective-C,iPhone,iPad) บทความนี้จะได้เรียนรู้การสร้าง View ขึ้นมา 2 ตัว และการ รับ-ส่ง ค่าระหว่าง View โดยจะใช้การสร้าง View ขึ้นมา 2 View และใน View แรกจะทำเป็นช่องกรอกข้อมูล และจะส่งค่าที่ได้จากการกรอกนั้น ไปยัง View ที่สอง โดย Code ทั้งหมดใช้ภาษา Objective-C ทำงานบน iOS และ iPhone

]iOS/iPhone Passing Data Between View

iOS/iPhone Passing Data Between View


ในการรับส่งค่าในภาษา Objective-C นั้น จะใช้หลักการเหมือนกับการเขียนโปรแกรมบนภาษาอื่น ๆ เช่น ในฝั่งปลายทางจะต้องสร้าง property ไว้สำหรับทำการ get/set ค่า และฝั่งส่งก็แค่ทำการ เรียก Class ปลายทาง พร้อม ๆ กับการส่งค่า property ไปเท่านั้น ซึ่งหลักการนี้เราได้เคยใช้บนหลายภาษาเช่น .NET หรือ Java

    NSString *name = [txtName text];
    
   DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
    view2.detailItem = name;
   [self presentViewController:view2 animated:NO completion:NULL];

เป็นตัวอย่างการส่งไปค่าไปยัง Class ของ DetailController ซึ่งมี property ว่า detailItem เป็นตัวรับค่า ดูตัวอย่างเพื่อความเข้าใจ

Xcode บน iPhone

]iOS/iPhone Passing Data Between View

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

]iOS/iPhone Passing Data Between View

เลือกและไม่เลือกดังรูป

]iOS/iPhone Passing Data Between View

จะเห็นว่าตอนนี้มีไฟล์ที่เป็น User Inteface หรือ View เพียง 1 ชุด ซึ่งเราจะเรียกมันว่า View 1 และเราจะสร้างมันขึ้นมาอีก 1 ชุด








]iOS/iPhone Passing Data Between View

คลิกขวาที่ Project เลือก New File...

]iOS/iPhone Passing Data Between View

เลือก Objective-C Class

]iOS/iPhone Passing Data Between View

เลือก และ ไม่เลือกดังรูป โดยให้ Include xib มาด้วย

]iOS/iPhone Passing Data Between View

ได้ไฟล์ View พร้อม Class ขึ้นมาอีก 1 ชุด เราเรียกว่า View 2

]iOS/iPhone Passing Data Between View

ภายใน View 1 ออกแบบหน้าจอ Interface ดังรูป

]iOS/iPhone Passing Data Between View

ภายใน View 2 ออกแบบหน้าจอ Interface ดังรูป

และอย่าลืมทำการเชื่อม IBOutlet และ IBAction ให้เรียบร้อยด้วย








สำหรับ Code ของ Class ทั้ง 4 ไฟล์มีดังนี้

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 UITextField *txtName;
}

- (IBAction)btnSubmit:(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"

#import "DetailController.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 {
    [txtName release];
    [super dealloc];
}

- (IBAction)btnSubmit:(id)sender {
    
    NSString *name = [txtName text];
    
   DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
    view2.detailItem = name;
   [self presentViewController:view2 animated:NO completion:NULL];
    
}

@end



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

#import <UIKit/UIKit.h>

@interface DetailController : UIViewController
{
    IBOutlet UILabel *lblResult;
}

@property (strong, nonatomic) id detailItem;

@end


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

#import "DetailController.h"

@interface DetailController ()

@end

@implementation DetailController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
     lblResult.text = [self.detailItem description];
}

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

- (void)dealloc {
    [lblResult release];
    [super dealloc];
}
@end


สำหรับ Code นั้นคงจะไม่ต้องอธิบาย เพราะสามารถดูแล้วน่าจะเข้าใจได้ในทันที หรือไม่ค่อย ๆ เรียนรู้ คิดว่าไม่ยากจนเกินไป

Screenshot

]iOS/iPhone Passing Data Between View

View 1 ทำการ Input ค่า และกด Submit เพื่อส่งไปยัง View 2

]iOS/iPhone Passing Data Between View

View 2 แสดงค่าที่ส่งมาจาก View 1


.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-29 15:20:35 / 2017-03-25 23:42:32
  Download : Download  iOS/iPhone Passing Data Between View (Objective-C,iPhone,iPad)
 Sponsored Links / Related

 
iOS/iPhone Multiple View (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Change View Startup Open First (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Storyboard and View , Multiple View (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Splash Screen Launch and Apps icons Launcher Example (iPhone,iPad)
Rating :

 
iOS/iPhone Portrait and Landscape Orientation (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)
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 00
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 อัตราราคา คลิกที่นี่