此範例目的是點擊按鈕顯示文字的應用,並計算及顯示按了幾下

1.stroryboard中新增一個button與label, 並與viewController.h檔建立程式關聯性(若不知道如何建立關聯性可參考範例一

   button設定為action, label設定為outlet

2.在viewController.h檔中新增一個iVar,命名為count並紀錄次數

3.並在viewController.m檔中設定初始值為0

4.每點擊一次按鈕計數加一,並將結果顯示於螢幕上

 

//viewController.h內容如下

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    int count;
}

- (IBAction)toClick:(id)sender;

@property (weak, nonatomic) IBOutletUILabel *showArea;

@end

//viewController.m內容如下

#import "ViewController.h"

@interfaceViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [superviewDidLoad];
    count = 0;
    _showArea.text=[NSStringstringWithFormat:@"%d", count];
}

- (IBAction)toClick:(id)sender {
    count++;
    _showArea.text=[NSStringstringWithFormat:@"%d", count];
}

@end

2.點擊按鈕顯示文字的應用-計算及顯示按了幾下  

arrow
arrow

    Will 發表在 痞客邦 留言(0) 人氣()