簡體   English   中英

從ViewController調用nib方法

[英]Call nib method from ViewController

告訴我我做錯了。 我在ViewController中有一個* nib文件的實例。 ViewController實現UITextFieldDelegate。 在委托方法“ textField shouldChangeCharactersInRange”中,我調用以* nib類編寫的方法。 在* nib類中,我初始化了一個User對象,但是在ViewController調用的方法中,User對象為nil ...

這是代碼。

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

}

*近戰課程

#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...
}

有什么建議么 ?

我在您的實施過程中發現許多錯誤。 請讓我一一糾正。

-(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:方法將返回一個UIView *數組。 如果要使用筆尖的視圖,則需要使用其返回值。

- (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. 自定義類初始化:

使用簡單的alloc初始化創建tableView,該初始化不調用initWithCoder方法。

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

我認為您應該實現- (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;
        }

我希望它會有所幫助,並且會成功。

僅當您從故事板上實例化視圖控制器時,才會調用initWithCoder。 不幸的是,在您的情況下,用戶對象正在initWithCoder內部初始化,並且您正在使用init創建viewcontroller的實例。

嘗試從故事板上加載ViewController。

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

或者如果您想使用Xib加載View Controller

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"Your_nib_name" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];

如果您仍然不滿意,請重寫nib類的init並在init方法中實例化用戶對象,盡管我不喜歡這樣做:)

快樂的編碼:)

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM