简体   繁体   English

无法从第二个ViewController调用方法

[英]Can't call method from second ViewController

I am having problems getting my application to build. 我在构建应用程序时遇到问题。 I am trying to call a method from ViewController1 in my current ViewController2. 我试图在当前的ViewController2中从ViewController1调用一个方法。

In my .h file of ViewController1 i have declared the method so it is visible. 在ViewController1的.h文件中,我声明了该方法,因此该方法可见。

-(void)saveLocation;

In my ViewController2.h file i have the following 在我的ViewController2.h文件中,我有以下内容

    #import "ViewController1.h"



@interface ViewController2 : UIViewController

@property (nonatomic,assign) ViewController1 *MethodSave;

@end

and in my ViewController2.m 并在我的ViewController2.m中

[self.MethodSave saveLocation];

it doesn't work and I've tried to fix it for a while now. 它不起作用,我已经尝试修复了一段时间。 Any help would be great. 任何帮助都会很棒。 Thank you 谢谢

I think you have only declared a property @property (nonatomic,assign) ViewController1 *MethodSave; 我认为您只声明了属性@property (nonatomic,assign) ViewController1 *MethodSave; but have not allocated this property. 但尚未分配此属性。 So first allocate it then call the function. 因此,首先分配它,然后调用该函数。

MethodSave = [ViewController1 new];
[self.MethodSave saveLocation];

also declare the property as strong not assign @property (nonatomic,strong) ViewController1 *MethodSave; 还声明属性为强不分配@property (nonatomic,strong) ViewController1 *MethodSave; this should work. 这应该工作。

and try to declare variable with camel case. 并尝试使用驼峰式大小写声明变量。

Are you also importing ViewController2 in your ViewController1? 您是否也在ViewController1中导入ViewController2? According to my own Google search your problem could be a circular dependency. 根据我自己的Google搜索,您的问题可能是循环依赖。

If this is the case, instead of importing ViewController1 in ViewController2, create a protocol in ViewController2.h and replace the ViewController1 property with an id that conforms to that protocol. 在这种情况下,不要在ViewController2中导入ViewController1,而是在ViewController2.h中创建一个协议,然后用符合该协议的ID替换ViewController1属性。

Add the following in ViewController2.h: 在ViewController2.h中添加以下内容:

@protocol ViewController2Delegate<NSObject>
@required
-(void)saveLocation;
@end

@interface ViewController2:UIViewController
@property id<ViewController2Delegate> *delegate;
@end

and add this to ViewController1.h: 并将其添加到ViewController1.h:

@interface ViewController1:UIViewController <ViewController2Delegate>

and then set the delegate to an instance of ViewController1 (if you're loading ViewController2 from ViewController1, it would be self ), and be sure to remove the import from ViewController2. 然后将委托设置为ViewController1的实例(如果要从ViewController1加载ViewController2,则为self ),并确保从ViewController2中删除导入。

I'm doing this by memory so it may not work copy and pasted, but hopefully this points you in the right direction. 我正在通过内存执行此操作,因此它可能无法进行复制和粘贴,但是希望这可以为您指明正确的方向。

try this , 尝试这个 ,

ViewController1 *MethodSave = [[ViewController1 alloc]init];
[MethodSave saveLocation];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM