SlideShare ist ein Scribd-Unternehmen logo
1 von 47
Class 03
iOS 應⽤用軟體設計
課程⼤大綱
•       ⽂文字的輸⼊入 (UITextField)
    •    QV061:BMI 計算
•       UISwitch, UISlider
    •    QV005:UISwitch 及 UISlider 基本⽤用法
    •    QV013:利⽤用三個 UISlider 改變背景⾊色
•       UIView
    •    QV074:UIView (Container)
•       以程式語法產⽣生各種 UIView 元件
    •    QV075:只是擺放
    •    QV076:程式語⾔言的⽅方式
    •    Samples:UICatalog
⽂文字的輸⼊入

UITextField ⽂文字
Project QV061


輸⼊入⾝身⾼高和體重,計算
BMI 之值
UITextField 使⽤用
ViewController.h




@interface ViewController : UIViewController
{
    IBOutlet UITextField *textHeight, *textWeight;
    IBOutlet UILabel *display;
}

- (IBAction) calcBMI:(id)sender;
- (IBAction) doneEdit:(id)sender;

@end
ViewController.xib
ViewController.m



- (IBAction) calcBMI:(id)sender
{
    float h = [[textHeight text] floatValue];
    float w = [textWeight.text floatValue];
    float bmi = w / ((h/100)*(h/100));
    display.text = [NSString stringWithFormat:@"%5.2f", bmi];
}
虛擬鍵盤不會隱藏的問題解決



                                     要注意各個 UITextFiled
                                     的 Did End On Exit 事件
                                     要指定動作,以隱藏輸⼊入鍵盤
- (IBAction) doneEdit:(id)sender
{
    [sender resignFirstResponder];
}
與鍵盤按鍵有關的⼩小問題


- (IBAction) calcBMI:(id)sender
{
    float h = [[textHeight text] floatValue];
    float w = [textWeight.text floatValue];
    float bmi = w / ((h/100)*(h/100));
    display.text = [NSString stringWithFormat:@"%5.2f", bmi];


    [textHeight resignFirstResponder];                隱藏輸⼊入鍵盤
    [textWeight resignFirstResponder];
}

- (IBAction) doneEdit:(id)sender
{
    [sender resignFirstResponder];
}
                                                 設定鍵盤類型
- (void)viewDidLoad
{
    [super viewDidLoad];

    [textHeight setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
    [textWeight setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
}
數值,開關資料的輸⼊入
UISlider 類別

• 主要屬性
 • value
 • minimumValue
 • maximumValue
 • continuous
UISwitch 類別

• 主要屬性
 • on
  開關的狀態 (BOOL值)
範例:QV005


  UISlider 及 UISwitch
        基本⽤用法
Project QV005


UI 元件 Slider
UI 元件 Switch
ViewController.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UILabel *messageSlider;
    IBOutlet UILabel *messageSwitch;
}                                              ViewController.xib
-(IBAction)changeSlider:(id)sender;
-(IBAction)changeSwitch:(id)sender;

@end
ViewController.xib
#import "ViewController.h"
                                                 ViewController.m
@implementation ViewController

-(IBAction)changeSlider:(id)sender
{
    UISlider *thisSlider = (UISlider *)sender;
    int number = (int)(thisSlider.value);
    NSString *msg = [[NSString alloc] initWithFormat:@"%d", number];
    messageSlider.text = msg;
}

-(IBAction)changeSwitch:(id)sender
{
    UISwitch *thisSwitch = (UISwitch *)sender;
    if(thisSwitch.on)
    {
        messageSwitch.text = @"ON";
    }
    else
    {
        messageSwitch.text = @"OFF";
    }
}

@end
範例:QV013


   利⽤用三個 UISlider
     改變背景⾊色
Project QV013

 三組 UISlider
 更換背景顏⾊色
 早期知名的⼿手電筒程式
ViewController.h                  注意此數值需與
                                  程式互相配合




                   三個都執⾏行同⼀一段函式
ViewController.h




#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UISlider *sliderR;
    IBOutlet UISlider *sliderG;
    IBOutlet UISlider *sliderB;
}

@property (retain, nonatomic) IBOutlet UISlider *sliderR;
@property (retain, nonatomic) IBOutlet UISlider *sliderG;
@property (retain, nonatomic) IBOutlet UISlider *sliderB;

-(IBAction)sliderChange:(id)sender;

@end
ViewController.m



                                                 顏⾊色的值為
-(IBAction)sliderChange:(id)sender           0 到 1 之間的浮點數
{
    float valueR = [sliderR value];
    float valueG = [sliderG value];
    float valueB = [sliderB value];

     UIColor *myColor = [UIColor colorWithRed:(valueR/255.0)
                                 green:(valueG/255.0)
                                 blue:(valueB/255.0)
                                 alpha: 1];

     self.view.backgroundColor = myColor;
}
範例:QV074

   UIView 的包容
  (container) 處理
Project QV074

利⽤用 UIView 包容多個
UIView ⼦子物件
UISwitch 及 UISlider
UIView 物件位置移動
ViewController.h



       #import <UIKit/UIKit.h>

       @interface ViewController : UIViewController
       {
           IBOutlet UISwitch *mySwitch;

           IBOutlet UIView *myView;
           IBOutlet UISlider *mySlider1, *mySlider2;
           IBOutlet UILabel *myLabel1, *myLabel2;
       }

       - (IBAction) changeSwitch:(id)sender;
       - (IBAction) changeSlider:(id)sender;

       @end


將多個物件包在⼀一塊
ViewController.xib




注意物件擺放的層次關係
ViewController.m (1/2)
    切換 UISwitch 以控制 UIView,
         會整個顯⽰示或隱藏


- (IBAction) changeSwitch:(id)sender
{
    UISwitch *thisSwitch = (UISwitch *)sender;

     if(thisSwitch.on)
     {                                     內部宣告明確類別的物
         myView.hidden = NO;
     }                                      件,取得傳⼊入的指標
     else
     {
         myView.hidden = YES;
     }
}
                                            取得 UISwitch 的輸⼊入
                                                    xxxxx.on
                                                 (BOOL 型態)
ViewController.m (2/2)



- (IBAction) changeSlider:(id)sender
{
    myLabel1.text = [NSString stringWithFormat:@"%3.1f", mySlider1.value];
    myLabel2.text = [NSString stringWithFormat:@"%3.1f", mySlider2.value];

    float x = 320 * mySlider1.value;
    float y = 460 * (1.0-mySlider2.value);

    // 因為不能改寫center.x及center.y。所以改⽅方法
    mySwitch.center = CGPointMake(x, y);
}




               移動 UIView 的位置
                                                取得 UISlider 的輸⼊入
                                                     xxxxx.value
                                                     (float 型態)
各種常⽤用的 UIView 元件
範例:QV075, QV076

以程式語法,產⽣生 UIView 物件
     的⽅方式
Project QV075


常⾒見且簡單的畫⾯面元件
只有畫⾯面擺放
Project QV076

以程式置放 UIView 物件
UILabel, UIButton,
UITextField, UISlider,
UISwitch, UIStepper,
UIImageView, UIWebView
ViewController.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UILabel *myLabel;
}

-   (void)   textChange:(id)sender;      會讓很多程式⽚片段使
-   (void)   doneEdit:(id)sender;
-   (void)   buttonClick:(id)sender;      ⽤用,故宣告在此
-   (void)   sliderChange:(id)sender;
-   (void)   switchChange:(id)sender;
-   (void)   stepperChange:(id)sender;

@end
ViewController.m (1/9)
       UILabel


- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UILabel (注意,myLabel已定義在類別中)
    myLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 30)];

    myLabel.textAlignment = UITextAlignmentCenter;
    myLabel.font = [UIFont fontWithName:@"Arial" size:20];
    myLabel.backgroundColor = [UIColor clearColor];
    myLabel.text = @"訊息⽂文字列";

    [self.view addSubview:myLabel];

}
ViewController.m (2/9)
    UIButton + UIAlert

- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UIButton
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    myButton.frame = CGRectMake(30, 70, 72, 37);
    [myButton setTitle:@"Click me" forState:UIControlStateNormal];
    [myButton addTarget:self
                 action:@selector(buttonClick:)
       forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:myButton];
                                                          UIAlertView 使⽤用
}

- (void) buttonClick:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"現在狀態"
                message:@"按下了按鈕" delegate:self
                cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
ViewController.m (3/9)
    UITextField (1/2)

- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UITextField
    UITextField *myTextField = [[UITextField alloc] init];
    myTextField.frame = CGRectMake(30, 120, 100, 31); // ⾼高(31) 為固定值

    myTextField.borderStyle = UITextBorderStyleRoundedRect;
    myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    myTextField.contentVerticalAlignment =
                         UIControlContentVerticalAlignmentCenter;
    myTextField.text = @"預設內容";
    myTextField.secureTextEntry = YES;

    [myTextField addTarget:self action:@selector(textChange:)
          forControlEvents:UIControlEventEditingChanged];
    [myTextField addTarget:self action:@selector(doneEdit:)
          forControlEvents:UIControlEventEditingDidEndOnExit];

    [self.view addSubview:myTextField];
}                                                        兩個事件指定
ViewController.m (4/9)
    UITextField (2/2)


- (void) textChange:(id)sender
{
    UITextField *thisTextField = (UITextField *)sender;

    if ([thisTextField.text length] == 0)
    {
        myLabel.text = @"沒有輸⼊入⽂文字";
    }
    else
    {
        myLabel.text = [NSString stringWithFormat:@"⽂文字:%@",
                                              thisTextField.text];
    }
}


- (void) doneEdit:(id)sender                       取得輸⼊入的內容
{
    [sender resignFirstResponder];
}
                                       隱藏虛擬鍵盤
ViewController.m (5/9)
    UISlider

- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UISlider
    CGRect frame1 = CGRectMake(30, 170, 79, 23); // ⾼高(23) 為固定值
    UISlider *mySlider = [[UISlider alloc] initWithFrame:frame1];
    mySlider.minimumValue = 0.0;
    mySlider.maximumValue = 100.0;
    mySlider.value = 50.0;
    [mySlider addTarget:self
                 action:@selector(sliderChange:)
       forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mySlider];
}

- (void) sliderChange:(id)sender
{
    UISlider *thisSlider = (UISlider *)sender;
    float value = thisSlider.value;                 取得輸⼊入的內容
    myLabel.text = [NSString stringWithFormat:@"滑桿值為:%5.2f", value];
}
UISwitch
                                             ViewController.m (6/9)
- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UISwitch
    CGRect frame2 = CGRectMake(30, 220, 79, 27); // 寬(79)⾼高(27)為固定值
    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:frame2];
    mySwitch.on = NO;
    [mySwitch addTarget:self
                 action:@selector(switchChange:)
       forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mySwitch];
}

- (void) switchChange:(id)sender
{
    UISwitch *thisSwitch = (UISwitch *)sender;
    if(thisSwitch.on)
                                                     取得:開或關
    {
        myLabel.text = [NSString stringWithFormat:@"切換鍵為:開 (ON)"];
    }
    else
    {
        myLabel.text = [NSString stringWithFormat:@"切換鍵:關 (不是ON)"];
    }
}
UIStepper          iOS 5 之後提供                  ViewController.m (7/9)

- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UIStepper
    CGRect frame3 = CGRectMake(185, 70, 94, 27); // 寬(94)⾼高(27)為固定值
    UIStepper *myStepper = [[UIStepper alloc] initWithFrame:frame3];
    myStepper.maximumValue = 10.0;
    myStepper.minimumValue = 0.0;
    myStepper.value = 5.5;
    [myStepper setStepValue:0.1]; // 每次改變的量
    [myStepper setContinuous:YES]; // 可以按住不放來連續變更
    [myStepper setWraps:YES]; // 設定是否循環(到最⼤大值時增加數值,從最⼩小值開始)
    [myStepper addTarget:self action:@selector(stepperChange:)
        forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:myStepper];
}

- (void) stepperChange:(id)sender
{
    UIStepper *thisStepper = (UIStepper *)sender;
    float value = thisStepper.value;                   取得輸⼊入的內容

    myLabel.text = [NSString stringWithFormat:@"增減鍵的值為:%4.1f", value];
}
ViewController.m (8/9)
     UIImageView



- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

    // 產⽣生 UIImage
    UIImage *myImage = [UIImage imageNamed:@"Cat.png"];

    // 產⽣生 UIImageView
    UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
    myImageView.frame = CGRectMake(185, 120, 128, 128);
    [self.view addSubview:myImageView];
}


                                            需注意,圖像使⽤用包含
                                          UIImageView (View Control)
                                             及 UIImage (圖檔本⾝身)
ViewController.m (9/9)
    UIWebView


- (void)viewDidLoad
{
    ****** 省略部份程式碼 ******

     // 產⽣生 UIWebView
     UIWebView *myWebView = [[UIWebView alloc]
                       initWithFrame:CGRectMake(30, 270, 270, 165)];

     myWebView.scalesPageToFit = YES;
     [myWebView loadRequest:
                   [NSURLRequest requestWithURL:
                     [NSURL URLWithString:@"http://www.apple.com"]]];

     [self.view addSubview:myWebView];
}




                                                  此處為簡略⽤用法
⾃自我練習
熟悉並區分程式⽤用法及 Interface Builder 的⽤用法
此範例提到之 UIView 類別
   UILabel
   UIButton
   UITextField
   UISlider
   UISwitch
   UIStepper
   UIImageView
   UIWebView
範例觀摩
UICatalog
............

Weitere ähnliche Inhalte

Was ist angesagt?

INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVERINSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVERDarwin Durand
 
"Android Data Binding в массы" Михаил Анохин
"Android Data Binding в массы" Михаил Анохин"Android Data Binding в массы" Михаил Анохин
"Android Data Binding в массы" Михаил АнохинFwdays
 
Михаил Анохин "Data binding 2.0"
Михаил Анохин "Data binding 2.0"Михаил Анохин "Data binding 2.0"
Михаил Анохин "Data binding 2.0"Fwdays
 
Awesome State Management for React and Other Virtual-Dom Libraries
Awesome State Management for React and Other Virtual-Dom LibrariesAwesome State Management for React and Other Virtual-Dom Libraries
Awesome State Management for React and Other Virtual-Dom LibrariesFITC
 
Константин Чернухо_Popups, alerts and windows
Константин Чернухо_Popups, alerts and windowsКонстантин Чернухо_Popups, alerts and windows
Константин Чернухо_Popups, alerts and windowsGeeksLab Odessa
 
Window object methods (timer related)
Window object methods (timer related)Window object methods (timer related)
Window object methods (timer related)Shivani Thakur Daxini
 
Why SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptWhy SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptmartinlippert
 
FormsKit: reactive forms driven by state. UA Mobile 2017.
FormsKit: reactive forms driven by state. UA Mobile 2017.FormsKit: reactive forms driven by state. UA Mobile 2017.
FormsKit: reactive forms driven by state. UA Mobile 2017.UA Mobile
 
Angular Mini-Challenges
Angular Mini-ChallengesAngular Mini-Challenges
Angular Mini-ChallengesJose Mendez
 
Standford 2015 week9
Standford 2015 week9Standford 2015 week9
Standford 2015 week9彼得潘 Pan
 
運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScript運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScripttaobao.com
 
ハンズオン資料 電話を作ろう(v2.x用)
ハンズオン資料 電話を作ろう(v2.x用)ハンズオン資料 電話を作ろう(v2.x用)
ハンズオン資料 電話を作ろう(v2.x用)Kenji Sakashita
 
Building Apps with Flutter - Hillel Coren, Invoice Ninja
Building Apps with Flutter - Hillel Coren, Invoice NinjaBuilding Apps with Flutter - Hillel Coren, Invoice Ninja
Building Apps with Flutter - Hillel Coren, Invoice NinjaDroidConTLV
 
Testando API's de forma unitária mocando as dependências
Testando API's de forma unitária mocando as dependênciasTestando API's de forma unitária mocando as dependências
Testando API's de forma unitária mocando as dependênciasMarcelo Aymone
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 
Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Kamil Augustynowicz
 

Was ist angesagt? (20)

INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVERINSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
 
"Android Data Binding в массы" Михаил Анохин
"Android Data Binding в массы" Михаил Анохин"Android Data Binding в массы" Михаил Анохин
"Android Data Binding в массы" Михаил Анохин
 
Михаил Анохин "Data binding 2.0"
Михаил Анохин "Data binding 2.0"Михаил Анохин "Data binding 2.0"
Михаил Анохин "Data binding 2.0"
 
Awesome State Management for React and Other Virtual-Dom Libraries
Awesome State Management for React and Other Virtual-Dom LibrariesAwesome State Management for React and Other Virtual-Dom Libraries
Awesome State Management for React and Other Virtual-Dom Libraries
 
iOS
iOSiOS
iOS
 
Константин Чернухо_Popups, alerts and windows
Константин Чернухо_Popups, alerts and windowsКонстантин Чернухо_Popups, alerts and windows
Константин Чернухо_Popups, alerts and windows
 
Window object methods (timer related)
Window object methods (timer related)Window object methods (timer related)
Window object methods (timer related)
 
Why SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScriptWhy SOLID matters - even for JavaScript
Why SOLID matters - even for JavaScript
 
Dmytro Zaitsev Viper: make your mvp cleaner
Dmytro Zaitsev Viper: make your mvp cleanerDmytro Zaitsev Viper: make your mvp cleaner
Dmytro Zaitsev Viper: make your mvp cleaner
 
FormsKit: reactive forms driven by state. UA Mobile 2017.
FormsKit: reactive forms driven by state. UA Mobile 2017.FormsKit: reactive forms driven by state. UA Mobile 2017.
FormsKit: reactive forms driven by state. UA Mobile 2017.
 
Angular Mini-Challenges
Angular Mini-ChallengesAngular Mini-Challenges
Angular Mini-Challenges
 
Muster in Webcontrollern
Muster in WebcontrollernMuster in Webcontrollern
Muster in Webcontrollern
 
Standford 2015 week9
Standford 2015 week9Standford 2015 week9
Standford 2015 week9
 
AngularJS.part1
AngularJS.part1AngularJS.part1
AngularJS.part1
 
運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScript運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScript
 
ハンズオン資料 電話を作ろう(v2.x用)
ハンズオン資料 電話を作ろう(v2.x用)ハンズオン資料 電話を作ろう(v2.x用)
ハンズオン資料 電話を作ろう(v2.x用)
 
Building Apps with Flutter - Hillel Coren, Invoice Ninja
Building Apps with Flutter - Hillel Coren, Invoice NinjaBuilding Apps with Flutter - Hillel Coren, Invoice Ninja
Building Apps with Flutter - Hillel Coren, Invoice Ninja
 
Testando API's de forma unitária mocando as dependências
Testando API's de forma unitária mocando as dependênciasTestando API's de forma unitária mocando as dependências
Testando API's de forma unitária mocando as dependências
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11
 

Andere mochten auch (13)

I os 16
I os 16I os 16
I os 16
 
I os 02
I os 02I os 02
I os 02
 
I os 04
I os 04I os 04
I os 04
 
I os 14
I os 14I os 14
I os 14
 
課程規畫
課程規畫課程規畫
課程規畫
 
I os 07
I os 07I os 07
I os 07
 
I os 15
I os 15I os 15
I os 15
 
I os 06
I os 06I os 06
I os 06
 
I os 09
I os 09I os 09
I os 09
 
I os 08
I os 08I os 08
I os 08
 
I os 05
I os 05I os 05
I os 05
 
I os 10
I os 10I os 10
I os 10
 
I os 13
I os 13I os 13
I os 13
 

Ähnlich wie I os 03

Anyone can play iPhone-Tuzhiwu
Anyone can play iPhone-TuzhiwuAnyone can play iPhone-Tuzhiwu
Anyone can play iPhone-TuzhiwuXiaochun Shen
 
Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder BehindJohn Wilker
 
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III) Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III) Mobivery
 
Quick Start to iOS Development
Quick Start to iOS DevelopmentQuick Start to iOS Development
Quick Start to iOS DevelopmentJussi Pohjolainen
 
Custom UIViewController Transitions
Custom UIViewController TransitionsCustom UIViewController Transitions
Custom UIViewController TransitionsJan Ilavsky
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragmentChiwon Song
 
Your Second iPhone App - Code Listings
Your Second iPhone App - Code ListingsYour Second iPhone App - Code Listings
Your Second iPhone App - Code ListingsVu Tran Lam
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4Calvin Cheng
 
Net conf BG xamarin lecture
Net conf BG xamarin lectureNet conf BG xamarin lecture
Net conf BG xamarin lectureTsvyatko Konov
 
Daniel Jalkut - dotSwift 2019
Daniel Jalkut - dotSwift 2019Daniel Jalkut - dotSwift 2019
Daniel Jalkut - dotSwift 2019DanielJalkut
 
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCsStandford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs彼得潘 Pan
 
Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View ControllersBob McCune
 
iOS for ERREST - alternative version
iOS for ERREST - alternative versioniOS for ERREST - alternative version
iOS for ERREST - alternative versionWO Community
 
Migrating Objective-C to Swift
Migrating Objective-C to SwiftMigrating Objective-C to Swift
Migrating Objective-C to SwiftElmar Kretzer
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalDroidcon Berlin
 
Swift ui userinput
Swift ui userinputSwift ui userinput
Swift ui userinputjoonjhokil
 

Ähnlich wie I os 03 (20)

Anyone can play iPhone-Tuzhiwu
Anyone can play iPhone-TuzhiwuAnyone can play iPhone-Tuzhiwu
Anyone can play iPhone-Tuzhiwu
 
Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder Behind
 
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III) Formacion en movilidad: Conceptos de desarrollo en iOS (III)
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
 
Quick Start to iOS Development
Quick Start to iOS DevelopmentQuick Start to iOS Development
Quick Start to iOS Development
 
Custom UIViewController Transitions
Custom UIViewController TransitionsCustom UIViewController Transitions
Custom UIViewController Transitions
 
201104 iphone navigation-based apps
201104 iphone navigation-based apps201104 iphone navigation-based apps
201104 iphone navigation-based apps
 
20180721 code defragment
20180721 code defragment20180721 code defragment
20180721 code defragment
 
Your Second iPhone App - Code Listings
Your Second iPhone App - Code ListingsYour Second iPhone App - Code Listings
Your Second iPhone App - Code Listings
 
iOS_Presentation
iOS_PresentationiOS_Presentation
iOS_Presentation
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4
 
Net conf BG xamarin lecture
Net conf BG xamarin lectureNet conf BG xamarin lecture
Net conf BG xamarin lecture
 
Daniel Jalkut - dotSwift 2019
Daniel Jalkut - dotSwift 2019Daniel Jalkut - dotSwift 2019
Daniel Jalkut - dotSwift 2019
 
IOS APPs Revision
IOS APPs RevisionIOS APPs Revision
IOS APPs Revision
 
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCsStandford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
Standford 2015 week4: 1.Protocols and Delegation, Gestures 2. Multiple MVCs
 
Creating Container View Controllers
Creating Container View ControllersCreating Container View Controllers
Creating Container View Controllers
 
iOS for ERREST - alternative version
iOS for ERREST - alternative versioniOS for ERREST - alternative version
iOS for ERREST - alternative version
 
Migrating Objective-C to Swift
Migrating Objective-C to SwiftMigrating Objective-C to Swift
Migrating Objective-C to Swift
 
From mvc to viper
From mvc to viperFrom mvc to viper
From mvc to viper
 
Thomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-finalThomas braun dependency-injection_with_robo_guice-presentation-final
Thomas braun dependency-injection_with_robo_guice-presentation-final
 
Swift ui userinput
Swift ui userinputSwift ui userinput
Swift ui userinput
 

Mehr von 信嘉 陳

Mehr von 信嘉 陳 (13)

Processing 06
Processing 06Processing 06
Processing 06
 
Processing 05
Processing 05Processing 05
Processing 05
 
Processing 04
Processing 04Processing 04
Processing 04
 
Processing 03
Processing 03Processing 03
Processing 03
 
Processing 02
Processing 02Processing 02
Processing 02
 
Processing 01
Processing 01Processing 01
Processing 01
 
Processing 09
Processing 09Processing 09
Processing 09
 
Processing 08
Processing 08Processing 08
Processing 08
 
Processing 07
Processing 07Processing 07
Processing 07
 
I os 01
I os 01I os 01
I os 01
 
Google 街景
Google 街景Google 街景
Google 街景
 
社群網站 Facebook
社群網站 Facebook社群網站 Facebook
社群網站 Facebook
 
網路搜尋
網路搜尋網路搜尋
網路搜尋
 

I os 03

  • 2. 課程⼤大綱 • ⽂文字的輸⼊入 (UITextField) • QV061:BMI 計算 • UISwitch, UISlider • QV005:UISwitch 及 UISlider 基本⽤用法 • QV013:利⽤用三個 UISlider 改變背景⾊色 • UIView • QV074:UIView (Container) • 以程式語法產⽣生各種 UIView 元件 • QV075:只是擺放 • QV076:程式語⾔言的⽅方式 • Samples:UICatalog
  • 5. ViewController.h @interface ViewController : UIViewController { IBOutlet UITextField *textHeight, *textWeight; IBOutlet UILabel *display; } - (IBAction) calcBMI:(id)sender; - (IBAction) doneEdit:(id)sender; @end
  • 7. ViewController.m - (IBAction) calcBMI:(id)sender { float h = [[textHeight text] floatValue]; float w = [textWeight.text floatValue]; float bmi = w / ((h/100)*(h/100)); display.text = [NSString stringWithFormat:@"%5.2f", bmi]; }
  • 8. 虛擬鍵盤不會隱藏的問題解決 要注意各個 UITextFiled 的 Did End On Exit 事件 要指定動作,以隱藏輸⼊入鍵盤 - (IBAction) doneEdit:(id)sender { [sender resignFirstResponder]; }
  • 9. 與鍵盤按鍵有關的⼩小問題 - (IBAction) calcBMI:(id)sender { float h = [[textHeight text] floatValue]; float w = [textWeight.text floatValue]; float bmi = w / ((h/100)*(h/100)); display.text = [NSString stringWithFormat:@"%5.2f", bmi]; [textHeight resignFirstResponder]; 隱藏輸⼊入鍵盤 [textWeight resignFirstResponder]; } - (IBAction) doneEdit:(id)sender { [sender resignFirstResponder]; } 設定鍵盤類型 - (void)viewDidLoad { [super viewDidLoad]; [textHeight setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; [textWeight setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; }
  • 11. UISlider 類別 • 主要屬性 • value • minimumValue • maximumValue • continuous
  • 12. UISwitch 類別 • 主要屬性 • on 開關的狀態 (BOOL值)
  • 13. 範例:QV005 UISlider 及 UISwitch 基本⽤用法
  • 14. Project QV005 UI 元件 Slider UI 元件 Switch
  • 15. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UILabel *messageSlider; IBOutlet UILabel *messageSwitch; } ViewController.xib -(IBAction)changeSlider:(id)sender; -(IBAction)changeSwitch:(id)sender; @end
  • 17. #import "ViewController.h" ViewController.m @implementation ViewController -(IBAction)changeSlider:(id)sender { UISlider *thisSlider = (UISlider *)sender; int number = (int)(thisSlider.value); NSString *msg = [[NSString alloc] initWithFormat:@"%d", number]; messageSlider.text = msg; } -(IBAction)changeSwitch:(id)sender { UISwitch *thisSwitch = (UISwitch *)sender; if(thisSwitch.on) { messageSwitch.text = @"ON"; } else { messageSwitch.text = @"OFF"; } } @end
  • 18. 範例:QV013 利⽤用三個 UISlider 改變背景⾊色
  • 19. Project QV013 三組 UISlider 更換背景顏⾊色 早期知名的⼿手電筒程式
  • 20. ViewController.h 注意此數值需與 程式互相配合 三個都執⾏行同⼀一段函式
  • 21. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UISlider *sliderR; IBOutlet UISlider *sliderG; IBOutlet UISlider *sliderB; } @property (retain, nonatomic) IBOutlet UISlider *sliderR; @property (retain, nonatomic) IBOutlet UISlider *sliderG; @property (retain, nonatomic) IBOutlet UISlider *sliderB; -(IBAction)sliderChange:(id)sender; @end
  • 22. ViewController.m 顏⾊色的值為 -(IBAction)sliderChange:(id)sender 0 到 1 之間的浮點數 { float valueR = [sliderR value]; float valueG = [sliderG value]; float valueB = [sliderB value]; UIColor *myColor = [UIColor colorWithRed:(valueR/255.0) green:(valueG/255.0) blue:(valueB/255.0) alpha: 1]; self.view.backgroundColor = myColor; }
  • 23. 範例:QV074 UIView 的包容 (container) 處理
  • 24. Project QV074 利⽤用 UIView 包容多個 UIView ⼦子物件 UISwitch 及 UISlider UIView 物件位置移動
  • 25. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UISwitch *mySwitch; IBOutlet UIView *myView; IBOutlet UISlider *mySlider1, *mySlider2; IBOutlet UILabel *myLabel1, *myLabel2; } - (IBAction) changeSwitch:(id)sender; - (IBAction) changeSlider:(id)sender; @end 將多個物件包在⼀一塊
  • 27. ViewController.m (1/2) 切換 UISwitch 以控制 UIView, 會整個顯⽰示或隱藏 - (IBAction) changeSwitch:(id)sender { UISwitch *thisSwitch = (UISwitch *)sender; if(thisSwitch.on) { 內部宣告明確類別的物 myView.hidden = NO; } 件,取得傳⼊入的指標 else { myView.hidden = YES; } } 取得 UISwitch 的輸⼊入 xxxxx.on (BOOL 型態)
  • 28. ViewController.m (2/2) - (IBAction) changeSlider:(id)sender { myLabel1.text = [NSString stringWithFormat:@"%3.1f", mySlider1.value]; myLabel2.text = [NSString stringWithFormat:@"%3.1f", mySlider2.value]; float x = 320 * mySlider1.value; float y = 460 * (1.0-mySlider2.value); // 因為不能改寫center.x及center.y。所以改⽅方法 mySwitch.center = CGPointMake(x, y); } 移動 UIView 的位置 取得 UISlider 的輸⼊入 xxxxx.value (float 型態)
  • 32. Project QV076 以程式置放 UIView 物件 UILabel, UIButton, UITextField, UISlider, UISwitch, UIStepper, UIImageView, UIWebView
  • 33. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UILabel *myLabel; } - (void) textChange:(id)sender; 會讓很多程式⽚片段使 - (void) doneEdit:(id)sender; - (void) buttonClick:(id)sender; ⽤用,故宣告在此 - (void) sliderChange:(id)sender; - (void) switchChange:(id)sender; - (void) stepperChange:(id)sender; @end
  • 34. ViewController.m (1/9) UILabel - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UILabel (注意,myLabel已定義在類別中) myLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 30)]; myLabel.textAlignment = UITextAlignmentCenter; myLabel.font = [UIFont fontWithName:@"Arial" size:20]; myLabel.backgroundColor = [UIColor clearColor]; myLabel.text = @"訊息⽂文字列"; [self.view addSubview:myLabel]; }
  • 35. ViewController.m (2/9) UIButton + UIAlert - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UIButton UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.frame = CGRectMake(30, 70, 72, 37); [myButton setTitle:@"Click me" forState:UIControlStateNormal]; [myButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:myButton]; UIAlertView 使⽤用 } - (void) buttonClick:(id)sender { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"現在狀態" message:@"按下了按鈕" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; }
  • 36. ViewController.m (3/9) UITextField (1/2) - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UITextField UITextField *myTextField = [[UITextField alloc] init]; myTextField.frame = CGRectMake(30, 120, 100, 31); // ⾼高(31) 為固定值 myTextField.borderStyle = UITextBorderStyleRoundedRect; myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; myTextField.text = @"預設內容"; myTextField.secureTextEntry = YES; [myTextField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged]; [myTextField addTarget:self action:@selector(doneEdit:) forControlEvents:UIControlEventEditingDidEndOnExit]; [self.view addSubview:myTextField]; } 兩個事件指定
  • 37. ViewController.m (4/9) UITextField (2/2) - (void) textChange:(id)sender { UITextField *thisTextField = (UITextField *)sender; if ([thisTextField.text length] == 0) { myLabel.text = @"沒有輸⼊入⽂文字"; } else { myLabel.text = [NSString stringWithFormat:@"⽂文字:%@", thisTextField.text]; } } - (void) doneEdit:(id)sender 取得輸⼊入的內容 { [sender resignFirstResponder]; } 隱藏虛擬鍵盤
  • 38. ViewController.m (5/9) UISlider - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UISlider CGRect frame1 = CGRectMake(30, 170, 79, 23); // ⾼高(23) 為固定值 UISlider *mySlider = [[UISlider alloc] initWithFrame:frame1]; mySlider.minimumValue = 0.0; mySlider.maximumValue = 100.0; mySlider.value = 50.0; [mySlider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:mySlider]; } - (void) sliderChange:(id)sender { UISlider *thisSlider = (UISlider *)sender; float value = thisSlider.value; 取得輸⼊入的內容 myLabel.text = [NSString stringWithFormat:@"滑桿值為:%5.2f", value]; }
  • 39. UISwitch ViewController.m (6/9) - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UISwitch CGRect frame2 = CGRectMake(30, 220, 79, 27); // 寬(79)⾼高(27)為固定值 UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:frame2]; mySwitch.on = NO; [mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:mySwitch]; } - (void) switchChange:(id)sender { UISwitch *thisSwitch = (UISwitch *)sender; if(thisSwitch.on) 取得:開或關 { myLabel.text = [NSString stringWithFormat:@"切換鍵為:開 (ON)"]; } else { myLabel.text = [NSString stringWithFormat:@"切換鍵:關 (不是ON)"]; } }
  • 40. UIStepper iOS 5 之後提供 ViewController.m (7/9) - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UIStepper CGRect frame3 = CGRectMake(185, 70, 94, 27); // 寬(94)⾼高(27)為固定值 UIStepper *myStepper = [[UIStepper alloc] initWithFrame:frame3]; myStepper.maximumValue = 10.0; myStepper.minimumValue = 0.0; myStepper.value = 5.5; [myStepper setStepValue:0.1]; // 每次改變的量 [myStepper setContinuous:YES]; // 可以按住不放來連續變更 [myStepper setWraps:YES]; // 設定是否循環(到最⼤大值時增加數值,從最⼩小值開始) [myStepper addTarget:self action:@selector(stepperChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:myStepper]; } - (void) stepperChange:(id)sender { UIStepper *thisStepper = (UIStepper *)sender; float value = thisStepper.value; 取得輸⼊入的內容 myLabel.text = [NSString stringWithFormat:@"增減鍵的值為:%4.1f", value]; }
  • 41. ViewController.m (8/9) UIImageView - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UIImage UIImage *myImage = [UIImage imageNamed:@"Cat.png"]; // 產⽣生 UIImageView UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; myImageView.frame = CGRectMake(185, 120, 128, 128); [self.view addSubview:myImageView]; } 需注意,圖像使⽤用包含 UIImageView (View Control) 及 UIImage (圖檔本⾝身)
  • 42. ViewController.m (9/9) UIWebView - (void)viewDidLoad { ****** 省略部份程式碼 ****** // 產⽣生 UIWebView UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 270, 270, 165)]; myWebView.scalesPageToFit = YES; [myWebView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.apple.com"]]]; [self.view addSubview:myWebView]; } 此處為簡略⽤用法
  • 43. ⾃自我練習 熟悉並區分程式⽤用法及 Interface Builder 的⽤用法 此範例提到之 UIView 類別 UILabel UIButton UITextField UISlider UISwitch UIStepper UIImageView UIWebView
  • 45.
  • 46.