希望你能帮到你。
我已经创建了自己的自定义 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];
}
自定义 View .h
#import <UIKit/UIKit.h>
@class SubItemView;
@interface SubItemView : UIScrollView {
}
@property (nonatomic, retain) NSMutableArray *cvSubitems;
@property (nonatomic, retain) NSString *teststring;
@end
自定义 View .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>"
)
自定义 UIScrollView 输出的第二个 NSLog:
Subclass Properties: (null)
我有点新手,所以我显然在这里遗漏了一些东西(可能很明显)。我真的很难将一个数组甚至一个简单的字符串传递到自定义类中,然后通过 NSLog 输出它的内容。
非常感谢任何帮助。
最佳答案
当调用 initWithFrame 方法时,您的 cvSubitems 属性尚未设置,因为它仅在您调用 initWithFrame 之后设置。
再次尝试在 View 初始化后调用的方法中记录数组值,或提供自定义初始化方法(例如 initWithMyData: andFrame:
)来解决此问题。
关于ios - 将数组和字符串传递给自定义 UIView 类(子类)(iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8333855/