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

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年10月28日 星期日

[ iOS ] 計時器(Timer)及進度列視圖(Progress Views)簡易教學

1.建立專案,資訊如下:
Product Name:progressView
Company Identifier: tw.edu.nkut
Class Prefix: DLI
Device Family: iPhone
取消/設定下列選項
取消:Use Storyboards
設定:Use Automatic Reference counting
取消:Include Unit Tests
2.打開DLIViewController.xib,在UI上放上UIActivityIndicationView及UIProgressView兩個物件,並建立Referencing Outlets,分別命名為activityProgress及progressBar。




3.建立程式
4.執行結果


2012年10月20日 星期六

Objective C 教學資源分享

學APPs基礎功很重要,對於想設計iPhone APPs記得要熟讀Objective C,可參考下列教材。

Objective-C 教材

[ iOS ] 切換畫面簡易教學

1. 建立一個switch專案

2.新增檔案
 3.選擇Objective C class,按下Next鍵。
4. 將類別名稱調整為DLIView2Controller,Subclass of 選擇UIViewController,勾選with XIB for user interface選項,按下Next鍵。
 5.選擇Create鍵。
 6.點選DLIViewController.xib,放上Button,並建立switch事件程序。
 7.輸入程式
DLIViewController.m程式列表如下:

#import "DLIViewController.h"
#import "DLIView2Controller.h"

@interface DLIViewController ()

@end

@implementation DLIViewController

- (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.
}

- (IBAction)switch:(id)sender {
    DLIView2Controller *controller = [[DLIView2Controller alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}
@end



8.執行結果

9.點選DLIView2Controller.xib增加一個按鈕,建立一個back事件程序。
10.輸入程式

//
//  DLIView2Controller.m
//  switch
//
//  Created by Lin Cheng-Min on 12/10/20.
//  Copyright (c) 2012 Lin Cheng-Min. All rights reserved.
//

#import "DLIView2Controller.h"
#import "DLIViewController.h"

@interface DLIView2Controller ()

@end

@implementation DLIView2Controller

- (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.
}

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

- (IBAction)back:(id)sender {
    DLIViewController *controller = [[DLIViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:controller animated:YES completion:NULL];
}
@end
11.執行結果