[英]Call nib method from ViewController
Tell me please what I do wrong. 告诉我我做错了。 I have a instance of *nib file in ViewController.
我在ViewController中有一个* nib文件的实例。 ViewController implements UITextFieldDelegate.
ViewController实现UITextFieldDelegate。 In the delegate method "textField shouldChangeCharactersInRange" I call method written in *nib class.
在委托方法“ textField shouldChangeCharactersInRange”中,我调用以* nib类编写的方法。 In *nib class I have a User object initialised, but in the method ViewController calls User object is nil...
在* nib类中,我初始化了一个User对象,但是在ViewController调用的方法中,User对象为nil ...
Here is the code.. 这是代码。
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.usersTableView = [[UsersTableView alloc] init];
// Call delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[self.usersTableView searchTextFieldDidChange:searchString];
}
*Nib class *近战课程
#import "User.h"
@interface UsersTableView()
@property (nonatomic) User *user;
@end
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"UsersTableView" owner:self options:nil];
[self addSubview:self.view];
self.user = [[User alloc] init];
}
return self;
}
.....Anyware [self testMethod];
- (void)testMethod {
NSLog (@"%@", self.user) // user object exist
}
// Method called from ViewController
- (void)searchTextFieldDidChange:(NSString *)searchText {
NSLog (@"%@", self.user) // user object is nil...
}
any suggestions ? 有什么建议么 ?
I see many errors in your implementation. 我在您的实施过程中发现许多错误。 Please let me correct them one by one.
请让我一一纠正。
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[[NSBundle mainBundle] loadNibNamed:@"UsersTableView" owner:self options:nil];
[self addSubview:self.view];
self.user = [[User alloc] init];
}
return self; }
loadNibNamed: method will return a UIView* array. loadNibNamed:方法将返回一个UIView *数组。 If you want to use views from your nib, then you need to use its return value.
如果要使用笔尖的视图,则需要使用其返回值。
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
NSArray* viewArray = [[NSBundle mainBundle] loadNibNamed:@"UsersTableView" owner:self options:nil];
[self addSubview:[viewArray firstObject]];
self.user = [[User alloc] init];
}
return self;
}
You create your tableView with a simple alloc init, which doesn't call initWithCoder method. 使用简单的alloc初始化创建tableView,该初始化不调用initWithCoder方法。
self.usersTableView = [[UsersTableView alloc] init]; self.usersTableView = [[UsersTableView alloc] init];
I think you should implement the - (instancetype)init
method, and put you custom view initialisation there. 我认为您应该实现
- (instancetype)init
方法,并将自定义视图初始化放在此处。
-(instancetype)init{
self = [super init];
if (self) {
NSArray* viewArray = [[NSBundle mainBundle] loadNibNamed:@"UsersTableView" owner:self options:nil];
[self addSubview:[viewArray firstObject]];
self.user = [[User alloc] init];
}
return self;
}
I hope it will help, and it will work. 我希望它会有所帮助,并且会成功。
initWithCoder gets called only when you instantiate a view controller from story board. 仅当您从故事板上实例化视图控制器时,才会调用initWithCoder。 Unfortunately, In your case user object is getting initialised inside initWithCoder and you are using init to create the instance of viewcontroller.
不幸的是,在您的情况下,用户对象正在initWithCoder内部初始化,并且您正在使用init创建viewcontroller的实例。
Try loading viewcontroller from story board. 尝试从故事板上加载ViewController。
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
or if you want to load view controller from xib use 或者如果您想使用Xib加载View Controller
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"Your_nib_name" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
If you are still not satisfied override init of your nib class and instantiate user object inside init method though I wont prefer doing that :) 如果您仍然不满意,请重写nib类的init并在init方法中实例化用户对象,尽管我不喜欢这样做:)
Happy coding :) 快乐的编码:)
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [[UIViewContoller alloc]init];
vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
[self presentViewController:vc animated:YES complited:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.