2011-06-23 13 views
0

FirstViewControllerの代理オブジェクトとしてSecondViewControllerを割り当てようとしています(正しく理解していれば)。ただし、FirstViewControllerはSecondViewControllerにメッセージを送信しません。Xcode:委任オブジェクトはデリゲートオブジェクトにメッセージを送信する必要がありますか?

私は、SecondViewControllerからFirstViewControllerからメッセージを受け取り、FirstViewControllerに応答したように見せかけましたか? (注:私SecondViewControllerがボタンを持っているビューを担当している私は私のSecondViewControllerのビュー上のボタンを押すと、私はそれがそのビューを更新するためにFirstViewControllerをお伝えしたいと思います。)

FirstViewController.h

#import <UIKit/UIKit.h> 

@protocol FirstViewControllerDelegate <NSObject> 
@optional 
- (void) setAnotherLabel; 
@end 

@interface FirstViewController : UIViewController { 
    IBOutlet UILabel *label; 
    id <FirstViewControllerDelegate> delegate; 
} 
@property (nonatomic, retain) IBOutlet UILabel *label; 
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate; 
- (void) pretendLabel; 
- (void) realLabel; 
@end 

FirstViewController.m

#import "FirstViewController.h" 


@implementation FirstViewController 
@synthesize label; 
@synthesize delegate; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void) setAnotherLabel; 
{ 
    label.text [email protected]"Real"; 
    [self.view setNeedsDisplay]; 
} 

- (void) pretendLabel; 
{ 
    label.text [email protected]"Pretend"; 
    [self.view setNeedsDisplay]; 
} 

- (void) realLabel; 
{ 
    [self setAnotherLabel]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [email protected]"Load"; 
    [self pretendLabel]; 
} 
... 
@end 

SecondViewController.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "FirstViewController.h" 

@interface SecondViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, FirstViewControllerDelegate> 
{ 
    UIImage *image; 
    IBOutlet UIImageView *imageView; 
} 

- (IBAction) sendPressed:(UIButton *)sender; 
- (IBAction) cancelPressed:(UIButton *)sender; 
@end 

SecondViewController.m

#import "SecondViewController.h" 

@implementation SecondViewController 

- (IBAction) sendPressed:(UIButton *)sender 
{ 
    FirstViewController *fvc = [[FirstViewController alloc] init]; 
    [fvc setDelegate:self]; 
    //how do I find out if I'm actually the delegate for FirstViewController at this point? 
    [fvc realLabel]; 
    self.tabBarController.selectedIndex = 0;//switch over to the first view to see if it worked 
} 

答えて

1

数本の問題とどのような混乱のビットのように見えるがあります。

FirstViewControllerとSecondViewControllerがタブバーコントローラの別々のタブにあるとします。

sendPressed:メソッドでは、FirstViewControllerの新しいインスタンスを作成しています。これはタブバーコントローラにあるFirstViewControllerと同じではなく、なぜrealLabelを呼び出すのも効果がありません。

2番目の点は、委任の仕組みを誤解しているように見えることです。投稿したコードに代理人がいる理由はありません。

代表団とのグリップを取得するための良い読み物:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

あなたの問題を解決するためにいくつかのオプションがあるとして:

  1. 投稿FirstViewControllerは、(多くの情報を観察していることをSecondViewControllerからの通知通知に関してネット上で利用可能)。
  2. self.tabBarController.viewControllers配列内のFirstViewControllerの特定のインスタンスを取得し、そこからメソッドを呼び出します。以下のような何か...
- (IBAction) sendPressed:(UIButton *)sender 
{ 
    for(UIViewController *controller in self.tabBarController.viewControllers) 
    { 
     if([controller isKindOfClass:[FirstViewController class]]) 
     { 
      FirstViewController *firstViewController = (FirstViewController *)controller; 
      [firstViewController realLabel]; 
     } 
    } 

    self.tabBarController.selectedIndex = 0;//switch over to the first view to see if it worked 
}

はこれより利用できる多くのオプションがありますが、上記のあなたの必要性のための最適なアプローチを研究にあなたの良いスタートを与えます。

これが役に立ちます。

+0

説明ありがとうIWN :-)私の論理の誤り(すなわち、この問題)は過去数日間私を悩ませています。私はホッとする。再度、感謝します。 – NateHill

+0

助けてくれてうれしい! – InsertWittyName

関連する問題