将数组和字符串传递给自定义 UIView 类(子类)(iOS)

发布于 2024-12-19 01:41:14 字数 1700 浏览 1 评论 0原文

希望你能帮忙。

我已经创建了自己的自定义 UIView,并且尝试传递一个 NSMutableArray,该数组是我根据 sqlite 数据库中的数据设置的。我已经从 ViewController 中对数组进行了 NSLog 处理,并且一切都显示正确。

不过我想将这个 NSMutableArray 传递到我的自定义 UIView (它实际上是一个 UIScrollView)中,以便我可以做一些魔法。但是,当我这样做时,我的 NSLog 显示输出为(null)。

这是我的代码(我还传递了一个测试字符串来帮助查看它是否特定于数组,但事实并非如此):

viewcontroller.m(刚刚显示了自定义类调用 - NSLog 输出数组内容(请参阅示例末尾) )

- (void)viewDidLoad
{
...
NSString *teststring = @"Testing";
NSLog(@"Subitems: %@", subitems);
SubItemView* subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
subitemview.cvSubitems = subitems;
subitemview.teststring = teststring;
[self.view addSubview:subitemview];
}

customview.h

#import <UIKit/UIKit.h>

@class SubItemView;

@interface SubItemView : UIScrollView {

}


@property (nonatomic, retain) NSMutableArray *cvSubitems;
@property (nonatomic, retain) NSString *teststring;

@end

customview.m

#import "SubItemView.h"

@implementation SubItemView

@synthesize cvSubitems;
@synthesize teststring;

- (id)initWithFrame:(CGRect)frame
{   
    CGRect rect = CGRectMake(0, 0, 400, 400);
    NSLog(@"Subclass Properties: %@", self.cvSubitems);
    self = [super initWithFrame:rect];

    if (self) {
        // Initialization code
    }
    return self;
}

viewcontroller.m 中的第一个 NSLog 输出:

Subitems: (
    "<SubItems: 0x6894400>",
    "<SubItems: 0x6894560>"
)

来自 Custom UIScrollView 输出的第二个 NSLog:

Subclass Properties: (null)

我是一个新手,所以我显然在这里遗漏了一些东西(可能是明显的)。我只是在努力将数组甚至简单的字符串传递到自定义类中,然后通过 NSLog 输出其内容,

非常感谢。

Hope you can help.

I have created my own Custom UIView and I'm trying to pass in a NSMutableArray which I have setup from data from a sqlite DB. I have NSLog'd the Array from the ViewController and everything is showing correctly.

However I want to pass this NSMutableArray into my Custom UIView (it's actually a UIScrollView) so that I can do some magic. However when I do this, my NSLog show's the output as (null).

Here is my code (I've also passed a test string to help to see if it's Array specific, but it isn't):

viewcontroller.m (just shown the Custom class call - NSLog outputs the Array contents (see end of examples)

- (void)viewDidLoad
{
...
NSString *teststring = @"Testing";
NSLog(@"Subitems: %@", subitems);
SubItemView* subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
subitemview.cvSubitems = subitems;
subitemview.teststring = teststring;
[self.view addSubview:subitemview];
}

customview.h

#import <UIKit/UIKit.h>

@class SubItemView;

@interface SubItemView : UIScrollView {

}


@property (nonatomic, retain) NSMutableArray *cvSubitems;
@property (nonatomic, retain) NSString *teststring;

@end

customview.m

#import "SubItemView.h"

@implementation SubItemView

@synthesize cvSubitems;
@synthesize teststring;

- (id)initWithFrame:(CGRect)frame
{   
    CGRect rect = CGRectMake(0, 0, 400, 400);
    NSLog(@"Subclass Properties: %@", self.cvSubitems);
    self = [super initWithFrame:rect];

    if (self) {
        // Initialization code
    }
    return self;
}

The first NSLog in the viewcontroller.m outputs:

Subitems: (
    "<SubItems: 0x6894400>",
    "<SubItems: 0x6894560>"
)

The second NSLog from the Custom UIScrollView outputs:

Subclass Properties: (null)

I am a bit of a newbie, so I'm obviously missing something (possibly obvious) here. I am just really struggling to pass an Array and even a simple string into a Custom class and just output it's contents via NSLog.

Any help is gratefully appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-12-26 01:41:14

那么,当调用 initWithFrame 方法时,您的 cvSubitems 属性尚未设置,因为它仅在调用 initWithFrame之后设置。

再次尝试在视图初始化后调用的方法中记录数组值,或提供自定义 init 方法(例如 initWithMyData: andFrame:)来解决此问题。

Well when your initWithFrame method is called, your cvSubitems property isn't set yet, as it is set only after your call to initWithFrame.

Try again to log your arrays value in a method that is called after your view is initialized, or provide a custom init method (e.g. initWithMyData: andFrame:) to solve this issue.

清欢 2024-12-26 01:41:14

因此,为了澄清已经说过的话,你打电话是不按顺序的。

1| SubItemView* subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
2| subitemview.cvSubitems = subitems;
3| subitemview.teststring = teststring;
  • 在第 1 行,您正在调用 SubItemView 上的 initWithFrame: 方法
  • 在第 2 行和第 3 行,您正在设置 ivars

重点是您正在设置 ivars(第 2+3 行) )在 initWithFrame: 方法返回后。

但是您试图在initWithFrame:方法中打印ivars

您还尝试在分配self之前记录ivars,这是也不是一个好主意

NSLog(@"Subclass Properties: %@", self.cvSubitems);
self = [super initWithFrame:rect];

要证明它们已被设置,您可以从实例化的位置打印:

SubItemView *subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
subitemview.cvSubitems = subitems;
subitemview.teststring = teststring;
NSLog(@"Subclass Properties: %@", subitemview.cvSubitems);

So to clarify what has already been said, you are calling out of order.

1| SubItemView* subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
2| subitemview.cvSubitems = subitems;
3| subitemview.teststring = teststring;
  • On line 1 you are calling the initWithFrame: method on SubItemView
  • On lines 2 and 3 you are setting the ivars

The point being that you are setting the ivars (lines 2+3) after the initWithFrame: method has returned.

But you are trying to print the ivars in the initWithFrame: method

You are also trying to log the ivars before you have even assigned self which is not a good idea either

NSLog(@"Subclass Properties: %@", self.cvSubitems);
self = [super initWithFrame:rect];

To prove that they are being set you can just print from where you instantiate:

SubItemView *subitemview = [[SubItemView alloc] initWithFrame:CGRectMake(150,150,0,0)];
subitemview.cvSubitems = subitems;
subitemview.teststring = teststring;
NSLog(@"Subclass Properties: %@", subitemview.cvSubitems);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文