2015年1月29日 星期四

NSUserDefaults無預警變為nil的問題

//在每次改變其值之後都執行它的synchronize方法即可
[[NSUserDefaults standardUserDefaults] synchronize];

2015年1月28日 星期三

解決alertController無法像alertView那樣重覆推出來的問題

UIViewController *presentingViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
        //因為一個ViewController只能present新的VC一次,故如果它present過了,就去抓取它present出來的VC來再present新的VC
        if (presentingViewController.presentedViewController != nil)
        {
            presentingViewController = presentingViewController.presentedViewController;
        }

        [presentingViewController presentViewController:alertController animated:YES completion:nil];

2015年1月27日 星期二

如何將NSString轉成NSNumber

//新增一個numberFormatter
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//將style設定為NSNumberFormatterDecimalStyle
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
//NSString轉成NSNumber

NSNumber *piNumber = [numberFormatter numberFromString:@"3.141592654"];

Note.若數字格式不正確,則piNumber會被指定為nil。

2015年1月26日 星期一

判斷是否使用UIAlertController


//重點一:UIAlertController要在畫面出來之後才可以使用,所以要寫在viewDidAppear不可以寫在viewDidLoad

//重點二:判斷時最好使用「尋找方法是否存在」的方式,而不要以iOS版本做判斷,以免造成未來改版時的誤判。
if (NSClassFromString(@"UIAlertController") != nil)
        {
            //使用UIAlertController
        }
        else {
            //使用UIAlertView

        }

2015年1月13日 星期二

button改title的小地方要注意一下

//應該用setTitle來改
[self.signUpButton setTitle:@"註冊" forState:UIControlStateNormal];

//下面這種方式是改不了的,因為titleLable是唯讀
self.signUpButton.titleLabel.text = @"註冊";

2015年1月9日 星期五

[[NSUserDefaults standardUserDefaults] setObject:object forKey:key];的物件限制

The value parameter can be only property list objects: NSDataNSStringNSNumberNSDateNSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. See What is a Property List? in Property List Programming Guide.

2015年1月7日 星期三

在CoreData之前,將物件儲存於disk的方式:Archiving Object

一、在我們自定義的物件的.h中宣告<NSCoding>這個Protocol。

二、在我們的物件的.m檔中,實作<NSCoding>中的這二個方法(required),程式碼中的_userName和_phone是這個自定義物件(member)的property
//將property編碼為binary code file
- (void)encodeWithCoder:(NSCoder *)coder {
    [super encodeWithCoder:coder];
    [coder encodeObject:_userName forKey:@"user_name"];
    [coder encodeInteger:_phone forKey:@"phone"];
}
//將property從binary code file中解碼
- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    _userName = [coder decodeObjectForKey:@"user_name"];
    _phone = [coder decodeIntegerForKey:@"phone"];
}

三,使用下列的方式來操作這個自定義物件:
//儲存
BOOL result = [NSKeyedArchiver archiveRootObject:member toFile:path];
//讀取
Member *member = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

談談Region Monitoring數量限制及文件未提及的部份

一、問題:

  1. Region Monitoring是否有數量限制?
  2. Beacon Region 跟 Circular Region是否共用數量限制?
  3. 如果超過數量限制時,再加入Circular會發生什麼樣的情形?

二、實作:

1. 目前有1個Beacon與19個Circular,總共20個監控。


 2. 加入第20個Circular成功,總共21個監控。

3. 加入第21個Circular失敗,包含Beacon總共21個監控。

三、結論:

1. 加入Region Monitoring限制為20個,這個問題在Apple文件上有寫明。
2. Beacon與Circular數量限制並無共用,Circular是二十個,Beacon尚未測試。
3. Circular區域監控是以identifier為主Key。要加入下一個時,如果已有相同的identifier,則會用新的GPS座標取代原有的;如果沒有且已滿20個的話,則會忽略。(如果未滿,則會新增為新地區。)