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
}
説明ありがとうIWN :-)私の論理の誤り(すなわち、この問題)は過去数日間私を悩ませています。私はホッとする。再度、感謝します。 – NateHill
助けてくれてうれしい! – InsertWittyName