希望你能幫助。傳遞數組和字符串到自定義UIView類(子類)(iOS)
我已經創建了自己的自定義UIView,我試圖傳入一個NSMutableArray,我已經從sqlite數據庫中設置了數據。我有從ViewController的NSLog'd數組,一切都正確顯示。
但是我想通過這個NSMutableArray到我的自定義UIView(它實際上是一個UIScrollView),以便我可以做一些魔術。但是,當我這樣做時,我的NSLog顯示輸出爲(空)。
這裏是我的代碼(我也通過了測試字符串幫忙看看它的具體陣,但它不是):
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;
}
第一的NSLog在viewcontroller.m輸出:
Subitems: (
"<SubItems: 0x6894400>",
"<SubItems: 0x6894560>"
)
第二NSLog的從自定義的UIScrollView輸出:
Subclass Properties: (null)
我有點新手的,所以我顯然在這裏丟失了一些東西(可能很明顯)我真的很努力地將一個數組,甚至一個簡單的字符串傳遞給一個自定義類,並通過NSLog輸出它的內容。
感謝任何幫助。
viewWill Appear是UIViewController方法,而不是UIView方法。 – jrturton
是的,我錯過了,更正了我的回答 –
非常感謝。最後,我重寫了initWithFrame初始化方法,並添加了要傳入的數組。 .H ' - (ID)initWithFrame:方法(的CGRect)幀的TestString:(的NSString *)TS cvsubitems:(NSMutableArray的*)CVS;' 的.m ' - (ID)initWithFrame:方法(的CGRect)幀的TestString: (NSString *)ts cvsubitems:(NSMutableArray *)cvs {0} self.teststring = ts; self.cvSubitems = cvs; ...' –