Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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/1/visual-studio-2012/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
Arrays 如何从其他类访问NSMutableArray_Arrays_Xcode_Class_Variables_Nsmutablearray - Fatal编程技术网

Arrays 如何从其他类访问NSMutableArray

Arrays 如何从其他类访问NSMutableArray,arrays,xcode,class,variables,nsmutablearray,Arrays,Xcode,Class,Variables,Nsmutablearray,我已经研究了两天了,似乎无法教会自己如何从其他班级访问我的NSMutableArray。我想用旧selectedArray数组中的信息填充新的buildButtonText数组。 我将发布所有相关的代码,我将感谢任何人花时间来帮助我找到什么是挂我了。提前感谢您抽出时间 在ViewController.h中: @interface ViewController : UIViewController{ NSMutableArray *selectedArray; }

我已经研究了两天了,似乎无法教会自己如何从其他班级访问我的NSMutableArray。我想用旧selectedArray数组中的信息填充新的buildButtonText数组。 我将发布所有相关的代码,我将感谢任何人花时间来帮助我找到什么是挂我了。提前感谢您抽出时间

在ViewController.h中:

    @interface ViewController : UIViewController{
    NSMutableArray *selectedArray;
    }
    @property (copy, nonatomic) NSMutableArray *selectedArray;
    @end
在ViewController.m中:

    #import "ViewController.h"
    #import "ViewController2.h"

    @interface ViewController ()
    @end

    - (void)viewDidLoad
    {
     selectedArray=[[NSMutableArray alloc]init];
    }

    //....Other methods which dictate the objects in the final "selectedArray"....

    -(IBAction)Next:(id)sender{
    ViewController2 *nextViewController = [[ViewController2 alloc]init];
    nextViewController.buildButtonText = selectedArray;
    }
    //.....Or perhaps here I should use "prepareForSegue" to pass objects...?
在ViewController2.h中:

    #import "ViewController.h"
    @interface ViewController2 : UIViewController{
    NSMutableArray *buildButtonText;
    }
    @property (strong, nonatomic) NSMutableArray *buildButtonText;
    @end
在ViewController2.m中:

    #import "ViewController2.h"
    #import "ViewController.h"
    @interface ViewController2 ()
    @end

    - (void)viewDidLoad{
    buildButtonText=[[NSMutableArray alloc]init];
    NSLog(@"Count of array: %d", [buildButtonText count]);
    }

日志将返回数组中的0项。我还尝试从第二视图控制器询问变量,如果这实际上是正确的协议,我希望进行简短的演示。如果你不能从我的代码中看出我是新的。

为什么不使用prepareForSegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"YOUR_SEGUE_IDENTIFIER"])
    {
        SECOND_VIEW_CONTROLLER *svc=[segue destinationViewController];
        svc.NSMUTABLEARRAY_TO_RECEIVE=NSMUTABLEARRAY_TO_SEND;
    }
}
也许这有帮助?只需替换大写字母即可


我最近刚刚启动了iOS应用程序开发,我在同样的情况下使用了类似的东西,效果非常好!确保将第二个ViewController的.h文件导入到第一个ViewController中。

我假设在这一行:nextViewController.buildButtonText=selectedArray;selectedArray中有对象吗?另外,您如何演示ViewController2?我很想为ViewController 2创建一个自定义的init方法,然后将selectedArray传递给它。感谢您的快速响应!是,阵列中有对象。它们将添加并在第一个视图控制器中使用。我通过故事板制作了ViewController 2,并通过“下一步”按钮连接了转换。你是说我应该试着从ViewController2.m调用变量?我会在viewDidload中执行此操作吗?感谢您的时间和专业知识。感谢您的明确答复。这似乎应该可以工作,但我收到一个无法识别的选择器发送到实例错误。segue标识符、头文件和所有变量似乎都是有序的,但我将在第二天使用它,希望能够“接受”这个答案。@Wisam22 hmmm,哪一个选择器应该是不可识别的?实际上,“不可识别的选择器”只是我删除一个方法并忘记删除情节提要中的连接的结果。但是,第二个数组仍在“我的第二个视图控制器”中返回0项。是否需要任何其他分配/初始化来允许控制器彼此看到?@Wisam22 segue的destinationViewController选择器返回将由segue实例化的ViewController实例。还要记住,prepareForSegue发生在第二个ViewController的viewDidLoad smethod之前。