Use caution when overriding the Init method in iOS development
Today in writing a small software, a bit of a problem, the function of this software is to search the National School, the homepage is the search input box, in the box to query the school in the province, click Buttom will jump to the corresponding view controller, and then the search results presented on a tableview, But I am debugging, every time I input and then click on the Search button, pop-up results list is always empty, I need to go back to the first page and then click on the search to appear results, and I change the search keyword on the homepage, click Search, results or the last search results, you must return a click to appear this time the search results.
After the great God, the reason for this problem is that I mistakenly used the rewrite Init method. I rewrite the method as follows, originally intended to be initialized at the time to give the view a title and background color:
1 // 2 - (Instancetype) init{ Span style= "color: #008080;" >3 if (self = [super Init]) { 4 self.view.backgroundColor = Screencolormygray; 5 self.title = @ " search ; 6 7 return self; 8 }
But the problem arises in this overriding initialization method, which is analyzed as follows:
First create a search page in the homepage:
1 resulttableviewcontroller *RESULTTABLEVC = [[Resulttableviewcontroller alloc]init];
This time there is a problem, because it will call the Init method, if I do not rewrite the Init method, the default is to go viewdidload, this is correct, but because my custom method has a self.view, this time there is actually no view, So call Self.view after the first to create a view, this time will be called to a method:-(void) Loadview, and this method is to load the view, it is the same time I have to push the interface presented first, but because I have not transmitted data , so it will cause nothing in the search results list, So when using the custom Init method, be careful to use statements such as Self.view, sometimes lead to unpredictable errors, and because such errors on the computer is not an error, but the programmer's logic error, so caught in such a bug is difficult to pop out.
As a rookie, write down the mistakes you make, and help you make or less mistakes in the future.
Use caution when overriding the Init method in iOS development