简体   繁体   中英

Call nib method from ViewController

Tell me please what I do wrong. I have a instance of *nib file in ViewController. ViewController implements UITextFieldDelegate. In the delegate method "textField shouldChangeCharactersInRange" I call method written in *nib class. In *nib class I have a User object initialised, but in the method ViewController calls User object is 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. 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;
    }
  1. Custom class initialization:

You create your tableView with a simple alloc init, which doesn't call initWithCoder method.

self.usersTableView = [[UsersTableView alloc] init];

I think you should implement the - (instancetype)init method, and put you custom view initialisation there.

-(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. Unfortunately, In your case user object is getting initialised inside initWithCoder and you are using init to create the instance of viewcontroller.

Try loading viewcontroller from story board.

 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];

or if you want to load view controller from xib use

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 :)

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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