網頁

2013年2月25日 星期一

ios中的方法宣告,訊息傳送,引數,與 本地名稱/公開名稱

[iphone app 開發]

我們來看一個例子


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

這個是在 pick view 的 delegate 中要求要實作的方法

其中
- (NSString *)  
- 表示他是實例方法(instance method)
(NSString *) 表示這個方法會回傳一個NSString 的指標


pickerView : 是方法名稱

後面帶一個冒號: 是為了定義這個方法可以讓其他人帶參數進來

(UIPickerView *)pickerView
這個表示,要用這個方法的人,要丟一個型別是(UIPickerView *)的東西進來,而
pickerView是我自定的local 變數,讓我可以自己稍後使用。
你就想成c 的  void test(int myint) 這樣。 myint 就是現在的pickerView。

接著,先看一下
(NSInteger)row 它是這個方法所要求帶進來的第二個變數。
可是在他前面,有個 titleForRow ,這個我們稱它為公開名稱。
意思就是,以後公開的讓別人取得row的值,但是別人要用 titleForRow來取得。

同樣道理。

forComponent:(NSInteger)component
forComponent 是第三個引數的公開名稱,讓別人用的。
component 是本地的local 變數名稱,讓自己用的。





================================
apple 文件中寫的很清楚
pickerView:titleForRow:forComponent:
Called by the picker view when it needs the title to use for a given row in a given component.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
Parameters
pickerView
An object representing the picker view requesting the data.
row
A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.
component
A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.
Return Value
The string to use as the title of the indicated component row.
Discussion
If you implement both this method and the pickerView:attributedTitleForRow:forComponent: method, the picker view prefers the pickerView:attributedTitleForRow:forComponent: method. However, if that method returns nil, the picker view falls back to using the string returned by this method.
Availability
  • Available in iOS 2.0 and later.
Declared In
UIPickerView.h

==============================================


訊息傳遞:在Ios中,通常我們以前說call 哪個function 會被改叫做傳什麼訊息,
因為在ios中,文件裡都是用傳遞訊息來溝通。

用上面的方法來當例子,如果有人要call 它來用,或是說有人想要傳訊息過去....

[pickerDelegate pickerView:somePicker titleForRow:1 forComponent:2]

pickerDelegat 是準備接收回傳訊息的人,換言之,它也是發起傳訊息的人,在等待回傳值。

pickerView 就是pickerDelegate 傳過去的方法名稱。
帶入兩個參數 給 titleForRow 的是1,給 forComponent 的是2。

這樣,相信會看apple doc 了吧。
















沒有留言:

張貼留言