2011年5月16日 星期一

UIDevice

UIDevice類別是題供iPhone/iPod touch有關的裝置資訊
此類別唯一實體可利用類別方法currentDevice來取得,  下列是可利用這個實體取得的資訊

1. 獨一無二的識別資訊(unique identifier)

@property (nonatomic, readonly, retain) NSString *uniqueIdentifier;


2. 作業系統OS

@property (nonatomic, readonly, retain) NSString *systemName;


3. OS Version

@property (nonatomic, readonly, retain) NSString *systemVersion;


4. model (iPhone or iPod touch or iPad etc)

@property (nonatomic, readonly, retain) NSString *model;


5. 裝置定向   (可利用orientation屬性取得)

@property (nonatomic, readonly) UIDeviceOrientation orientation;
可能的內容值如下
-UIDeviceOrientationUnknown
-UIDeviceOrientationPortrait
-UIDeviceOrientationPortraitUpsideDown
-UIDeviceOrientationLandscapeLeft
-UIDeviceOrientationLandscapeRight
-UIDeviceOrientationFaceUp(螢幕朝上)
-UIDeviceOrientationFaceDown(螢幕朝下)


6. 電池狀態 (透過batteryLevel取得目前電池電量等級)

@property (nonatomic, readonly) float batteryLevel;

取得的數值是介於0.0~1.0(完全充飽), 假設電池電量無法背叛斷出來, 此值會等於1.0


6-1 利用batteryState取得電池狀態

@property (nonatomic, readonly) UIDeviceBatteryState batteryState;

回傳值為下列之一
-UIDeviceBatteryStateUnknown (電池狀態無法判斷, 使用simulator會得到此結果)
-UIDeviceBatteryStateUnplugged (電池因為未接上電源而處於耗電狀態)
-UIDeviceBatteryStateCharging
-UIDeviceBatteryStateFull


6-2 電池狀態等級通知
任何物件都可以被當成觀察者來加入底下兩個通知
-UIDeviceBatteryLevelDidChangeNotification
  此通知會在電池電量等級有發生改變時被發送出來 , 發送頻率最多是每分鐘一次


-UIDeviceBatteryStateDidChangeNotification
  電池狀態有發生改變時被發送出來, 例如: 裝置正被連接到電源上


為了接收這些通知資訊, 須將屬性batteryMonitoringEnabled設為YES





2011年5月15日 星期日

Customization Navigation View

basic property of navigation

//取得Navigation實體
@property (nonatomic, readonly, retain) UINavigationItem *navigationItem

2011年5月4日 星期三

About afconvert

iPhone OS支援的音樂格式 (部份)
CAF:apple的core audio音樂格式(.caf)
ALAC :apple不失真的音樂格式(.m4a)
AIFF:apple的音樂格式(.aif, .aiff)
AAC:itunes store音樂傳送時使用(.m4a)
MP3:(.mp3)

2011年5月2日 星期一

About AVAudioPlayer

There are many different ways to get an iPhone app to generate and record audio, but AVFoundation is the best compromise between simplicity and functionality

AVFoundation.framework : AVAudioPlayer, AVAudioRecorder

2011年4月27日 星期三

view switcher(三)-SwitchViewController








//

//  SwitchViewController.h

#import <UIKit/UIKit.h>

@class BlueViewController;
@class YellowViewController;

@interface SwitchViewController : UIViewController {
BlueViewController *_blueViewController;
YellowViewController *_yellowViewController;

}

@property (nonatomic, retain) BlueViewController *_blueViewController;
@property (nonatomic, retain) YellowViewController *_yellowViewController;

-(IBAction)switchViews:(id)sender;

@end

修改MainWindow.xib
按兩下MainWindow.xib開啓IB, 必須再建立用來表示root controller的實例; 因為IB程式庫沒有SwitchViewController, 所以必須加入一個view controller,再將其類別改為SwitchViewController
(圖一)將UIViewController拉到nib時所產生的view



接下來建立root controller的view, root controller的view將包含螢幕下方的一個工具列
之前我們將UIViewController拉到nib主視窗時, 產生了一個新視窗(如第一張圖), 要在此視窗為root controller(SwitchViewController)建立view

SwitchViewController主要任務是切換BlueView與YellowView, 所以必須有可以讓使用者改變檢視的方法, 所以在工具列增加一按鈕讓使用者切換檢視

從Library拖曳View到(圖一), Library拖曳Toolbar
更改button name 為 Switch Views

接著將動作與button連接
選擇MainWindow.xib的Switch View Controller, 選擇Inspector的Connections Inspector, 將switchViews方法連結至Toolbar的button (如下圖)

之前在SwitchViewControllerAppDelegate.h建立IBOutlet (_switchViewController), 因此應用程式可以連接到SwitchViewController的實例, 並在主要的應用程式視窗中加入sub view,..  所以必須在nib中將SwitchViewController實例連接到此IBOutlet
選擇Switch View Controller App Delegate, 選擇Inspector的Connections Inspector,將_switchViewController連接到SwitchViewController的實例, 如下圖

儲存nib, 開始編輯SwitchViewController.m

//  SwitchViewController.m
//  SwitchViewController

//

#import "SwitchViewController.h"

#import "BlueViewController.h"
#import "YellowViewController.h"

@implementation SwitchViewController

@synthesize _blueViewController;
@synthesize _yellowViewController;

-(IBAction)switchViews:(id)sender
{
//先檢查_yellowViewController的view之上層view是否為nil
if (self._yellowViewController.view.superview == nil) 
{
//檢察_yellowViewController是否被建立, 若尚未被建立, 則下列if成立 
if (self._yellowViewController == nil) 
{
YellowViewController *yellowController = [[YellowViewController alloc]
  initWithNibName:@"YellowView" bundle:nil];
self._yellowViewController =yellowController;
[yellowController release];
}
[_blueViewController.view removeFromSuperview];
[self.view insertSubview:_yellowViewController.view atIndex:0];
}
else {
if (self._blueViewController == nil) 
{
BlueViewController *blueController = [[BlueViewController alloc]
  initWithNibName:@"BlueView" bundle:nil];
self._blueViewController =blueController;
[blueController release];
}
[_yellowViewController.view removeFromSuperview];
[self.view insertSubview:_blueViewController.view atIndex:0];
}

}


- (void)viewDidLoad {
BlueViewController *blueController = [[BlueViewController alloc] 
  initWithNibName:@"BlueView" bundle:nil];
self._blueViewController = blueController;
//將藍色檢視加入成為根檢視的子檢視, 索引值為0表示此子檢視放在所有東西的最下層,
//如此可確保工具列永遠都看的見
[self.view insertSubview:blueController atIndex:0];
[blueController release];
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
if (self._blueViewController.view.superview == nil) 
{
self._blueViewController = nil;
}
else 
{
self._yellowViewController = nil;
}

}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
[_blueViewController release];
[_yellowViewController release];
    [super dealloc];
}


@end

將兩個檢視加入按鈕觸發動作

//
//  BlueViewController.h

#import <UIKit/UIKit.h>

@interface BlueViewController : UIViewController {

}

-(IBAction)blueButtonPressed;

@end


//
//  YellowViewController.h
#import <UIKit/UIKit.h>

@interface YellowViewController : UIViewController {

}

-(IBAction)yellowButtonPressed;

@end

將兩個檢視與 xib連結,  分別開啓BlueView.xib , YellowView.xib
ex: double click "BlueView.xib", select File's Owner, 按下command+4呼叫Identity Inspector
NSObject 改為 BlueViewController

更改view背景顏色為藍色

增加button, 連接blueButtonPressed, 除了連接IBAction, 還要將BlueViewController的View接口連接到nib中的view

YellowView的做法也與BlueView相同