顯示具有 Objective-C 標籤的文章。 顯示所有文章
顯示具有 Objective-C 標籤的文章。 顯示所有文章

2011年2月22日 星期二

[iOS Dev] 自動出現鍵盤並延遲出現 UIKeyboard auto show and delay

我在弄一個功能, 從 MainViewController 換到SecondViewController後, 自動顯示鍵盤

本來是寫在ViewDidLoad裡, 讓鍵盤自動跑出來

- (void)viewDidLoad {
    [self.textField becomeFirstResponder];
    [super viewDidLoad];
}

但是這樣換頁動畫還沒完, 鍵盤會同時蹦出來, 我想要的是畫面Load完, 鍵盤再從下面蹦出來

所以用了另一種方法如下:

- (void)enableKeyboard {
 [self.textField becomeFirstResponder];
}

- (void)viewDidLoad {
    [self performSelector:@selector(enableKeyboard) withObject:nil afterDelay:.3]; 
    [super viewDidLoad];
}

這樣子就可以在viewDidLoad之後延遲0.3秒再顯示鍵盤, 我的換頁動畫設定0.3秒, 所以延遲也設0.3秒, 這樣動畫顯示完會恰恰好蹦出來

2011年2月11日 星期五

[iOS Dev] 簡單使用 UIActionSheet 和 iPad上的注意事項

簡單的UIActionSheet範例:

在.h加上 UIActionSheetDelegate 的 Protocols 實作 clickedButtonAtIndex 事件

1. 在iPad上不能使用在ViewDidLoad, 會出現Exception錯誤, 所以拉一個Button用IBAction去執行
2. 在iPad上點擊其他空白地方時, 會自動關閉UIActionSheet, buttonIndex會回傳3, 但是如果將cancelButtonTitle設成nil的話, buttonIndex會回傳-1

Source Code

2011年2月9日 星期三

[iOS Dev] 簡單的網路連結測試 Simple Internet Connection test

以下範例為使用 NSURLConnection 測試網路連結

若網路連結失敗會回傳錯誤訊息 kCFURLErrorNotConnectedToInternet , 則使用UIAlertView來回報錯誤 (也可以做成Cancel and Retry讓使用者選擇)




若用 iPhone Simulator 是用Mac的網路連結, 所以測試前, 要先把Mac的網路斷線

2011年2月8日 星期二

[Objective-C] Classes 筆記 (3) - Property 屬性 (setter & getter)

在Objective-C 2.0之後, 可以透過@property@synthesize來達到setter & getter的效果(或稱accessors & mutators)

[Objective-C] Classes 筆記 (2) - Method, Message Sending 方法與訊息傳遞

Methods
  1. instance method (宣告時在開頭用 - )
  2. class method (宣告時在開頭用 + )
    • 用法像是JAVA的static method
差別在於instance method要先實例化, class method可以直接透過Object使用
-(void) instanceMethod:(NSString *)param;
+(void) classMethod:(NSString *)param;


Message Sending -- 訊息傳遞

[object method:param];

有點類似其他語言的call function or call method, 但是嚴格上來說是不一樣的
object.methodName(params);

Message Sending即使你打錯method name或是method不存在的時候, compiler並不會顯示錯誤, 因為object會自動忽略, 在XCode上, 只會出現警告訊息, 'Object' may not respond to '-method'

[Objective-C] Classes 筆記 (1) - 基本的實作與宣告

@interface - 可以宣告在header file(.h) 或是 implement file(.m)

  • Declare a class
@interface MyClass : NSObject {
}

  • Declare View Controller Class
@interface MyViewController : UIViewController {
}


2011年1月27日 星期四

2011年1月25日 星期二

[Objective-C] 將NSDictionary新增至NSMutableArray

建立一個words的陣列, 然後建立一個dictionary存入words

Source Code
// 宣告array
NSMutableArray *words = [NSMutableArray array];

// 宣告dictionary
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
   @"Title 01", @"title",
   @"Explain 01", @"explain",
   nil];

// 將dict新增至array裡
[words addObject:dict];

// print出dict中所有資料
for (id key in dict) NSLog(@"key: %@  value: %@", key, [dict objectForKey:key]);

// print出Array中所有資料
for (NSInteger i=0; i < [words count]; i++) NSLog(@"%@", [words objectAtIndex:i]);

// print Array有幾筆資料
NSLog(@"Words count : %i", [words count]);
Result








See Also

NSDictionary Class Reference
NSMutableArray Class Reference

2011年1月17日 星期一

[Objective-C] NSTimer snippet

[NSTimer
    scheduledTimerWithTimeInterval:2
    target:self
    selector:@selector(doSomething)
    userInfo:nil
    repeats:NO];

NSTimer Class Reference
Related Posts Plugin for WordPress, Blogger...