gpt4 book ai didi

iphone - Xcode:委托(delegate)对象是否必须向委托(delegate)对象发送消息?

转载 作者:行者123 更新时间:2023-11-29 05:02:50 24 4
gpt4 key购买 nike

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

我可以假装 SecondViewController 确实从 FirstViewController 收到消息并响应 FirstViewController 吗? (注意:我的 SecondViewController 负责一个有按钮的 View 。当我按下 SecondViewController View 上的按钮时,我希望它告诉 FirstViewController 更新其 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
}

最佳答案

这方面存在一些问题,而且似乎有点困惑。

我假设 FirstViewController 和 SecondViewController 位于选项卡栏 Controller 中的单独选项卡中。

sendPressed: 方法中,您正在创建 FirstViewController 的新实例 - 这与标签栏 Controller 中的 FirstViewController 不同,以及为什么调用 realLabel没有效果。

第二点是您似乎误解了委派的工作原理 - 您发布的代码中没有理由需要委派。

了解委托(delegate)的好读物: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
}

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

希望这对您有所帮助。

关于iphone - Xcode:委托(delegate)对象是否必须向委托(delegate)对象发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6460206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com