Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 如何使用swift访问在方法中创建的变量或uicontrol?_Ios_Variables_Methods_Swift_Controls - Fatal编程技术网

Ios 如何使用swift访问在方法中创建的变量或uicontrol?

Ios 如何使用swift访问在方法中创建的变量或uicontrol?,ios,variables,methods,swift,controls,Ios,Variables,Methods,Swift,Controls,我正在使用swift构建一个应用程序,因此我有一个问题: 当我在一种方法中创建变量或uicontrol时,如何在另一种方法中访问它 ps:由于逻辑代码,我不想在方法之外创建变量 class abc { func method1() { var img = UIImageView(); var xyz = 1; } func method2() { // access to img control here /

我正在使用swift构建一个应用程序,因此我有一个问题:

当我在一种方法中创建变量或uicontrol时,如何在另一种方法中访问它

ps:由于逻辑代码,我不想在方法之外创建变量

class abc
{
    func method1()
    {
      var img = UIImageView();
      var xyz = 1;
    }
    func method2()
    {
      // access to img control here
      // access to variable xyz here
    }
}
使用元组:

   class abc
    {
        func method1()->(UIImageView, Int)
        {
          var img = UIImageView();
          var xyz = 1;
        }
        func method2()
        {
          let (imgControl, xyz) = method1()
          // access to img control here
          // access to variable xyz here
        }
    }

您必须以某种方式将创建的变量共享给另一个方法。这可以通过实例变量、某种类型的全局变量实现,也可以通过某种方式从方法返回。如何使用swift实现这一点?这是我的问题。PHP有全局关键字来设置全局变量,但我没有看到用这种方式在SWIFT中,我必须返回值。如果我像这样在func中定义控件,它是如何工作的:func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indexPath:NSIndexPath)->(UIImageView,testvar:Int){