2011-06-23 174 views
0

我想分配SecondViewController作爲FirstViewController的代理對象(如果我理解正確)。但是,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沒有任何效果。

第二點是您似乎誤解了委派的工作原理 - 在您發佈的代碼中沒有任何理由讓委託人參與。

用於獲取認真處理代表良好讀取:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

作爲一個解決問題的方法有幾個選項:

  1. 後從SecondViewController該FirstViewController是觀察(大量信息的通知關於通知可在網上獲得)。
  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