Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
Iphone MainViewController.m和FlipsideViewController.m之间的调用方法_Iphone_Objective C_Xcode_Methods_User Defined Functions - Fatal编程技术网

Iphone MainViewController.m和FlipsideViewController.m之间的调用方法

Iphone MainViewController.m和FlipsideViewController.m之间的调用方法,iphone,objective-c,xcode,methods,user-defined-functions,Iphone,Objective C,Xcode,Methods,User Defined Functions,请参见以下当前(次要)问题的编辑 我试图在MainViewController.m和FlipsideViewController.m之间调用方法(方法正确,而不是函数?)——从一个文件/类调用到另一个文件/类 我猜这就是通常所说的“从另一个类调用方法”。我知道有很多这样的问题,但我就是不能让它正常工作 在我的例子中,我在上述两个文件中都有几个用户定义的方法/函数。有时,我需要从MainViewController.m文件中的FlipsideViewController.m中调用一个方法: //

请参见以下当前(次要)问题的编辑


我试图在MainViewController.m和FlipsideViewController.m之间调用方法(方法正确,而不是函数?)——从一个文件/类调用到另一个文件/类

我猜这就是通常所说的“从另一个类调用方法”。我知道有很多这样的问题,但我就是不能让它正常工作

在我的例子中,我在上述两个文件中都有几个用户定义的方法/函数。有时,我需要从MainViewController.m文件中的FlipsideViewController.m中调用一个方法:

// in MainViewController.m

- (void) calculateDays {
    //executes caluculations
   // inserts data into labels, etc 
}
如果我想从同一个文件中调用此函数,我只需执行以下操作:

[self calculateDays];
这很容易,但是,我也希望从FlipsideViewController.m文件中调用此函数,反之亦然那么我该怎么做呢,有些问题可以回答这个问题,但对我来说不太管用。我马上解释原因

这是我尝试过并认为应该有效的方法:

MainViewController *mvs = [[MainViewController alloc] init]; //alloc init MVC
[mvs calculateDays]; //call "external" function
//method called from within self on viewDidLoad: [self calculateDay];
Processed value to update label with: 26
New value in outlet after having been inserted: 26


//method called from another (FlipsideViewController) class file: [mvs calculateDay];
Processed value to update label with: 26
New value in outlet after having been inserted: (null)

/* 
  This doesn't work either from that external file: 
  [[mvs LabelName] setText:@"Hello, update label!"]; no errors but no display either

*/
它给了我一个错误:“未知类型名MainViewController”。因此,我假设我必须以某种方式包含/导入它才能工作(就像在javascript或PHP中一样)。因此,我将其包括在FlipSideViewController.m类中:

 #import "MainViewController.h"
很好,到目前为止没有错误。然后我尝试编译/构建它,但遇到了另一个错误: “叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)” “ld:架构armv7s的3个重复符号”

这让我想到,像那样导入MainViewController不是一个好办法,,因为我随后导入了很多其他东西,这些东西可能会干扰FlipSideViewController类中的一些代码

我尝试过类似的解决方案,但似乎没有任何效果。请任何人向我解释一下我做错了什么,以及如何正确地做到这一点:MainViewController.m和FlipsideViewController.m之间调用方法,反之亦然


H2CO3提出的解决方案确实解决了大部分问题(XCode有一段时间出现了错误,并给了我随机错误,迫使我重新构建整个项目),但仍有一件事不太管用:更改UILabel(UIOutlet)的内容。请看你们中是否有人能帮我:

当从self内部(即[self calculateDay])调用该方法时,该值将成功插入UILabel。从FlipsideViewController调用时,要插入的值存在并已成功处理,但无法插入UILabel。请看下面

一些日志:

MainViewController *mvs = [[MainViewController alloc] init]; //alloc init MVC
[mvs calculateDays]; //call "external" function
//method called from within self on viewDidLoad: [self calculateDay];
Processed value to update label with: 26
New value in outlet after having been inserted: 26


//method called from another (FlipsideViewController) class file: [mvs calculateDay];
Processed value to update label with: 26
New value in outlet after having been inserted: (null)

/* 
  This doesn't work either from that external file: 
  [[mvs LabelName] setText:@"Hello, update label!"]; no errors but no display either

*/

如果您改为导入头,那么应该会提供所有必要的声明,但不会出现“复制符号”链接器错误。这是编写(目标)C代码的“标准”/“常用做法”


(外行术语)在Objective-C中,您只能使用每个文件都知道的对象。在本例中,您尝试在FlipsideController.m文件中使用MainViewController。FlipsideController.m不知道MainViewController是什么,因此它会抛出错误,因为它不知道它是什么或如何使用它。您有两个选项可以告诉Flipsidecontroller什么是MainViewController,您可以导入标题(
#导入“MainViewController.h”
),这将使您能够完全访问Flipsidecontroller.h中定义的所有内容。(除非您真的知道自己在做什么,否则您可能永远不应该导入.m)您还可以在.h中创建转发声明-
@class filpsideconroller
,并在.m中导入文件。这对于避免循环导入等非常有用。

谢谢,但我已经尝试过了,它给了我编译器错误:“MainViewController没有可见的[at]接口声明选择器'calculateDays'”,因此我尝试以与FlipSideViewController:[at]接口MainViewController()类似的方式添加[at]接口@end,我又犯了一个错误。。所以我想这不是解决方案?@Lindros如果你的类写得很好,它应该是这样工作的,不会对其他源文件进行严重的黑客攻击和垃圾填充。您必须以某种方式声明类,以便编译器知道它实际实现了这个方法。如果没有,请在头文件中的
@接口内编写
-(void)calculateDays
。这样更好,它可以编译并运行,但在此行MainViewController MainViewController*mvs=[[MainViewController alloc]init]上崩溃;有什么想法吗?是否仍应在FlipsideViewController.m文件中添加[at]接口MainViewController()[at]端?如果它在
alloc init
上崩溃,那么您可能在初始化过程中做了一些糟糕的事情。检查潜在的悬空指针、
nil
参数等@Lindros调试器会说什么?谢谢,但如上所述,导入头文件会导致其他错误。请参阅我对H2CO3答案的评论。转发声明也不起作用,不幸的是,给了我一些语义问题