简体   繁体   English

将xib插座加载到另一个视图控制器

[英]load xib outlets to another view controller

I have a Viewcontoller called BookingMain and in the run time it will subview the xib called BookingForm to it. 我有一个名为BookingMain ,在运行时它将子视图名为BookingForm的xib。 in BookingForm there are outlets fullName, Email, Telephone . BookingForm有网点全名fullName, Email, Telephone

im trying to access these outlets from my BookingMain . 我试图从我的BookingMain访问这些商店。

So what i have done is under @implementation BookingMain i added BookingForm *book; 所以我所做的是在@implementation BookingMain我添加了BookingForm *book; then tried to access those BookingForm out lets this way. 然后尝试通过这种方式访问​​那些BookingForm

NSString *Name;
book.fullName.text = Name;

//book.fullName.text : this is always null

but it gives me null value. 但这给了我空值。

im new to iOS and couldn't figure it out. 我是iOS的新手,无法弄清楚。

Updated : 更新 :

.H file

@interface BookingForm : UIView

@property (strong, nonatomic) IBOutlet UIView *view;


@property (weak, nonatomic) IBOutlet UITextField *fullName;
@property (weak, nonatomic) IBOutlet UITextField *email;
@property (weak, nonatomic) IBOutlet UITextField *phone;

.m File

#import "BookingForm.h"

@implementation BookingUserDetails

-(id)initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];

    if (self) {

        NSString *xib = @"BookingForm";

        [[NSBundle mainBundle] loadNibNamed:xib owner:self options:nil];

        [self addSubview:self.view];
    }

    return self;

}

@end

A couple of things to check here : 这里有几件事要检查:

  • When you are creating the BookingForm, using [BookingForm alloc] init]; 在创建BookingForm时,使用[BookingForm alloc] init]; You are calling the init method. 您正在调用init方法。 So add an init method and replicate the code you currently have in the initWithCoder method. 因此,添加一个init方法并复制您当前在initWithCoder方法中拥有的代码。

  • In the code you have below, you are just declaring a variable called Name. 在下面的代码中,您只是声明一个名为Name的变量。 At this point its is still nil since the Name has not be allocated or initialized. 此时,由于尚未分配或初始化名称,因此它仍然为零。 When you assign this to book.fullName.text, this is also going to be nil. 当您将其分配给book.fullName.text时,它也将为零。

     NSString *Name; book.fullName.text = Name; //book.fullName.text : this is always null 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM