我有一个非常奇怪的问题与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)
任何人都能够看到什么即时做错了什么?
当然。我已经完全错过的那部分。它适用于我现在应该使用storyboard id和'TestViewController * first = [self.storyboard instantiateViewControllerWithIdentifier:identifier];'。谢谢您的帮助 –