2014年2月25日 星期二

小提醒:在block中如果要改UI顯示,要記得用回main thread

//block會另開一個thread去跑,所以如果要顯示UI,要記得用GCD改原main thread
dispatch_async(dispatch_get_main_queue(), ^{
                cell.textLabel.text = device.name;

            });

2014年2月24日 星期一

UITableView的單選機制

//先在class中宣告一個全域變數int _selectedIndex = 0;

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     *  @單選機制
     */
    //取消前一個選取的
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_selectedIndex inSection:0];
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
    //lastCell.accessoryType = UITableViewCellAccessoryNone;
    lastCell.accessoryView = nil;

//取消所有目前看的到的Cell的(可以取代上面的區塊)
NSArray *visibleCells = [tableView visibleCells];
    for(UITableViewCell *_eachCell in visibleCells)
    {
        //_eachCell.accessoryType = UITableViewCellAccessoryNone;
        _eachCell.accessoryView = nil;

    }
    
    //選取的
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.accessoryView = button;
    
    //存入這筆選到的
    _selectedIndex = indexPath.row;
    //0.5秒後移除cell被選取時的效果
    [tableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:indexPath afterDelay:.5];

}

2014年2月21日 星期五

Storeboard用segue切換viewController

一般是直接用storyboard把圖上的segue拉好,不用加程式碼。
但也可以如下列程式碼用performSegueWithIdentifier

-(void)buttonAction:(id)sender
{
    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
}
//在執行上面的method之後,會先進來這個method,你可以在此把值pass進去
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegueName"]) 
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}

判斷是否有開啟網路

先下載這個Library
https://gist.github.com/darkseed/1182373

- (void)viewDidLoad {
    Reachability *internetReach = [[Reachability reachabilityForInternetConnection] retain];
    [internetReach startNotifer];
    Reachability *wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifer];

    NetworkStatus netStatus1 = [internetReach currentReachabilityStatus];
    NetworkStatus netStatus2 = [wifiReach currentReachabilityStatus];
    if(netStatus1 == NotReachable && netStatus2 == NotReachable)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"This feature requires an internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    else
    {//wifi connection available;
}
}

2014年2月20日 星期四

實作property的getter跟setter

@interface BLEManager : NSObject{
    CBPeripheral* _activePeripheral;
}



//實作activePeripheralgetter
-(CBPeripheral*) activePeripheral{
    return _centralController.activePeripheral;
}
//實作activePeripheralsetter
-(void) setActivePeripheral:(CBPeripheral *)activePeripheral{
    _activePeripheral = activePeripheral;

}

Note.實作完之後,就要手動完成property的其他事情。

bluetooth ble開發經驗

1.Scan到的peripheral一定要先把它指向一個strong CBPeripheral Property

2.這個strong Property不能去new它,不然就會出現用zombie也找不到的error

3.iOS跟android都不一定會收到所有的廣播,例如固定device每4秒發一次廣播,iOS實際收到的頻率可能是4,4,16,4,8,12,4,4,4,68,4,4,用lightBlue app看也是如此。

4.
-(id)init {
self = [super init];
if ( self )
{
//創一個thread專門給CentralManager使用
_centralQueue = dispatch_queue_create("forCentralManagerOnly", DISPATCH_QUEUE_SERIAL);
//It works without Warning message.
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:_centralQueue];

//用nil的話,預設是給它main queue
//_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

_receivedData = [[NSMutableData alloc] init];
//self.activePeripheral = [CBPeripheral new];
self.peripherals = [NSMutableArray new];

}

return self;

}

2014年2月19日 星期三

實作一個拍照的UIActionSheet

#pragma mark - Image capture
- (IBAction)takePicture:(UITapGestureRecognizer*)sender {
UIActionSheet *sheet;
sheet = [[UIActionSheet alloc] initWithTitle:@"Pick Photo"
delegate:self
  cancelButtonTitle:@"Cancel"
  destructiveButtonTitle:nil
  otherButtonTitles:@"Take Photo", @"Choose Photo", nil];
[sheet showInView:self.navigationController.view];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
switch (buttonIndex) {
case 0: {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
break;
case 1: {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}
break;
default:
break;
}

}

2014年2月18日 星期二

BLE回連 in iOS7

NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere

NSArray *peripherals = [_cbCentralManager retrievePeripheralsWithIdentifiers:@[uuid]];

for(CBPeripheral *periph in peripherals)
{
    [_cbCentralManager connectPeripheral:peripheral options:nil];
}

2014年2月13日 星期四

實驗:objective-C物件做為method的參數,應是copy,而不是直接傳指標

//程式碼如下
- (void)viewDidLoad
{
    [super viewDidLoad];

    int a = 2;
    NSArray* b = @[@2];
    NSLog(@"before:a=%d b=%@",a,b);
    [self testInt:a Object:b];
    NSLog(@"after:a=%d b=%@",a,b);
}

-(void) testInt:(int)a Object:(NSArray*) b{
    a = 1;
    b = @[@1];
    NSLog(@"processing:a=%d b=%@",a,b);


}

//Log如下
2014-02-13 13:26:54.160 testParse[11196:70b] before:a=2 b=(
    2
)
2014-02-13 13:26:54.161 testParse[11196:70b] processing:a=1 b=(
    1
)
2014-02-13 13:26:54.163 testParse[11196:70b] after:a=2 b=(
    2
)