2012年11月3日 星期六

[ iOS ] 兩個TableView相互切換

1.執行結果如下;

2.DLIViewController.h
//
//  DLIViewController.h
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import

@interface DLIViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *listData;
@end


3.DLIViewController.m

//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"
#import "DLIViewController2.h"

@interface DLIViewController ()

@end

@implementation DLIViewController
@synthesize listData;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    listData = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    UIImage *cellImage = [UIImage imageNamed:@"nkut_logo.jpg"];
    cell.imageView.image = cellImage;
    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLIViewController2 *controller = [[DLIViewController2 alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}
@end

4. DLIViewController2.h
//
//  DLIViewController2.h
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import

@interface DLIViewController2 : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableView2;
@property (strong, nonatomic)NSArray *listData2;
@end

5. DLIViewController2.m
//  DLIViewController2.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"
#import "DLIViewController2.h"

@interface DLIViewController2 ()

@end

@implementation DLIViewController2
@synthesize listData2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    listData2 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];

    
}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData2 count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData2 objectAtIndex:indexPath.row];

    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.textLabel.text = [listData2 objectAtIndex:[indexPath row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLIViewController *controller = [[DLIViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}

@end

[ iOS ] TableView和WebView 的整合

1. 參考 http://cheng-min-i-taiwan.blogspot.tw/2012/10/ios-webview.htmlhttp://cheng-min-i-taiwan.blogspot.tw/2012/11/ios-tableview_2055.html
2. 打開DLIViewController2.xib檔,加入WebView物件。
 3.增加Referencing Outlet。
 4.程式列表


//
//  DLIViewController2.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController2.h"

@interface DLIViewController2 ()

@end

@implementation DLIViewController2
@synthesize webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    
}

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

@end

5. 執行結果




[ iOS ] TableView簡易教學 ﹣ 選擇後切換畫面

2. 選擇File->New 建立第二個畫面程式。

 (記得要更名)
 (按下Create鈕)
 3.增加TableView被選擇後的程式(DLIViewController.m 中藍色字)

//
//  DLIViewController.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"
#import "DLIViewController2.h"

@interface DLIViewController ()

@end

@implementation DLIViewController
@synthesize listData;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    listData = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    UIImage *cellImage = [UIImage imageNamed:@"nkut_logo.jpg"];
    cell.imageView.image = cellImage;
    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLIViewController2 *controller = [[DLIViewController2 alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}
@end

4. 執竹結果
(畫面切換)

[ iOS ] TableView 簡易教學 ﹣ 選擇後的動作

1. 參考前一篇說明 http://cheng-min-i-taiwan.blogspot.tw/2012/11/ios-tableview_3.html
2. 增加TableView被選擇後的程式(DLIViewController.m 中藍色字)

//
//  DLIViewController.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"

@interface DLIViewController ()

@end

@implementation DLIViewController
@synthesize listData;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    listData = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    UIImage *cellImage = [UIImage imageNamed:@"nkut_logo.jpg"];
    cell.imageView.image = cellImage;
    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * string=[listData objectAtIndex:[indexPath row]];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
    [alert show];
}
@end


3.執行結果

[ iOS ] TableView 簡易教學 ﹣ 放置小圖示

1.請參考前一篇教學
2.把圖片拖拉至專案中。
3.新增圖示程式 (藍色字)


//
//  DLIViewController.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"

@interface DLIViewController ()

@end

@implementation DLIViewController
@synthesize listData;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    listData = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    UIImage *cellImage = [UIImage imageNamed:@"nkut_logo.jpg"];
    cell.imageView.image = cellImage;
    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
}
@end




[ iOS ] TableView 簡易教學

1.建立專案
2.輸入專案名稱


 3.選擇專案要儲存位置
4.選擇DLIViewController.m檔案

 5.放入UITableView物件
 6.連結Referencing Outlet
 7.連結
  • 將 TableView 的 dataSource Outlet 跟 File Owner 做連結
  • 將 TableView 的 delegate Outlet 跟 File Owner 做連結
 8. 打開DLIViewControll.h,輸入藍色字程式

//
//  DLIViewController.h
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import

@interface DLIViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *listData;
@end


9.打開 DLIViewControll.m,輸入藍色字程式
//  DLIViewController.m
//  tableView
//
//  Created by Lin Cheng-Min on 12/11/3.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIViewController.h"

@interface DLIViewController ()

@end

@implementation DLIViewController
@synthesize listData;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    listData = [[NSArray alloc] initWithObjects:@"a",@"b",@"c", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *tableIdentifier = @"Simple table";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
}
@end
 10. 執行

2012年11月1日 星期四

Android USB 應用之 HelloUsbUart(RS-232)

在NDK這篇HelloUart中,有人詢問到開發平台轉移至智慧型手機上應該如何實現呢??
前面寫了一個方法是透過無線藍牙 -- Android Bluetooth 應用之 HelloBTUart(RS-232)
本文延續這個問題,提供了第二個答案就是直接使用有線的USB來做 Virtual COM port (VCP) 。

Android自從Honeycomb (3.1)版本後就開始支援USB hosting機制,也就是說從3.1版後Android提供了android.hardware.usb類別來使用,把早先Android從Accessory的角色變成Host及Accessory(Device)兩種都可以的角色。
至目前為止,Android在USB hosting機制從一開始只支援USB Keyboard、Mouse到現在Jelly Bean陸續已經可以支援到USB儲存裝置、攝影機、網卡、搖桿、....等總類非常繁多。
所以,本篇實作的部分主要是如何在Android上使用USB裝置(UsbDevice API)。