2012-03-21 126 views
0

我傳遞一個字符串通過委託和協議。該字符串正確接收到類,但是在調用viewDidLoad方法後發生。 viewDidLoad方法需要傳遞的字符串。iPhone協議委託傳遞數據

關於我能做些什麼來獲得在viewDidLoad之前調用委託方法的任何想法?我認爲這是委託/協議數據傳遞的想法。

方法,其中,創建新的視圖,並推動:

ViewControllerTwo *two = [[ViewControllerTwo alloc] initWithNibName:@"ViewControllerTwo" bundle:nil]; 
two.delegate = self; 
[two setString:theString]; 
[self.navigationController pushViewController:two animated:YES]; 
[two release]; 

在ViewControllerTwo:

- (void)setString:(NSString *)str 
{ 
    self.myString = str; 
} 

編輯:謝謝你的輸入。但是,我明白如何通過init方法傳遞這些數據。我最近一直在測試協議和代表,我想看看是否有辦法做到這一點。我已經成功地在另一個類中傳遞了像這樣的數據,並且它工作。首先調用協議方法來設置字符串。這似乎是處理傳遞數據的更簡潔的方法。

+0

我編輯了我的問題。我想看看這是否可以通過委託,或者我做錯了什麼,因爲我在另一個班級工作。在init和viewDidLoad之前,委託方法將首先被調用。 – Vikings 2012-03-21 22:18:46

回答

1

我想你可能會對Delegation的用途和原因有些困惑。

例如,如果您在ViewController中執行某種操作,並且需要通知另一個子類正在執行該操作或該操作的結果,則可能需要在UIViewController子類中制定協議。

現在爲了讓想要了解動作(接收者)的子類,它必須在其頭文件中符合該協議。

您還必須將delegate設置爲receiving class/controller

有很多方法可以參考receiving controller/class將其設置爲delegate,但常見的錯誤是分配並初始化該類的新實例,以便在該類已創建時將其設置爲委託。所做的就是將新創建的類設置爲委託,而不是已創建的類並等待消息。

你想要做的只是將一個值傳遞給一個新創建的類。由於您只需創建此類UIViewController所需的全部內容,因此屬於receiver(ViewControllerTwo)中的一個屬性。

在你的情況下,NSString

@Property (nonatiomic, retain) NSString *string; //goes in ViewControllerTwo.h 

,當然也不要在主忘記:

@synthesize string; //Goes in ViewControllerTwo.m 

現在有沒有必要在你的ViewControllerTwo二傳手。

- (void)setString:(NSString *)str //This Method can be erased 
    {         //The setter is created for free 
     self.myString = str;   // when you synthesized the property 
    } 

當您使用@synthesize時,setter和Getters是免費的。只需將值傳遞給ViewController即可。執行與您的代碼相同,但代表除外:

ViewControllerTwo *two = [[ViewControllerTwo alloc] initWithNibName:@"ViewControllerTwo" bundle:nil]; 
    [two setString:theString]; 
    [self.navigationController pushViewController:two animated:YES]; 
    [two release]; 
0

如果您需要在初始化階段傳遞該字符串,您可以在init方法中傳遞它。

所以在你的控制器,你需要創建一個這樣的屬性和額外的init方法:

.H

@property (nonatomic, copy) NSString* myString; 

-(id)initWithString:(NSString*)theString; 

.M

@synthesize myString; 

-(id)initWithString:(NSString*)theString { 
    self = [super initWithNibName:@"ViewController" bundle:nil]; // I prefer to set the name of the controller internally 
    if(self) { 
     myString = [theString copy]; 
    } 
    return self; 
} 

然後,使用self.myString無論你想。

如果您不使用ARC,請記住發佈。

- (void)dealloc 
{ 
    [myString release]; 
    [super dealloc]; 
} 
0

您可以爲ViewControllerTwo創建自定義初始值設定項,例如:在ViewControllerOne

- (id)initWithString:(NSString *)aString; 
{ 
    self = [super initWithNibName:@"ViewControllerTwo" bundle:nil]; 
    if(!self) return nil; 

    self.myString = aString; 
    // other initialization 

    return self; 
} 

您初始化也只是:

ViewControllerTwo *vc = [[ViewControllerTwo alloc] initWithString:theString]; 
[[self navigationController] pushViewController:vc animated:YES]; 
[vc release]; 
0

我不會用一個協議這一點。要將數據從父級視圖控制器傳遞給子級,請使用自定義初始化方法。

在ViewControllerTwo.h:

-(id)initWithString:(NSString *)str; 

,然後實現在您的ViewControllerTwo.m:

-(id)initWithString:(NSString *)str { 
    self = [super self]; 
    if(self) { 
     self.myString = str; 
    } 
    return self; 

然後打電話給在您的視圖控制器之一:

ViewControllerTwo *two = [[ViewControllerTwo alloc] initWithString:@"Cool String"]; 
0

將數據放在init方法或viewDidLoad中將不起作用,因爲用戶可以在不使用u的情況下來回切換加載視圖或重新初始化視圖控制器。

檢索更改數據的最佳位置是viewWillAppear控制器方法。這樣每次用戶切換時數據都會被更新。

另一個最好的方法是使用MVC架構。如果你有單獨的模型類,它將保存數據,你可以從任何控制器寫入/更新。

+0

調用dealloc方法,以便在每次加載視圖時創建視圖,並正確傳遞數據。 – Vikings 2012-03-21 22:13:24

+0

我不想使用MVC,因爲它只有一個對象通過。對我的項目來說似乎過度殺傷。 – Vikings 2012-03-21 22:17:37