2012年10月5日 星期五

Switch的基本使用(ㄧ)


一個最基本的Switch的設定與使用
Xcode版本4.5 & IOS6


1. 開啓專案


2. 設定StoryBoard,引進一個UISwitch及一個Label,並設定大小與顏色。

3. 註冊元件到switchViewController.h
#import <UIKit/UIKit.h>

@interface switchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

@end

4. 加入控制碼到switchViewController.m
#import "switchViewController.h"

@interface switchViewController ()

@end

@implementation switchViewController

@synthesize showLabel;
@synthesize mySwitch;

- (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)switchChange:(id)sender {
   
    if (mySwitch.on == 1){
        showLabel.text = @"開啟";
    }
    else {
        showLabel.text = @"關閉";
    }

}
@end

5. 執行結果


2012年10月4日 星期四

Slider的基本使用(ㄧ)

一個最基本的動態Slider的設定與使用,這是配合在iPad上Popover的設計用法。主要是使用一個Timer來當中斷,並檢查公共變數的值,來即時顯示變化。

Xcode版本4.5 & IOS6


1. 開一個iPad的專案



2. 設定StoryBoard,並加入一個Tool Bar及一個Label

3. 加入一個UIViewController,並將BarItem拉一個Segue到這個UIView

4.新增一個class 檔,對應到步驟三的新UIView

5. 對UIView上的Class指定成此到新增的Class檔

6. 對mainAppDelegate.h設定公共變數
#import <UIKit/UIKit.h>

@interface mainAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, retain)   NSMutableArray *globalData;

@end



7. 對mainAppDelegate.m設定
#import "mainAppDelegate.h"

@implementation mainAppDelegate

@synthesize globalData;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}
...

8. 對主程式的mainViewController.h加入所需的變數
#import <UIKit/UIKit.h>
#import "mainAppDelegate.h"

@interface mainViewController : UIViewController

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

@property (nonatomic, retain) mainAppDelegate *appDelegate;

@property (nonatomic) NSTimer  *timer;

@property (nonatomic) NSNumber  *sliderValue;

- (void) showText;

@end

9. 對主程式的mainViewController.m加入所需的程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

#define TimeUnit 0.5 // 0.5sec

@implementation mainViewController

@synthesize sliderValue;
@synthesize timer;
@synthesize appDelegate;
@synthesize valueLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    appDelegate = (mainAppDelegate *) [[UIApplication sharedApplication] delegate];
   
    appDelegate.globalData = [[NSMutableArray alloc] init];
   
    NSNumber *defaultValue = [NSNumber numberWithDouble:0.3];
   
    [appDelegate.globalData addObject:defaultValue];  //0

   
    [self  initTimer];

}

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

- (void) initTimer
{
   
    // nowTime = lastTime;
    if (timer == nil)
    {
        timer = [NSTimer scheduledTimerWithTimeInterval:TimeUnit target:self selector:@selector(showText) userInfo:nil repeats:YES];
    }
}

- (void) showText
{
    sliderValue = [appDelegate.globalData objectAtIndex:0];

    valueLabel.text= [NSString stringWithFormat:@"Value = %2.3f",[sliderValue floatValue]] ;
   
}


@end

10.對Slider所在的UIView作設定,先針對SliderViewController.h
#import <UIKit/UIKit.h>
#import "mainViewController.h"
#import "mainAppDelegate.h"

@interface SliderViewController : UIViewController

@property (nonatomic, retain) mainAppDelegate *appDelegate;

@property (nonatomic) UISlider  *mySlider;

@property (nonatomic) float myValue;

-(void) controlValueChanged;

@end

11.最後對SliderViewController. m加入控制碼
#import "SliderViewController.h"

@interface SliderViewController ()

@end

@implementation SliderViewController

@synthesize appDelegate;
@synthesize mySlider;
@synthesize myValue;

float  lastvalue=0.5;

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


//特別說明,因為Popover再一次指定後,會重複執行此處。
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    appDelegate = (mainAppDelegate *)[[UIApplication sharedApplication] delegate];
   
    mySlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    [mySlider addTarget:self action:@selector(controlValueChanged) forControlEvents:UIControlEventValueChanged];
   
    mySlider.minimumValue = 0;
    mySlider.maximumValue = 3;
    mySlider.continuous = YES;
   
    // voiceSlider.value = 0.5;  // 此種設定會讓Slider不能動
    [mySlider setValue:lastvalue]; //只可以用此法,
   
    [self.view addSubview:mySlider];
   
}

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

- (void) controlValueChanged
{
    myValue = mySlider.value;
   
    NSNumber *number = [NSNumber numberWithFloat:myValue];
   
    [appDelegate.globalData replaceObjectAtIndex:0 withObject:number];
   
    lastvalue = myValue;
}


@end


12. 執行結果,Label會即時顯示Slider上的變化。



2012年10月3日 星期三

UINavigationBar的基本使用(一)

一個最基本的動態NavigationBar的設定,並使用兩個Button來做移除及設定動態NavigationBar的顯示。

Xcode版本4.5 & IOS6

1. 開啓一個專案

2. 設定StoryBoard並加入一個Label及兩個Button

3. 設定dynamicNavBarViewController.h

#import <UIKit/UIKit.h>

@interface dynamicNavBarViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UIButton *showButton;
@property (weak, nonatomic) IBOutlet UIButton *delButton;

@property (nonatomic) UINavigationBar *myNavBar;

@property (nonatomic) UINavigationItem *NavTitle;

@property (nonatomic) UIBarButtonItem *myBarItem11;
@property (nonatomic) UIBarButtonItem *myBarItem12;
@property (nonatomic) UIBarButtonItem *myBarItem13;

-(void)myNavAction1;
-(void)myNavAction2;
-(void)myNavAction3;


@end

4.  將程式碼加入到設定dynamicNavBarViewController. m
#import "dynamicNavBarViewController.h"

@interface dynamicNavBarViewController ()

@end

@implementation dynamicNavBarViewController

@synthesize myNavBar;

@synthesize myBarItem11, myBarItem12, myBarItem13;

@synthesize NavTitle;

@synthesize showLabel;


- (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)showNavBar:(id)sender {
   
    [self setupMyNavBar];
   
    [self.view  addSubview:myNavBar];
}
- (IBAction)delNavBar:(id)sender {
   
    [self removeAllSubView];
}

-(void)setupMyNavBar
{
    myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 44.0)];
   
    NavTitle = [[UINavigationItem alloc] initWithTitle:@"NavBar 測試"];
   
    [myNavBar  pushNavigationItem:NavTitle animated:YES];
   
    myBarItem11 = [[UIBarButtonItem alloc]
                   initWithTitle:@"左按钮"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(myNavAction1)];
   
    NavTitle.leftBarButtonItem = myBarItem11;
   
    myBarItem12 = [[UIBarButtonItem alloc]
                   initWithTitle:@"右按钮"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(myNavAction2)];
   
    myBarItem13 = [[UIBarButtonItem alloc]
                   initWithTitle:@"右二按钮"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(myNavAction3)];   
  
    NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem12,myBarItem13,nil];   
   
    NavTitle.rightBarButtonItems = toolbarItems;
   
}

- (void) removeAllSubView
{
    for(UIView *subview in [self.view subviews]) {
       
        if([subview isKindOfClass:[UINavigationBar class]]) {
            [subview removeFromSuperview];
        }
       
    }
}


-(void)myNavAction1
{
    NSString *text = @"測試左鍵";
   
    showLabel.text = text;
}

-(void)myNavAction2
{
    NSString *text = @"測試右鍵";
   
    showLabel.text = text;
}

-(void)myNavAction3
{
    NSString *text = @"測試右二鍵";
   
    showLabel.text = text;
}



@end

5. 執行結果





2012年10月2日 星期二

UIToolBar的基本使用(三)

一個最基本的動態ToolBar的設定,並使用兩個Button來做移除及設定動態ToolBar的顯示
Xcode版本4.5 & IOS6

1. 開啓一個新的專案



2. 設定StoryBoard並加入一個Label及兩個Button

3. 設定dynamicToolBarViewController.h

@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UIButton *showButton;
@property (weak, nonatomic) IBOutlet UIButton *delButton;

@property (nonatomic) UIToolbar *myToolBar;

@property (nonatomic) UIBarButtonItem *myBarItem1;
@property (nonatomic) UIBarButtonItem *myBarItem2;
@property (nonatomic) UIBarButtonItem *myBarItem3;
@property (nonatomic) UIBarButtonItem *myBarItem4;
@property (nonatomic) UIBarButtonItem *myBarItem5;

-(void)showText1 ;
-(void)showText2 ;
-(void)showText3 ;

4. 將控制碼加入到設定dynamicToolBarViewController. m

#import "dynamicToolBarViewController.h"

@interface dynamicToolBarViewController ()

@end

@implementation dynamicToolBarViewController

@synthesize showLabel;

@synthesize myToolBar;

@synthesize myBarItem1, myBarItem2, myBarItem3, myBarItem4, myBarItem5;


- (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)showToolBar:(id)sender {

    [self setupMyToolBar];
   
    [self.view  addSubview:myToolBar];

}
- (IBAction)delToolBar:(id)sender {
   
    [self removeAllSubView];
}

-(void)setupMyToolBar
{
    myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 704, 1024, 44.0)];
   
   
    //myBarItem1= [[UIBarButtonItem alloc] initWithCustomView:label];
    myBarItem1= [[UIBarButtonItem alloc]initWithTitle:@"Test1" style:UIBarButtonItemStylePlain target:self action:@selector(showText1)];
   
    myBarItem2= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
   
   
    myBarItem3= [[UIBarButtonItem alloc]initWithTitle:@"Test2" style:UIBarButtonItemStyleBordered target:self action:@selector(showText2)];
   
    myBarItem4= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
   
    myBarItem5= [[UIBarButtonItem alloc]initWithTitle:@"Test3" style:UIBarButtonItemStyleDone target:self action:@selector(showText3)];
   
    myBarItem2.width = 420;
   
    NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem1,myBarItem2,myBarItem3,myBarItem4,myBarItem5,nil];
    myToolBar.items = toolbarItems;
   
}

- (void) removeAllSubView
{
    for(UIView *subview in [self.view subviews]) {
   
        if([subview isKindOfClass:[UIToolbar class]]) {
            [subview removeFromSuperview];
        }
       
    }
}


-(void)showText1
{
   
    NSString *text = @"測試一";
   
    showLabel.text = text;
   
}

-(void)showText2
{
   
    NSString *text = @"測試二";
   
    showLabel.text = text;
   
}

-(void)showText3
{
   
    NSString *text = @"測試三";
   
    showLabel.text = text;
   
}

@end


5. 顯示結果如圖





2012年10月1日 星期一

如何申請iOS開發者帳號及相關資訊

這可以參考網址
http://blog.ichih.com/2011/09/ios.html

裡面的Video說明的非常詳細

另外也可參考http://www.minwt.com/ios/4353.html
裡面的步驟也非常詳細


全程用英文輸入,
名字用信用卡上的英文,First name填名(BB CC),Last name填姓(AA),中間名不用填
地址用中華郵政的英文轉換 ,輸入中文就會轉出英文了,再貼上去就可以了。



以下是轉帖內容及問題處理


假如您希望把App放到您的iPhone手機上做測試

或是把開發好的App放到App Store上架

那麼申請iOS開發者帳號是不可少的


在申請完畢之後, 您將擁有以下兩個網站的權限:


iOS Provisioning Portal

以及

iTunes Connect

您將可以透過iOS Provisioning Portal進行憑證申請以及實機偵錯的動作

以及透過iTunes Connect進行App Store上架的動作

不過要申請此帳號也是有代價的:年費是3200台幣喔!

另外, 在上述的流程中, 我們建議您全程使用英文輸入

因為根據申請的經驗, 如果輸入中文有可能儲存後會變成亂碼

另外根據其他網友的經驗, 比較容易發生問題的部份在於

1. 輸入地址的時候

2. 輸入信用卡的姓跟名顛倒

請大家多留意, 萬一申請完還是出現以下的錯誤訊息:

We are unable to active your iOS Developer Program membership....

請直接撥打Apple的0800-020-021的客服電話, 對方會問你的訂單號碼

你唸給他聽之後, 他就會幫你處理, 通常一天之內就會幫你開通囉!
我有遇到,被轉到下面的服務中心,立刻就開通了。


其他IOS開發上的問題可以打 0800-022-237會連到大陸的服務中心,有服務小姐會跟你解釋


使用方式可參考http://blog.ichih.com/2012/02/appios-developer-program.html

以下是轉帖內容

首先iOS Developer Program共分為三種:

Developer Program, Enterprise Program, 以及University Program


其中University Program是免費的, 但只提供給學生,

而且只有以下圖表的第二跟第三項的權限,

等於說只能在你自己的實機上測試,

完全無法使用任何發佈的管道來發佈你的App,

(因為功能太少, 所以我們先把它從比較表拿掉)


另外兩種分別是Developer Program 以及Enterprise Program

這兩種帳號的權限比較圖表, 請參考以下:

(原圖表取自Apple官網:https://developer.apple.com/programs/start/ios/



這兩個帳號, 除了費用的部份不同:

Developer Program的年費是99美金, 而Enterprise Program的年費是299美金之外


另外最大的不同在於發佈模式的不同

Developer Program的發佈模式有Ad Hoc, App Store, Custom B2B App

Enterprise Program的發佈模式則有Ad Hoc, In-house

接下來我們來解釋一下這些發佈模式:

1. Ad Hoc Distrubution

使用這個模式, 您可以將您開發好的App透過E-mail發送,

或是放上網站讓使用者可以下載並安裝你的App,

這個模式可容許你分享你的App到最多100部iOS裝置上

但是透過這種方式發送的App, 在憑證到期後, 就會無法執行

2. App Store Distrubution

這是大家最熟知的方式,

您可以透過此模式, 把開發好的App放到App Store上供人下載, 以賺取利潤

當然您也可以免費供人下載, 然後靠廣告, 或是內建In-app purchase來賺錢

3. Custom B2B App Distrubution:

這是比較新的一個發佈方式, (應該是2011年才推出的...)

主要是讓你可以提供客製化的B2B Apps給那些已經擁有Volume Purchase Program account的企業客戶,

客製化的B2B App提供了獨特的解決方案, 以解決特定的商業需求

4. In-house Distrubution

這個模式讓您可以把開發好的iOS App分享給所有企業內部員工,

並且做到把Apps安全地存放, 無線發佈或更新,

讓他們在任何時間任何地點都可以更新到最新版本


PS: 上述1~3的原文可以參考這邊:
https://developer.apple.com/programs/ios/distribute.html


總結來說:

Developer Program因為有App Store, Custom B2B App等發佈方式

因此適合販售軟體給個人或企業的軟體開發商


Enterprise Program因為有In-house發佈方式

因此較適合想要開發"僅企業內部使用"的Apps的公司行號