2013-08-28 33 views
0

我有一个非常奇怪的问题与UIScrollView并添加UIViewControllers。 看起来,当UIViewController添加到UIScrollView进行分页时,UIViewController会删除所有添加的对象。UIScrollView与IUViewController丢弃对象

在项目我有两个视图情节串连图板,并且它们正确地连接到相应的代码。

我知道这些代码不动添加的UIViewController到正确的X,但在本次测试我只是增加一个的UIViewController所以它不事关。

这是滚动码.H:

#import <UIKit/UIKit.h> 
#import "TestViewController.h" 

@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; 
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; 
@property (strong, nonatomic) NSMutableArray *scrollController; 

@end 

这是滚动码的.m:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

self.scrollController = [[NSMutableArray alloc] init]; 

} 

- (void)viewDidAppear:(BOOL)animated { 

//just adding two controllers 
TestViewController *first = [[TestViewController alloc] init]; 
[self.scrollView addSubview:first.view]; 
[self.scrollController addObject:first]; 

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.scrollController.count, self.scrollView.frame.size.height); 

self.pageControl.numberOfPages = [self.scrollController count]; 


} 

- (void)scrollViewDidScroll:(UIScrollView *)sender { 

// Update the page when more than 50% of the previous/next page is visible 
CGFloat pageWidth = self.scrollView.frame.size.width; 
int page = floor((self.scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 

self.pageControl.currentPage = page; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

这是代码的ViewController .H:

#import <UIKit/UIKit.h> 

@interface TestViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *lblMsg; 

@end 

这是视图控制器代码.m:

#import "TestViewController.h" 

@interface TestViewController() 

@end 

@implementation TestViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

NSLog(@"Label: %@", self.lblMsg); 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

在日志的输出是:

Label: (null) 

任何人都能够看到什么即时做错了什么?

回答

0

,如果您使用

TestViewController *first = [[TestViewController alloc] init]; 

您的标签(IBOutlet中)将不会被链接您的视图控制器。

导航到Xcode中的故事板,选择您的视图 - 控制和公用事业的右侧分配中的身份检查一个独特的故事板ID(“myIdentifier”)。 尝试

NSString *identifier = @"myIdentifier"; 
TestViewController *first = [self.storyboard instantiateViewControllerWithIdentifier:identifier]; 

看一看文档:

每个视图控制器对象是视图的唯一所有者。您必须 不会将同一个视图对象与多个视图控制器 对象关联。唯一的例外是,容器视图 控制器的实现可能会增加这一观点作为自己的 视图层次结构中的子视图。在添加子视图之前,容器必须首先调用其addChildViewController:方法来创建两个视图控制器对象之间的父子关系 。

+0

当然。我已经完全错过的那部分。它适用于我现在应该使用storyboard id和'TestViewController * first = [self.storyboard instantiateViewControllerWithIdentifier:identifier];'。谢谢您的帮助 –