scrollview添加其他页面的xib视图

时间:2013-11-19 13:35:40

标签: iphone uiscrollview ios7 xib

我有两个.xib文件。第一个xib名称是viewcontroller.xib,它包含使用页面控件的scrollview。

第二个xib名称是registration.xib,它包含标签textfield和submit按钮。

我的问题是我想将registration.xib文件的全部视图添加到viewcontroller.xib文件的scrollview中。

我能做什么????????????

        //In viewdidload of viewcontroller.m

        regiViewController=[[RegistrationViewController alloc]init];

        regiViewController.view11.translatesAutoresizingMaskIntoConstraints=NO;


        UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        containerView.backgroundColor = [UIColor purpleColor];

        [containerView addSubview:regiViewController.view11];


        [scrollView addSubview:containerView];

在registration.m

       - (void)setup {
[[NSBundle mainBundle] loadNibNamed:@"RegistrationViewController" owner:self options:nil];
[view11 addSubview:self.view];

self.view11.translatesAutoresizingMaskIntoConstraints=NO;

}

2 个答案:

答案 0 :(得分:0)

我个人会用

[[RegistrationViewController alloc] initWithNibName:@"RegistrationViewController" bundle:nil];

而不是从[setup]方法加载XIB,但那只是我。但是,在您当前的代码中viewcontroller.m从不调用setup方法,因此永远不会加载XIB。试试这个:

//In viewdidload of viewcontroller.m

regiViewController=[[RegistrationViewController alloc]init];

[regiViewController setup];

UIView *containerView=[[UIView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];

[containerView addSubview:regiViewController.view11];


[scrollView addSubview:containerView]; 

答案 1 :(得分:0)

感谢亲爱的回复我,但你的代码无效...我的设置方法在此行之后调用

            regiViewController=[[RegistrationViewController alloc]init];

//再次参见viewcontroller.m

              regiViewController=[[RegistrationViewController alloc]init];

    regiViewController.view11.translatesAutoresizingMaskIntoConstraints=NO;


    UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
    containerView.backgroundColor = [UIColor purpleColor];

    [containerView addSubview:regiViewController.view11];


    [scrollView addSubview:containerView];

// registrationviewcontroller.m

      #import "RegistrationViewController.h"

     @interface RegistrationViewController ()

     @end

      @implementation RegistrationViewController


    @synthesize view11;

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
   {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
                 // Custom initialization
                 [self setup];
               }
     return self;
   }


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


}

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

- (void)setup {
[[NSBundle mainBundle] loadNibNamed:@"RegistrationViewController" owner:self         options:nil];
[view11 addSubview:self.view];

self.view11.translatesAutoresizingMaskIntoConstraints=NO;
}

  @end