2013-05-09 46 views
0

首先,我想說我是iOS開發的新手。我在ScrollView中顯示xib文件的視圖時遇到問題。在ScrollView中顯示來自xib的視圖

此MyView只有一個標籤的綠色背景。下面是它的控制器代碼:

@implementation MyView 

    - (id)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 
     //init code 
     } 
     return self; 
    } 

    - (id) initWithCoder:(NSCoder *)aDecoder 
    { 
     if (self = [super initWithCoder:aDecoder]) { 
     [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; 
     } 

    return self; 
    } 

@end 

,這裏是從主控制器代碼哪裏是滾動型:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.size.height)]; 
    scroll.pagingEnabled = YES; 
    scroll.frame = CGRectMake(0, 20, 320, 350); 
    scroll.contentSize = CGSizeMake(320, 350); 
    scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    scroll.bounces = YES; 
    scroll.bouncesZoom = YES; 
    scroll.scrollEnabled = YES; 
    scroll.minimumZoomScale = 0.5; 
    scroll.maximumZoomScale = 5.0; 
    scroll.delegate = self; 
    scroll.pagingEnabled = YES; 

    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];  
    UIView *myView = [nibContents objectAtIndex:0]; 
    myView.frame = CGRectMake(0,20,300,300); 

    [scroll addSubview:myView]; 

}

只有我想要做的事情是,我想在ScrollView中查看MyView(帶有一個標籤的綠色背景)。如果我運行這個應用程序,scrollview是空的,裏面沒有任何內容。

也許這是一個愚蠢的問題,但正如我所說,我是這個新手。我有一個來自蘋果的PageControll應用程序,但對我來說,這是在我的iOS開始非常複雜的代碼。當然,我是用谷歌搜索...請告訴我問題在哪裏。謝謝

回答

1

你在哪裏定義滾動?它是你的viewcontroller的屬性嗎? 我覺得這裏的問題是,你不滾動視圖添加到您的ViewController的看法與

[self.view addSubview:scroll] 
+0

它是在ViewController的接口中定義的:'code' __strong IBOutlet UIScrollView * scroll; – DanielH 2013-05-09 13:55:19

+0

所以我試圖把這個代碼放入viewDidLoad方法中,並且工作正常!非常感謝你! – DanielH 2013-05-09 14:00:53

+0

非常歡迎:)您能否將我的答案標記爲正確? (綠色滴答下的投票) – micantox 2013-05-09 14:01:07

0

一切似乎正確。只需保留您的課堂中您從以下行中收到的視圖,並將其作爲子視圖添加到您的滾動視圖中即可。

self.myView = [nibContents objectAtIndex:0]; 

最好的問候。

+0

我無法使用保留,如果我使用ARC。我對嗎? – DanielH 2013-05-09 13:42:53

+0

使用強而不是等於保留 – Arun 2013-05-09 13:43:29

+0

它沒有幫助... – DanielH 2013-05-09 13:47:56