Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么';你不能委派工作吗?_Ios_Objective C_Delegates - Fatal编程技术网

Ios 为什么';你不能委派工作吗?

Ios 为什么';你不能委派工作吗?,ios,objective-c,delegates,Ios,Objective C,Delegates,我一直在学习 因此,我编写了委托的代码,然后运行此代码 你能告诉我有什么问题吗 这是我的密码 ViewController2.h @protocol ViewController2Delegate <NSObject>; @required -(void)practiceDelegateMethod:(Float32)var1 andVar2:(Float32)var2; @end @interface ViewController2 : UIViewController @prop

我一直在学习

因此,我编写了委托的代码,然后运行此代码

你能告诉我有什么问题吗

这是我的密码

ViewController2.h

@protocol ViewController2Delegate <NSObject>;
@required
-(void)practiceDelegateMethod:(Float32)var1 andVar2:(Float32)var2;
@end
@interface ViewController2 : UIViewController
@property (assign, nonatomic) id <CompressSetupViewControllerDelegate> delegate;
视图控制器1.h

@interface HomeViewController : UIViewController< ViewController2Delegate >

在HomeViewController中创建
ViewController2
assign delegate self的实例时

    ViewController2 *controller = [[ViewController2 alloc]init];
    controller.delegate = self

我认为问题在于,由于某些问题,您无法在viewController1中获得viewController2的引用。 在viewcontroller1中获取viewController2的非nil引用,然后

.delegate=self

我想您在viewcontroller1中对ViewController2的引用为nil。

更换后再试一次

@property (assign, nonatomic) id <CompressSetupViewControllerDelegate> delegate;
@属性(赋值,非原子)id委托;
与:

@属性(赋值,非原子)id委托;

在ViewController之间传递数据还有另一种方法
NSNotification

在发送消息的类中,发布如下通知:

[[NSNotificationCenter defaultCenter] postNotificationName: @"YOUR_NOTIFICATION_NAME" object: anyobjectyouwanttosendalong(can be nil)];
在要在发布通知时通知您的视图控制器中:

在viewDidLoad中,执行以下操作:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(METHOD_YOU_WANT_TO_INVOKE_ON_NOTIFICATION_RECEIVED) name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];
重要!不要在viewDidUnload()中忘记这一点:

注意:-当只有一个对象通知另一个对象时,最好使用协议:)但在这种情况下,由于有多个视图控制器在侦听,请使用通知

如果您想使用协议,请参考上面的链接


在ViewController1上,是否设置了ViewController2.delegate=self@CongTran yes,在viewDidLoad中设置。@CongTran VIEWCONTROLER2DELEGATE*vc2=[[VIEWCONTROLER2DELEGATE alloc]init];vc2.delegate=self;创建这样的视图控制器看起来不正确。是否确实要在viewController1中获取viewController2的引用,然后调用.delegate=self?。我认为您在viewcontroller1中对ViewController2的引用为零。用户不必创建新的引用。。。他们也可以从其他地方获得它…是的,不需要创建新的引用,但这只是为了理解目的,原始代码是CompressSetupViewControllerDelegate;在创建类的对象之后,在ViewController1中?那么CompressSetupViewControllerDelegate协议的代码在哪里?您好,您的问题有点让人困惑:因为您说原始代码是CompressSetupViewControllerDelegate,并且没有CompressSetupViewControllerDelegate协议的代码,并且在您编写的ViewController1.h文件中. 基本上您想使用ViewController2Delegate或CompressSetupViewControllerDelegate?谢谢您的帮助。我已解决问题,请使用通知。谢谢您的帮助。我已解决问题使用通知。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-@JisooKwon我编辑了我的答案,如果它是正确的,那么就给它打上正确的分数,这样它对其他人的帮助:)和@NeilMasson谢谢你的建议:)
@property (assign, nonatomic) id <ViewController2Delegate> delegate;
[[NSNotificationCenter defaultCenter] postNotificationName: @"YOUR_NOTIFICATION_NAME" object: anyobjectyouwanttosendalong(can be nil)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(METHOD_YOU_WANT_TO_INVOKE_ON_NOTIFICATION_RECEIVED) name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];