Xcode:委托对象是否必须向委托对象发送消息?

发布于 2024-11-17 01:22:24 字数 2385 浏览 0 评论 0原文

我试图将 SecondViewController 指定为 FirstViewController 的委托对象(如果我理解正确的话)。但是 FirstViewController 不会向 SecondViewController 发送任何消息。

我是否可以假装 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 =@"Real";
    [self.view setNeedsDisplay];
}

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

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    label.text=@"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
}

I'm trying to assign SecondViewController as a delegate object of FirstViewController (if I understand correctly). However FirstViewController doesn't send any messages to SecondViewController.

Am I allowed to pretend as though SecondViewController did get a message from FirstViewController and respond to the FirstViewController? (Note: My SecondViewController is in charge of a view that has a button. When I press the button on my SecondViewController's view I want it to tell the FirstViewController to update its view)

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 =@"Real";
    [self.view setNeedsDisplay];
}

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

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    label.text=@"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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-11-24 01:22:24

这有一些问题,而且似乎有点混乱。

我假设 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
}

还有比这更多的选项,但以上内容将为您提供一个良好的开端,让您研究适合您需求的最佳方法。

希望这有帮助。

There are a few issues with this and what appears to be a bit of confusion.

I assume that FirstViewController and SecondViewController are in separate tabs in the tab bar controller.

In the sendPressed: method, you're creating a new instance of FirstViewController - this is not the same FirstViewController that is in your tab bar controller and why calling realLabel has no effect.

The second point is that you appear to misunderstand how delegation works - there is no reason for a delegate in the code you posted.

Good read for getting to grips with delegates: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

As for a solution to your problem there are a few options:

  1. Post a notification from SecondViewController that FirstViewController is observing (lots of information available on the net regarding notifications).
  2. Get the specific instance of FirstViewController within the self.tabBarController.viewControllers array and call the method from there. Something like...
- (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
}

There are more options available than this, but the above will give you a good start into researching the best approach for your need.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文