2012-03-30 93 views
3

我是客觀編程的新手,對於愚蠢的問題非常抱歉。 我正在爲一些社交網絡做一些類型的消息傳遞,而我被困在非常簡單的事情 - 如何從一個類的對象發送消息到另一個類的對象?iphone - 如何發送消息從一個對象到另一個?

我有一個類,叫做SignInViewController,它創建了一個SignUpButton後SignUpViewController的實例按下它完成就像這樣:

SignUpViewController *signUpViewController = [[SignUpViewController alloc]init]; 
signUpViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentModalViewController:signUpViewController animated:YES]; 

又過了些管理與AFNetwork做(我使用具體的類此,所謂的ServerManager的),我想向我的SignUpViewController實例繪製新文本框的消息,我認爲這是可以做到這樣的:

在SignUpViewController.h

- (void)showTheCodeTextField; 
在ServerManager.m

[[SignUpViewController self] showTheCodeTextField]; 

,然後在SignUpViewController.m:

-(void)showTheCodeTextField 
{ 
    NSLog(@"Time to draw codeTextField"); 
} 

我在代碼後線得到一個熟悉的SIGABRT。我知道我做錯了什麼,但我無法弄清楚究竟是什麼。

請問你能幫我嗎?

+0

哪個類連接如何?沒有任何意義..請清楚,以便有人可以幫助.. – 2012-03-30 17:33:54

回答

2

您可以使用NSNotificationCenter來完成這項工作。 在呈現您的SignUpController後,您將其添加爲ServerManager發送的通知的觀察者。當這個通知來了,你可以打電話給你的消息來畫出視圖。

於是,經過

SignUpViewController *signUpViewController = [[SignUpViewController alloc]init]; 
signUpViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentModalViewController:signUpViewController animated:YES]; 

讓觀察者

[[NSNotificationCenter defaultCenter]signUpViewController selector:@selector(showTheCodeTextField:) name:@"SignUpSucceded" object:[ServerManager sharedInstance]]; 

比,在你的服務器管理器,你發送通知,containig是文本框,並在您註冊的方法獲取called.That的你的textField文字在你的notification.userinfo

[[NSNotificationCenter defaultCenter]postNotificationName:@"SignUpSucceded" object:self userInfo:[NSDictionary dictionaryWithObject:textField.text forKey:@"login"]]; 
    } 

Gett在signUpView這裏荷蘭國際集團旅遊文本

-(void)showTheCodeTextField:(NSNotification*)notification 
{ 
    NSLog(@"Time to draw codeTextField %@",[notification userInfo]); 
} 
1

您的ServerManager類需要引用SignUpViewController實例。現在你得到的類不是類實例的self。您應該在您的ServerManager類中擁有一個引用您的SignUpViewController實例的屬性。

1

貌似你試圖將消息發送到一個類(SignUpViewController)而不是它的實例/對象(signUpViewController):

變化

[[SignUpViewController self] showTheCodeTextField]; 

[[signUpViewController self] showTheCodeTextField]; 

,你應該沒問題

+1

你假設他有一個參考。 – mydogisbox 2012-03-30 17:40:35

+0

@mydogisbox:那是真的:正確的答案是你和我的組合:分裂7.5/7.5? ;) – 2012-03-30 17:42:32

+1

聽起來對我很好! – mydogisbox 2012-03-30 17:46:27

1

如果您在ServerManager的實例有SignUpViewController的實例,你會使用這種約定(假設你的實例名爲「signUpController」):

[[self signUpController] showTheCodeTextField]; 

另一種可能性是送你的ServerManager的通知包含對SignUpViewController的引用,並將showTheCodeTextField消息傳遞給它。這假定你有如何發送通知的知識。

祝你好運。你看起來像剛剛開始使用Cocoa和Objective-C。掛在那裏!

2

它是更好,如果你使用的按鈕事件委託聲明你,你創建你的按鈕,只需使用該委託任何類代表在任何你只想使用該委託是非常有用的學習代表爲客觀的c其簡單而高效

相關問題