2012-11-08 58 views
0

我是新來的目標C,並試圖掌握代表的方法和用法。目前我有Alertview類,我需要它來創建另一個類來調用它的功能。我設法與appdelegate類一樣。但是不能和我自己的班級一起做。無法使用委託

   TableViewController *newControll = (TableViewController*)[UIApplication sharedApplication].delegate; 
       [newControll openSettings]; 

這就是我正在嘗試訪問我的方法。 Compilator在newControll中看到這個方法,但是調用這個方法會得到unrecognizet選擇器。基本上,我需要從另一個類調用其中已經創建的函數。我很抱歉,如果這是一個簡單的解決方案,但我不能完全掌握delegate和objective-c。

也許不是每個人都得到了我需要做的,所以我會試着再解釋一次。我的對象TableViewController。從這個對象裏面我呼叫類AlertView顯示一些警報消息。並因此在警報對話框中的用戶交互(密碼是好的,密碼不是)我需要撥打我的TableViewController中的方法openSettings或不打電話。那麼如何做到這一點?

+0

我不知道你想要,但編譯器說起事情..)起初 - 你確定[UIApplication sharedApplication] .delegate是不是零?第二個 - [UIApplication sharedApplication] .delegate可能沒有任何選擇器openSettings。 –

+0

它會幫助你發佈一些代碼! – footyapps27

+0

你想看哪個部分? – Datenshi

回答

1

如果您的TableViewController不是您的appdelegate類,則應該使用TableViewController的對象來使用方法openSettings

它應該是這樣的,

TableViewController *newControll = [[TableViewController alloc] init]; 

假設你正在向一個新的alertView。在你AlertView.h文件中添加,

@property(nonatomic, retain) TableViewController *tableViewController; 

以及在創建新的alertView對象,

AlertView *alertView = [[AlertView alloc] init]; 
//some code.. 
alertView.tableViewController = self; 

現在,在您AlertView類,稱呼其爲,的appdelegate的

[self.tableViewController openSettings]; 

使用率不要做到這一點。

如果你需要iOS上的一些教程,check raywenderlich

+0

嘗試過這種方式,但它沒有正確地完成它,因爲我需要對之前創建的確切對象進行調用,而不是創建新對象並撥打電話。 – Datenshi

+0

newControll在哪裏創建?在哪個級別?你想在哪裏調用這個方法?反正更新了我的答案。 – iDev

+0

我已經更新了我的問題,請看看,也許它會更清楚我正在嘗試做什麼 – Datenshi