ios - Objective-C 中的数组初始化问题

标签 ios objective-c arrays

我在将信息从一个 View Controller 输入到另一个 View Controller 时遇到问题。这个函数在第二个 View Controller 中,并且在第一个 Controller 中多次调用,因为它在一个 for 循环中,并且我想将多个对象添加到数组 transfer_array 中。这些对象属于 Poi 类型,由 imageLocationX、imageLocationY 和名称组成。我有 BOOL 的原因是我试图只初始化数组一次。 Poi 对象在第一次调用函数时添加到数组中,再次调用函数后数组为空。

- (id)display:(double)imageXX andY:(double)imageYY withName:(NSString *)namee ifDone:(BOOL *)donee{
    NSLog(@"````````````````````````````````````````````````````````");

    NSLog(donee ? @"Yes" : @"No");
    if(donee == NO){
        transfer_array = [[NSMutableArray alloc] init];
    }

    NSLog(@"imageX: %f",imageXX);
    NSLog(@"imageY: %f", imageYY);
    NSLog(@"name: %@", namee);

    labelPoi = [[Poi alloc] init];
    labelPoi.imageLocationX = imageXX;
    labelPoi.imageLocationY = imageYY;
    labelPoi.name = namee;
    [transfer_array addObject:labelPoi];


    NSLog(@"label.x: %f should be: %f", labelPoi.imageLocationX, imageXX);
    NSLog(@"label.y: %f should be: %f", labelPoi.imageLocationY, imageYY);
    NSLog(@"label.name: %@ should be: %@",labelPoi.name,namee);
    NSLog(@"transssssfer: %lu", (unsigned long)transfer_array.count);

    return self;
}

这是第一次调用函数时的日志:

````````````````````````````````````````````````````````
2013-07-29 13:25:52.502 App[20856:11303] No
2013-07-29 13:25:52.502 App[20856:11303] imageX: 979.008057
2013-07-29 13:25:52.503 App[20856:11303] imageY: 115.728180
2013-07-29 13:25:52.503 App[20856:11303] name: Urgent Care
2013-07-29 13:25:52.503 App[20856:11303] label.x: 979.008057 should be: 979.008057
2013-07-29 13:25:52.503 App[20856:11303] label.y: 115.728180 should be: 115.728180
2013-07-29 13:25:52.503 App[20856:11303] label.name: Urgent Care should be:  Urgent Care
2013-07-29 13:25:52.503 App[20856:11303] transfer_array.count: 1

第二次:

2013-07-29 13:25:52.506 App[20856:11303] ````````````````````````````````````````````````````````
2013-07-29 13:25:52.506 App[20856:11303] Yes
2013-07-29 13:25:52.506 App[20856:11303] imageX: 224.485718
2013-07-29 13:25:52.506 App[20856:11303] imageY: 116.353401
2013-07-29 13:25:52.506 App[20856:11303] name: Student Health Center
2013-07-29 13:25:52.507 App[20856:11303] label.x: 224.485718 should be: 224.485718
2013-07-29 13:25:52.507 App[20856:11303] label.y: 116.353401 should be: 116.353401
2013-07-29 13:25:52.507 App[20856:11303] label.name: Student Health Center should be:  Student Health Center
2013-07-29 13:25:52.507 App[20856:11303] transfer_array.count: 0

我无法访问数组中的任何信息,因为它是空的。有谁知道我如何修改它以便该函数将不断添加我想要添加的对象而不是保持为空?

编辑

这是在第一个 View Controller 中调用函数的方式,这个方法是 friend 向我推荐的

PictureViewController *newlabel = [[PictureViewController alloc] display:PointOfInterest.imageLocationX andY:PointOfInterest.imageLocationY withName:PointOfInterest.name ifDone:done];
            if(done == NO){
                done = YES;
            }

最佳答案

尽管 esker 的回答确实消除了 done 的必要性,因此更可取,但这不是您的错误所在。您遇到的问题是,每次执行循环时,您都在初始化 PictureViewController 的新实例。在这个新对象中,数组尚未创建,因此它是 (null),返回计数 0。

您需要做的是创建一个 viewController 实例,然后将所有对象添加到其中。此外,在 init 方法中,您想调用 [super init] 或另一个版本的 super 初始化程序或您拥有的另一个调用 super 初始化程序的初始化程序。

关于ios - Objective-C 中的数组初始化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931525/

相关文章:

ios - 收不到ios通知

iOS 13 : Scroll view inside navigation controller won't go under the status bar

iphone - iOS : If app is not running in background, 调用 openURL 在启动时崩溃应用程序

javascript - 如果 Obj 中存在数组,则获取数组项,如果不存在则获取字段

ios - Swift 进度指示器图像 mask

iphone - beginAnimations 和 using animations block 之间的动画速度不同

ios - 约束不尊重大小类

ios - 构造为 ViewModel

c - 将数组传递给函数

c++ - 为什么我的列表值被覆盖?