2012年11月17日 星期六

Popover在iPad上的使用(一)

一個popover的UIViewController與主UIViewController之間的資料傳輸

實作如下

1.  開啓一個專案

2. 在Storyboard上加上一個新的UIViewController作為popover,並在兩邊加上一個TextFiled作為資料輸入,然後用button加segue連結成popover顯示,popover segue的ID設為popSeg

3. 加上新的檔案給popover的UIViewController使用


4. 在mainViewController.h加上Button及TextField的註冊
#import <UIKit/UIKit.h>
#import "popViewController.h"

@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *popButton;

@property (weak, nonatomic) IBOutlet UITextField *mainTextField;


@end

5. 在mainViewController.m加上控制傳輸的程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize popButton, mainTextField;

- (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)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
   
    if([segue.identifier isEqualToString:@"popSeg"]){
        UIStoryboardPopoverSegue *popoverSegue =(UIStoryboardPopoverSegue *)segue;
        popoverSegue.popoverController.delegate = self; //popover 資料傳輸的重點設定
       
        popViewController *editorVC = segue.destinationViewController;
        editorVC.dataTextField.text = self.mainTextField.text;
    }
   
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    self.mainTextField.text = ((popViewController *) popoverController.contentViewController).dataTextField.text;

}


@end


6. 在popViewController.h加上TextField的註冊,popViewController.m不用加任何程式碼
#import <UIKit/UIKit.h>
#import "mainViewController.h"
@interface popViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *dataTextField;

@end


7. 執行結果




2012年11月16日 星期五

一步一步学习 iOS 5 编程(第二版)文件下載

網路上看到的下載點,文件寫的還不錯,有需要的人可自行下載。

https://skydrive.live.com/?cid=639d8e6a2c8e9fe8&id=639D8E6A2C8E9FE8!109#cid=639D8E6A2C8E9FE8&id=639D8E6A2C8E9FE8!108

UIAlertView的基本使用(五)

使用alertView來輸入賬號與密碼

實作很簡當,從前一篇的實作例只需增加一小部份的程式碼

1. 在Storyboard中加入兩個Label及一個Button
 
2.在mainViewController.h註冊新的元件
#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController

@property (nonatomic)  NSString  *text;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@property (nonatomic) UIStepper *stepper2;
@property (weak, nonatomic) IBOutlet UILabel *stepper2Label;

@property (nonatomic) UILabel *insideLabel;

@property (weak, nonatomic) IBOutlet UILabel *loginLabel;
@property (weak, nonatomic) IBOutlet UILabel *passwdLabel;

@end


3. 在mainViewController.m加上所需的程式碼
- (IBAction)loginInput:(id)sender {
   
    alertNumber = 2;
   
    [self alertViewAction3];

}



- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
           
            if (alertNumber == 0)
               text = @"Cancel Button Pressed";
           
                       
            break;
        case 1:
            text =  @"Button 1 Pressed";
            break;
        case 2:
            text = @"Button 2 Pressed";
            break;
        case 3:
            text = @"Button 3 Pressed";
            break;
        default:
            break;
    }
   
    if (alertNumber == 0)
       textLabel.text = text;
    else if (alertNumber == 1)
        stepper2Label.text = text;
    else {
        if (alertView.tag == 100){
            UITextField *loginField = [alertView textFieldAtIndex:0];
            loginLabel.text  = [NSString stringWithFormat:@"login: %@", loginField.text];
           
            UITextField *passwdField= [alertView textFieldAtIndex:1];
            passwdLabel.text = [NSString stringWithFormat:@"passwd: %@", passwdField.text];
           
        }
    }

   
}


- (void) willPresentAlertView:(UIAlertView *)alertView{
   
    if (alertNumber ==0)
       alertView.frame = CGRectMake(360.0f, 350.0f, 300.0f, 350.0f);
    else if (alertNumber ==1)
       alertView.frame = CGRectMake(250, 650.0f, 280.0f, 180.0f);
    else
       alertView.frame = CGRectMake(360.0f, 350.0f, 300.0f, 350.0f);

}


- (void) alertViewAction3
{
   
    UIAlertView *alert3 = [[UIAlertView alloc] init];
   
    alert3.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
   
    alert3.delegate = self;
   
    [alert3 addButtonWithTitle:@"Add"];
   
    alert3.tag = 100;
   
    [alert3 show];
   
}


4. 執行結果



2012年11月15日 星期四

UIAlertView的基本使用(四)

在顯示alertView時,採用動畫的模式。不過實作後,發現其效果並不美觀,會呈現次元件分離的狀態。

實作很簡當,從前一篇的實作例只需修改mainViewController.m的部分程式碼

- (IBAction)showAlertView:(id)sender {  //移動模式
  
    alertNumber = 0;
   
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:5];
   

    [self alertViewAction1];
   
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用某个方法
    //[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    [UIView commitAnimations];


   
   
}

- (IBAction)dataInput:(id)sender { // 翻頁模式
    alertNumber = 1;
   
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    [UIView setAnimationDuration:1];
//   
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

   
    [self alertViewAction2];
   
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用某个方法
    //[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    [UIView commitAnimations];

   
}

因為是動畫,故不截圖了。

2012年11月14日 星期三

UIAlertView的基本使用(三)

參考前篇的文件,改良原來的實作,增加了一個stepper在第二個alertView上,並讓兩個alertView有不同的大小與位置,形態也不相同。

修改原先的實作,來表現特殊效果如下

1. 加入一個Button及一個label到storyboard上

2. 在mainViewControlller.h上註冊新的元件
#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController

@property (nonatomic)  NSString  *text;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@property (nonatomic) UIStepper *stepper2;
@property (weak, nonatomic) IBOutlet UILabel *stepper2Label;

@property (nonatomic) UILabel *insideLabel;


@end

3. 在mainViewController.m加上新的程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize text;

@synthesize textLabel;

@synthesize stepper2;
@synthesize stepper2Label;
@synthesize insideLabel;


int value=0;
int alertNumber = 0;

- (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)showAlertView:(id)sender {
  
    alertNumber = 0;
   
    [self alertViewAction1];

   
}

- (IBAction)dataInput:(id)sender {

    alertNumber = 1;
   
    [self alertViewAction2];
   
}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
           
            if (alertNumber == 0)
               text = @"Cancel Button Pressed";
           
            break;
        case 1:
            text =  @"Button 1 Pressed";
            break;
        case 2:
            text = @"Button 2 Pressed";
            break;
        case 3:
            text = @"Button 3 Pressed";
            break;
        default:
            break;
    }
   
    if (alertNumber == 0)
       textLabel.text = text;
    else
        stepper2Label.text = text;
   
}

- (void) willPresentAlertView:(UIAlertView *)alertView{
    

    // alertView的大小位置,只能在此處設定
    if (alertNumber ==0)
       alertView.frame = CGRectMake(360.0f, 350.0f, 300.0f, 350.0f);
    else
       alertView.frame = CGRectMake(250, 650.0f, 280.0f, 180.0f);

}



- (void) stepperValueIschanged
{
    text = [NSString stringWithFormat:@"Stepper2 Value = %2.2f", stepper2.value];
   
    stepper2Label.text = text;
   
    insideLabel.text = text;
   
}


- (void) alertViewAction1
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
   
    [alert1 show];
   
}


- (void) alertViewAction2
{
   
    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Stepper Input"
                                                     message:@"\n\n\n" // IMPORTANT 製造空白區
                                                    delegate:self  // call
                                           cancelButtonTitle:@"取消"
                                           otherButtonTitles:@"選擇", nil];
   
 
    insideLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 250, 20)];
   
    //資源分配與位置設定
    stepper2 = [[UIStepper alloc] init];
   
    // size value are ignore,注意大小不能設定,因此設為任何值都可以,此處為0
    [stepper2 setFrame:CGRectMake(80,60 , 0, 0)];
   
    //設定stepper的範圍與起始值
    [stepper2 setMaximumValue:10.0];
    [stepper2 setMinimumValue:0.0];
    [stepper2 setValue:5.5];
   
    //設定stepper每次增減的值
    [stepper2 setStepValue:0.1];
   
    //設定stepper可以按住不放來連續更改數值
    [stepper2 setContinuous:YES];
   
    //設定stepper是否循環(到最大值時再增加數值最從最小值開始)
    [stepper2 setWraps:YES];
   
    //將stepper加入UIControlEventValueChanged的觸發事件中並設定觸發時所處理的函式
    [stepper2 addTarget:self action:@selector(stepperValueIschanged) forControlEvents:UIControlEventValueChanged];
   
    [alert2 addSubview:stepper2];
   
    [alert2 addSubview:insideLabel];
   
    // 向上移動110點,平移設定
    [alert2 setTransform:CGAffineTransformMakeTranslation(0.0, 110.0)];
   
    //alertNumber = 1;
   
    [alert2 show];
   

}


@end

4. 執行結果



2012年11月13日 星期二

UIAlertView的基本使用(二)

如果要讓UIAlertView發揮更多的功能,就要注意UIAlertViewDelegate的使用

整理相關資料如下

基本的UIAlertViewDelegate參考網頁
http://osbo.com/xcode/uialertviewdelegate/

官方文件網頁
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html


參考做法
1. ViewController.h // 紅色部分需要加上,但實作時似乎又可以不加
此處可參考網頁說明
http://stackoverflow.com/questions/10639886/why-do-i-not-need-to-declare-uialertviewdelegate-in-the-header

@interface ViewController : UIViewController <UIAlertViewDelegate>

@end


2. ViewController.m 


#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[[UIApplication sharedApplication] setStatusBarHidden:YES];

//四種基本的UIAlertView框架模式,要注意delegate要設成self,才能用到後面預設的Function call
//* mode1
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other1", @"Other2", nil];
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/


UIAlertViewStyleDefault


/* mode2
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
alertView.delegate = self;
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/


UIAlertViewStyleLoginAndPasswordInput
 


/* mode3
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
alertView.delegate = self;
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/


UIAlertViewStylePlainTextInput




/* mode4
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
alertView.delegate = self;
[alertView addButtonWithTitle:@"Add"];
[alertView show];
//*/

UIAlertViewStyleSecureTextInput

}


- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSLog(@"alertViewShouldEnableFirstOtherButton");

return YES;
}
- (void)willPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"willPresentAlertView");
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"didPresentAlertView");
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"clickedButtonAtIndex");

switch (buttonIndex)
{
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;
case 3:
NSLog(@"3");
break;
default:
break;
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"willDismissWithButtonIndex");

switch (buttonIndex)
{
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;
case 3:
NSLog(@"3");
break;
default:
break;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex");

switch (buttonIndex)
{
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;
case 3:
NSLog(@"3");
break;
default:
break;
}
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"alertViewCancel - system canceled alert view");
}

@end

2012年11月12日 星期一

UIAlertView的基本使用(一)

一個警告訊號的元件

主要使用方法
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];  

最簡化的使用參考
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"恭喜過關了" message:@"\n" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
 

以下是實驗例

1. 開啓一個專案

2. 加入一個Button及一個Label到storyboard
 3. 在mainViewController.h註冊相關元件及一個暫存的NSString
#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController

@property (nonatomic)  NSString  *text;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;


@end

4. 在mainViewController.hm加入AlterView的程式碼,因為會額外增加三個Button到AlertView上,

注意:UIAlertView 的 CancelButton 的 buttonIndex 是 0,其他按鈕的 buttonIndex 則順序增加。

#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize text;

@synthesize textLabel;


- (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)showAlertView:(id)sender {
   
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil]; //對於不要用的部分可以砍掉或設成nil

    [alert show];
   
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            text = @"Cancel Button Pressed";
            break;
        case 1:
            text =  @"Button 1 Pressed";
            break;
        case 2:
            text = @"Button 2 Pressed";
            break;
        case 3:
            text = @"Button 3 Pressed";
            break;
        default:
            break;
    }
   
    textLabel.text = text;
   
}


@end

5. 執行結果





2012年11月11日 星期日

UIStepper的使用

可參考http://furnacedigital.blogspot.tw/2011/12/uistepper.html


以下是實驗結果,試了一個靜態及一個動態的實作。

1. 開啓專案

2. 因為要實驗貼圖的效果,因此加入了Mac的圖庫

3. 在storyboard加上一個stepper及兩個Label

4. 在mainViewController.h註冊所需元件
#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIStepper *stepper1;
@property (weak, nonatomic) IBOutlet UILabel *showLabel;

@property (nonatomic) UIStepper *stepper2;

@property (weak, nonatomic) IBOutlet UILabel *showLabel2;

@end




5. 在mainViewController.m加上程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize showLabel, stepper1;

@synthesize stepper2;
@synthesize showLabel2;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
   
    //資源分配與位置設定
    stepper2 = [[UIStepper alloc] init];
   
    // size value are ignore,注意大小不能設定,因此設為任何值都可以,此處為0
    [stepper2 setFrame:CGRectMake(300, 500, 0, 0)];
   
  //  [stepper2 sizeToFit];
    
  //  [stepper2 setCenter:self.view.center];
   
    //設定stepper的範圍與起始值
    [stepper2 setMaximumValue:10.0];
    [stepper2 setMinimumValue:0.0];
    [stepper2 setValue:5.5];
   
    //設定stepper每次增減的值
    [stepper2 setStepValue:0.1];
   
    //設定stepper可以按住不放來連續更改數值
    [stepper2 setContinuous:YES];
   
    //設定stepper是否循環(到最大值時再增加數值最從最小值開始)
    [stepper2 setWraps:YES];
   
    //將stepper加入UIControlEventValueChanged的觸發事件中並設定觸發時所處理的函式
    [stepper2 addTarget:self action:@selector(stepperValueIschanged) forControlEvents:UIControlEventValueChanged];
   
    NSString *imageName1 = [NSString stringWithFormat:@"Doggie.gif"];
   
    NSString *imageName2 = [NSString stringWithFormat:@"Pointy.gif"];
   
    NSString *imageName3 = [NSString stringWithFormat:@"RedDog.gif"];
   
   
    UIImage *minImage = [[UIImage imageNamed:imageName1] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 0)];
   
    UIImage *maxImage = [[UIImage imageNamed:imageName2] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 0)];
   
    UIImage *divImage = [[UIImage imageNamed:imageName3] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 0)];

    [stepper2 setDecrementImage:minImage forState:UIControlStateNormal];
    [stepper2 setIncrementImage:maxImage forState:UIControlStateNormal];
   
    [stepper2 setDividerImage:divImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
   
    //將stepper加至畫面中
    [self.view addSubview:stepper2];

   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)stepperValueChange:(id)sender {
   
    showLabel.text = [NSString stringWithFormat:@"Stepper1 Value = %3.2f", stepper1.value];
   
}

- (void) stepperValueIschanged
{
    showLabel2.text = [NSString stringWithFormat:@"Stepper2 Value = %2.2f", stepper2.value];
}


@end


6. 實驗結果,因為大小不能設定,所以第二個Stepper的圖示有點太大。如果要使用圖示效果,還是使用兩個Button來組合,還比較好看。